Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling extra members when deserializing with Json.net

Tags:

c#

json.net

Suppose I want to deserialize a set of Json data into a Person object.

class Person
{
    [DataMember]
    string name;
    [DataMember]
    int age;
    [DataMember]
    int height;

    object unused;
}

But if I have the Json data like the one below:

{
    "name":"Chris",
    "age":100,
    "birthplace":"UK",
    "height":170,
    "birthdate":"08/08/1913",
}

The fields "birthdate" and "birthplace" are not part of the Person class. But I still want to retain those fields, so is it possible to use Json.net or other libraries that can store those extra fields into one of the fields of Person such as "unused" as declared above?

like image 609
user2395837 Avatar asked Aug 20 '14 02:08

user2395837


People also ask

How do I deserialize 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 exceptions does Jsonconvert Deserializeobject throw?

Serialization or deserialization errors will typically result in a JsonSerializationException .

What is Jsonproperty attribute?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.

What is MissingMemberHandling?

MissingMemberHandling Property. Gets or sets how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization. The default value is Ignore. Namespace: Newtonsoft.Json.


2 Answers

You should be able to use the [JsonExtensionData] attribute for this: http://james.newtonking.com/archive/2013/05/08/json-net-5-0-release-5-defaultsettings-and-extension-data

void Main()
{
    var str = "{\r\n    \"name\":\"Chris\",\r\n    \"age\":100,\r\n    \"birthplace\":\"UK\",\r\n    \"height\":170," +
    "\r\n    \"birthdate\":\"08/08/1913\",\r\n}";
    var person = JsonConvert.DeserializeObject<Person>(str);
    Console.WriteLine(person.name);
    Console.WriteLine(person.other["birthplace"]);
}

class Person
{
    public string name;
    public int age;
    public int height;
    [JsonExtensionData]
    public IDictionary<string, object> other;
}
like image 65
StriplingWarrior Avatar answered Nov 11 '22 13:11

StriplingWarrior


Yes,you can do it with JSON.NET:

dynamic dycperson= JsonConvert.DeserializeObject(@"{
'name':'Chris',
'age':100,
'birthplace':'UK',
'height':170,
'birthdate':'08/08/1913'}");
Person person = new Person{
  name = dycperson.name,
  age=dycperson.age,
  height=dycperson.height,
  unused= new {birthplace = dycperson.birthplace, birthdate=dycperson.birthdate}
};
like image 26
codeyu Avatar answered Nov 11 '22 14:11

codeyu