2

What could be the reason for the following code to suddenly be throwing an error after upgrading the JSON.NET library to v10:

var name = json.Value<string>("Name"); // Error: Object must implement IConvertible

The error goes away if I change the line to indexer syntax:

var name = (string)json["Name"];

The puzzling bit is that the former line used to work perfectly fine with v6 of the library, but fails after upgrading to v10.

I'm also detecting a different behavior of null comparisons, which I also suspect is caused by the upgrade:

if (json["Name"] != null)
{
    // I find myself in here because JTokenType.Null was unexpectedly returned above.
    // This wasn't the case in v6!
}

Was there a breaking change somewhere between v6 and v10 of Newtonsoft.Json library? If not, what else could be causing my problems (especially the first one)?

aoven
  • 2,248
  • 2
  • 25
  • 36
  • What is the value at that time? – Patrick Hofman Jun 08 '17 at 14:13
  • You can check here about changes in each version: https://github.com/JamesNK/Newtonsoft.Json/releases – Evk Jun 08 '17 at 14:17
  • @Patrick: I had to replicate the exact dataset that was involved when the problem was first caught and run it under debugger. The value in question is of System.Guid type. This one doesn't implement IConvertible so this couldn't have worked before unless v6 of JSON.NET did things differently for Guids. – aoven Jun 08 '17 at 14:47
  • Json.NET did make some changes in its automatic recognition of GUIDs between versions 5, 6 and 7; see for instance https://stackoverflow.com/a/30384841/3744182 – dbc Jun 08 '17 at 19:46

1 Answers1

1

It turns out that the problematic value was a JToken of type Guid. I can't currently verify if this indeed worked differently in JSON.NET v6, but it seems to me that JToken.Value<T>() method performs conversion to T differently than typecasting JToken instance directly to T does. I also can't tell at this time whether this is a bug in JSON.NET or simply a gotcha that needs to be remembered.

Sadly, I'm no closer to the explanation for my second problem, because I don't have a reproducible case, yet.

aoven
  • 2,248
  • 2
  • 25
  • 36