Call me crazy, but I was under the impression that there was a convention of try
which meant give it a go, but if you can't then get back to me and let me know that it was a "no-go".
I have recently started a new project and I have decided to use System.Text.Json
instead of Newtonsoft
since it has been out for a while now and I like to play with new things.
I have the following bit of code in a JsonConverter
:
using (var jsonDoc = JsonDocument.ParseValue(ref reader))
{
if (jsonDoc.RootElement.TryGetInt32(out int number))
{
}
}
When it is a number, it works awesomely, but when it is anything but a number, it throws as if I was calling GetInt32()
.
In the custom converter, I sometimes get a number back, but I also can get an object back which contains the number that I am expecting as well as a string. I thought that I would be able to test for this using the TryGetInt32
method.
I have two questions:
- How could I test if I am getting the number back, or getting the number AND the string?, and
- What is the difference between
TryGetInt32(out int number)
andGetInt32()
?