2

We are getting similar response from one of 3rd party API providers:

{
    "$id" : "1",
    "$values" : [
        {
            "$id" : "2",
            "Description": "Description"
            "$values" : [
                {
                    "$id" : "1",
                    "IntProperty" : 6,
                    "StringProperty" : "Test 1",
                    "DecimalProperty" : 11.23
                },
                {
                    "$id" : "2",
                    "IntProperty" : 7,
                    "StringProperty" : "Test 2",
                    "DecimalProperty" : 12.23
                },
                {
                    "$id" : "3",
                    "IntProperty" : 8,
                    "StringProperty" : "Test 3",
                    "DecimalProperty" : 11.24
                }
            ]
        }
    ]
}

As you can see for some reason they are using "$id" to provide db id to the API user. However, when JSON.net attempts to deserialize such string it is reading "$id" as object reference. I tried to use JsonProperty attribute as well as PreserveReferencesHandling.None and TypeNameHandling.All but this doesn't seem to work. Is there any other option than do a string.Replace("\"$id\"", "\"id\"")?

Data structure I'm trying to convert it to:

public class RootObject
{
    [JsonProperty( PropertyName = "$id" )]
    public string Id { get; set; }
    [JsonProperty( PropertyName = "$values" )]
    public List<ChildObject> Values { get; set; }
}

public class ChildObject
{
    [JsonProperty( PropertyName = "$id" )]
    public string Id { get; set; }
    public string Description { get; set;}
    [JsonProperty( PropertyName = "$values" )]
    public List<ChildObject1> Values { get; set; }
}   

public class ChildObject1
{
    JsonProperty( PropertyName = "$id" )]
    public string Id { get; set; }
    public int IntProperty { get; set; }
    public string StringProperty { get; set;}
    public decimal DecimalProperty { get; set;}
}
AlexanderM
  • 1,613
  • 4
  • 21
  • 35
  • What are you trying to deserialize it to? Do you have model class(es) you are trying to populate? You could create a custom `JsonConverter` or you might be able to get away with just using `JsonPropertyAttribute` and setting the name there. – Matt Burland Oct 07 '14 at 15:04
  • [JsonProperty( PropertyName = "$id" )] didn't worked. Looks like these overrides take effect after object references read so by that time I'm already getting duplicate reference error. – AlexanderM Oct 07 '14 at 15:08
  • A really _ugly_ solution would be to replace the occurrences in the `JSON` string before deserializing it. – emerson.marini Oct 07 '14 at 15:17
  • @MelanciaUK, this is what I'm doing now. I am wondering if there is a better solution. – AlexanderM Oct 07 '14 at 15:20
  • Decorate all your `Model` classes with this attribute: `[JsonObject(IsReference = false)]` – emerson.marini Oct 07 '14 at 15:31
  • 1
    This `JSON` string was serialized with the `PreserveReferencesHandling = PreserveReferencesHandling.Objects` set, that's why you have `$` prefixing these properties. – emerson.marini Oct 07 '14 at 15:35
  • @MelanciaUK looks like you're right. I will confirm with those guys if they are really sending us back array of arrays of objects. Didn't expected that they will use such serialization method for publicly facing API that in theory could be consumed by something else than .net. Anyway, thanks – AlexanderM Oct 07 '14 at 16:17

0 Answers0