c# - Can not deserialize JSON containing $ref keys -
i have following code trying deserialize json string , library gives me error:
additional content found in json reference object. json reference object should have $ref property. path 'user.obj', line 1, position 34.
any idea wrong? (i understand complaining second $ref, don't know why.) workaround ?
void main() { var s = "{\"user\": {\"$ref\": \"123456\", \"obj\": {\"$ref\": \"123456\"}}}"; jsonconvert.deserializeobject<root>(s).dump(); } // define other methods , classes here public class root { [jsonproperty("user")] public user user { get; set; } } public class user { [jsonpropertyattribute("$ref")] public string ref { get; set; } [jsonpropertyattribute("obj")] public obj obj { get; set; } } public class obj { [jsonpropertyattribute("$ref")] public string ref { get; set; } }
json.net uses $ref
along $id
metadata preserve object references in json. when sees $ref
assumes property not part of actual json property set, internal identifier referring matching $id
somewhere else in json. since usage of $ref
different json.net expects see, throwing error.
update
in json.net version 6.0.4 , later, there setting can instruct deserializer treat these metadata properties normal properties instead of consuming them. need set metadatapropertyhandling
setting ignore
, deserialize usual.
var settings = new jsonserializersettings(); settings.metadatapropertyhandling = metadatapropertyhandling.ignore; var obj = jsonconvert.deserializeobject<formdefinitionlist>(json, settings);
prior version 6.0.4, workaround needed solve issue. if cannot upgrade lastest version of json.net, see my answer similar question possible solutions.
Comments
Post a Comment