Dathan
Dathan

Reputation: 7446

Custom mapping with Json.NET

I'm trying to map JSON that looks like

"ids": {
    "id": {
        "@value":"6763754764235874140"
    }
}

And I'd like to map it onto a couple of classes that look like

class Property
{
    public Ids Ids { get; set; }
}

class Ids
{
    public string Id { get; set; }
}

So basically I want to stuff the value of ids/id/@value from the JSON document into Ids.Id in the class architecture. From browsing the documentation, I thought I could use something like

[JsonProperty(ItemConverterType=typeof(IdConverter))]
public string Id { get; set; }

and provide a custom JsonConverter subclass named IdConverter. When I do, though, my IdConverter.ReadJson never gets called. What am I doing wrong?

Upvotes: 7

Views: 10260

Answers (1)

Dathan
Dathan

Reputation: 7446

Looks like the answer was that ItemConverterType is for converting items in an array. Double-annotating the property with JsonProperty and JsonConverter attributes works:

[JsonProperty(PropertyName="id")]
[JsonConverter(typeof(IdConverter))]
public string Id { get; set; }

Upvotes: 16

Related Questions