Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there options with Json.NET that can have it deserialize inconsistent json types into objects?

Tags:

json.net

An example would be that I am consuming json from an api. The api is not consistent in how it returns the json. Say you have an Author and it has a property of Books[]. The api is unfortunately choosing to return Author.Books (of type Book) in cases when there is only one book. The prefered method would be to return just one Book inside Author.Books[].

Json.NET understandably throws a serialization exception when I try to have it deserialize a chunk of json and it finds "Author":{"Book":{... mixed in with "Author":{"Book":["...

Is there a way around this?

like image 330
Roger Avatar asked Apr 29 '11 05:04

Roger


People also ask

What is JSON serialization deserialization?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

How do I deserialize an object in JSON?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

What is JSON serialization and deserialization in C#?

Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.

Can JsonSerializer deserialize return null?

The JSON.Net deserializer handles this and returns null. The JsonDeserializer in RestSharp successfully deserializes the string "null" to a null object, but then inside JsonDeserializer. Map(), the second parameter, which represents dictionary of data that has been deserialized, is null.


1 Answers

Does this answer your question?

Deserializing JSON when sometimes array and sometimes object

like image 116
James Newton-King Avatar answered Oct 03 '22 05:10

James Newton-King