I am having a problem while using the JsonObject.Parse. Am I allowed to use this directly or should I always use the JsonSerializer.DeserializeFromString<Object> instead?
This problem occured while adding a custom OAuth2 provider for Instagram. At some point I had to fetch some user data from the instagram API using the GetJsonFromUrl() method which returned something like this:
“{“meta”:{“code”:200},“data”:{“username”:“user”,“bio”:”",“website”:"",“full_name”:“username”,“id”:“12345”}}" .
Then, when I tried to parse the json string using the JsonObject.Parse() method, the result was not the expected. However, if I unescape the data (using obj.GetUnescaped(“data”)) I get the correct values.
JsonObject is what to use if you want to parse JSON dynamically, i.e. without deserializaing into a typed schema.
Ricardo Brandão:
Hi Demis,
I finally figure it out. I was trying to parse the data object using the JsonObject.Parse(root[“data”]) while should be using the root.Object(“data”). That was the reason why I was getting the odd values.
Thanks for the quick answer.
Stephen Brannan:
I’m going to high jack this post a little bit. +Demis Bellot Is there a reliable way to know if root[“data”] is an object vs an array using ServiceStack api?
Nope, SS expects you to tell it how it should interpret it.
Stephen Brannan:
So what your saying really is put in a feature request? There are situations where I am dynamically parsing using the JsonObject and don’t know if it’s an object, array or a simple value.
In what situations are you expected to parse something you don’t know the schema of?
Stephen Brannan:
Situations where the Model contains dynamic properties within a property called CustomProperties (type is Dictionary<string, object>). This is where I use JsonObject to parse it dynamically. For now I’ve written my own methods to determine the json types. The properties within this CustomProperties is defined outside of ServiceStack in a configurable definition file for custom properties.