Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Json.NET deserialize "dynamic" properties?

Tags:

json

c#

my C# service is receiving objects from an external service with a "fuzzy" format that looks like:

{
  "member": {
      "<dynamicProperty>": {
          "value":"some_string",
          "score": 10
      }
}

This property "" can change for every object, I don't have a defined and restricted list for its possible values and of course I can't change this format.

Does anybody know if Json.NET or some other Json .NET serializer, could help me and allow me to define classes like Member and DynamicProperty below that I could use for an easy deserialization?

class Member
{
    [JsonProperty(PropertyName= "??")] // what should I put here?
    public DynamicProperty { get; set; }
}

class DynamicProperty
{
    public string value;
    public int score;
}

Thanks

like image 329
pierroz Avatar asked May 13 '14 16:05

pierroz


1 Answers

You can use a Dictionary<string, object>

class Member
{
     public Dictionary<string, object> { get; set; }
}

Or, you can use the JsonExtensionData Attribute:

class Member
{
     [JsonExtensionData]
     public Dictionary<string, JToken> { get; set; }
}
like image 154
Yuval Itzchakov Avatar answered Oct 19 '22 13:10

Yuval Itzchakov