Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 306
Dathan Avatar asked Aug 07 '12 10:08

Dathan


1 Answers

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; }
like image 159
Dathan Avatar answered Sep 18 '22 20:09

Dathan