I'm working on a legacy system that works with IotHub SDK. The application uses both System.Text.Json (there's an agreement to use this for current & future development) and Newtonsoft.Json for (de)serializing JSON.
I need to deserialize the following JSON into a Dictionary<string,object> because the value can either be a string or a JSON object:
{
"route1": "route 1 text",
"route2": {
"route": "route 2 text",
"priority": 1,
"timeToLiveSecs": 60
}
}
What I expect is that the value would be deserialized as:
- route1 as string
- route2 as object
But System.Text.Json deserializes both values as JsonElement. And to get the value, a method like GetString() needs to be called (see last row)
Because of the usage combination of Newtonsoft & System.Text.Json in this legacy application, serializing this object with Newtonsoft doesn't work.
The deserialization & re-serialization only works when the same libraries were used in both process. And IoTHub SDK from Microsoft itself, uses Newtonsoft.Json.
Is there a way to make both libraries work together in this case? I find it dangerous to use either one to deserialize an object type with this behavior, when there's a chance 3rd party libraries may use another library.