Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize json with known and unknown fields

Tags:

json

c#

json.net

Given following json result: The default json result has a known set of fields:

{
    "id": "7908",
    "name": "product name"
}

But can be extended with additional fields (in this example _unknown_field_name_1 and _unknown_field_name_2) of which the names are not known when requesting the result.

{
    "id": "7908",
    "name": "product name",
    "_unknown_field_name_1": "some value",
    "_unknown_field_name_2": "some value"
}

I would like the json result to be serialized and deserialized to and from a class with properties for the known fields and map the unknown fields (for which there are no properties) to a property (or multiple properties) like a dictionary so they can be accessed and modified.

public class Product
{
    public string id { get; set; }
    public string name { get; set; }
    public Dictionary<string, string> fields { get; set; }
}

I think I need a way to plug into a json serializer and do the mapping for the missing members myself (both for serialize and deserialize). I have been looking at various possibilities:

  • json.net and custom contract resolvers (can't figure out how to do it)
  • datacontract serializer (can only override onserialized, onserializing)
  • serialize to dynamic and do custom mapping (this might work, but seems a lot of work)
  • let product inheriting from DynamicObject (serializers work with reflection and do not invoke the trygetmember and trysetmember methods)

I'm using restsharp, but any serializer can be plugged in.

Oh, and I cannot change the json result, and this or this didn't help me either.

Update: This looks more like it: http://geekswithblogs.net/DavidHoerster/archive/2011/07/26/json.net-custom-convertersndasha-quick-tour.aspx

like image 829
nickvane Avatar asked Mar 06 '13 17:03

nickvane


People also ask

What is the difference between serialize and deserialize JSON?

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 a JSON file?

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.

How do you tell Jackson to ignore a field during deserialization?

Overview So when Jackson is reading from JSON string, it will read the property and put into the target object. But when Jackson attempts to serialize the object, it will ignore the property. For this purpose, we'll use @JsonIgnore and @JsonIgnoreProperties.

What does Jsonconvert Deserializeobject do?

Deserializes the JSON to the specified . NET type. Deserializes the JSON to the specified . NET type using a collection of JsonConverter.


2 Answers

An even easier option to tackling this problem would be to use the JsonExtensionDataAttribute from JSON .NET

public class MyClass
{
   // known field
   public decimal TaxRate { get; set; }

   // extra fields
   [JsonExtensionData]
   private IDictionary<string, JToken> _extraStuff;
}

There's a sample of this on the project blog here

UPDATE Please note this requires JSON .NET v5 release 5 and above

like image 113
cecilphillip Avatar answered Oct 09 '22 02:10

cecilphillip


See https://gist.github.com/LodewijkSioen/5101814

What you were looking for was a custom JsonConverter

like image 21
Lodewijk Avatar answered Oct 09 '22 04:10

Lodewijk