I have some problems deserializing a json "class" in red below (yes, unfortunately it is literally named "attribute"):
{
"spatialReference": {
"wkid": 1234
},
"geometryType": "Point",
"features": [
{
"attribute": {
"husnr": "1",
"vej": {
"kode": "1111",
"navn": "road name here",
"href": "/?servicename=keys&method=vej&vejkode=1111"
},
"postdistrikt": {
"kode": "2222",
"navn": "city name here",
"href": "/?servicename=keys&method=postdistrikt&postnr=2222"
},
"kommune": {
"kode": "3333",
"navn": "municipality name here",
"href": "?servicename=keys&method=kommune&komkode=3333"
}
},
"geometry": {
"x": 715914.18,
"y": 6180299.52
}
}
]
}
The json is valid and I can see that when I call the method from a browser I get valid json back. But when I try deserialize it, then all the values are there except the "attribute" value - the value is null (and not a "attribute" object).
This is my deserializer:
public class KeysDeserializer : IDeserializer {
#region IDeserializer Members
public T Deserialize<T>( IRestResponse response ) {
var text = response.Content; if ( String.IsNullOrEmpty(text) ) text = {};
var json = new Newtonsoft.Json.JsonSerializer( );
json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
var sr = new StringReader( text );
var reader = new JsonTextReader( sr );
var result = json.Deserialize( reader, typeof( T ) );
reader.Close( );
return (T)result;
}
public string DateFormat { get; set; }
public string Namespace { get; set; }
public string RootElement { get; set; }
#endregion
}
How do I make the deserializer accept that the JsonProperty literally is called "attribute"?
I can't call te json C# class Attribute, because then my code won't compile, ie. I can't do this:
public class Feature {
[JsonProperty( "attribute" )]
public Attribute Attribute { get; set; }
// <--- compile error: 'PostAddress.Feature' does not contain a definition for 'Attribute'
// and no extension method 'Attribute' accepting a first argument of type 'PostAddress.Feature'
// could be found (are you missing a using directive or an assembly reference?)
[JsonProperty( "geometry" )]
public PointGeometry Geometry { get; set; }
}
but i can do this:
public class Feature {
[JsonProperty( "attribute" )]
public Attribute Attributes { get; set; } // compiles fine
[JsonProperty( "geometry" )]
public PointGeometry Geometry { get; set; }
}
public class PostAdresse {
[JsonProperty( "spatialReference" )]
public SpatialReference SpatialReference { get; set; }
[JsonProperty( "geometryType" )]
public string GeometryType { get; set; }
[JsonProperty( "features" )]
public IList<Feature> Features { get; set; }
}
public class Attribute {
[JsonProperty( "husnr" )]
public string Husnr { get; set; }
[JsonProperty( "vej" )]
public Vej Vej { get; set; }
[JsonProperty( "postdistrikt" )]
public Postdistrikt Postdistrikt { get; set; }
[JsonProperty( "kommune" )]
public Kommune Kommune { get; set; }
}
Has anyone seen this before, and how did they work around it?