intercept JSON in C# to return a list of nullable boolean -
i getting error when try return list of nullable boolean json interceptor. interceptor attribute is:
[jsonconverter(typeof(nullableboollistdeserializer))] public list<bool?> exemptbenefits { get; set; }
the readjson method on interceptor is:
list<bool?> result = new list<bool?>(); (reader newtonsoft.json.linq.jtokenreader).currenttoken.tolist().foreach(item => { string value = (string)item.toobject(typeof(string)); switch (value.tolower()) { case "true": case "yes": case "1": result.add(true); break; case "false": case "no": case "0": default: result.add(false); break; } }); return result;
the json being submitted is:
{ "exemptbenefits": [ "1" ], "_apiendpoint": "/benefits/loan" }
the error getting is:
unexpected token when deserializing object: string. path 'exemptbenefits[0]', line 1, position 187.
wondering how convert list of strings (eg "1","0"."true", "false") json list (true,false,true,false) in json interceptor (actually newtonsoft.json)
if want convert list of string values list of nullable boolean values jsonconverter
class, recommend using jarray
inside converter instead of trying deal reader directly. allow simplify code while avoiding error encountered:
class nullableboollistdeserializer : jsonconverter { readonly string[] truestrings = { "true", "yes", "1" }; public override bool canconvert(type objecttype) { return objecttype == typeof(list<bool?>); } public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer) { return jarray.load(reader) .children<jvalue>() .select(jv => { string b = (string)jv; return b != null ? truestrings.contains(b.tolower()) : (bool?)null; }) .tolist(); } public override bool canwrite { { return false; } } public override void writejson(jsonwriter writer, object value, jsonserializer serializer) { throw new notimplementedexception(); } }
fiddle: https://dotnetfiddle.net/jap5w7
of course, can better that. instead of making converter handle list<bool?>
, make handle simple bool?
instead, e.g.:
class nullablebooldeserializer : jsonconverter { readonly string[] truestrings = { "true", "yes", "1" }; public override bool canconvert(type objecttype) { return objecttype == typeof(bool?); } public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer) { string b = (string)reader.value; return b != null ? truestrings.contains(b.tolower()) : (bool?)null; } public override bool canwrite { { return false; } } public override void writejson(jsonwriter writer, object value, jsonserializer serializer) { throw new notimplementedexception(); } }
then, swap out [jsonconverter]
attribute on exemptbenefits
property declaration [jsonproperty]
attribute has itemconvertertype
parameter set simplified converter. json.net handle creating list you.
[jsonproperty(itemconvertertype = typeof(nullablebooldeserializer))] public list<bool?> exemptbenefits { get; set; }
fiddle: https://dotnetfiddle.net/dp4n11
Comments
Post a Comment