RestSharp Serialize JSON in camelCase -
i trying post json in camelcase, , have followed instructions here:
https://github.com/restsharp/restsharp/wiki/deserialization#overriding-jsonserializationstrategy
public class camelcaseserializerstrategy : pocojsonserializerstrategy { protected override string mapclrmembernametojsonfieldname(string clrpropertyname) { return char.tolower(clrpropertyname[0]) + clrpropertyname.substring(1); } }
then creating new client code:
var client = new restclient(_baseurl); simplejson.currentjsonserializerstrategy = new camelcaseserializerstrategy();
still, when making request, serializer not activated. restsharp documentation on place , largely incorrect. looking @ source (restrequest.addbody), doesn't serializerstrategy used @ all.
i looking way make change @ client level, or somewhere doesn't require modifying each request.
i've seen this blog - , maybe that's way. seems huge step restsharp if can change serialization strategies @ request level.
i faced exact same problem. fortunately, managed solve following instructions presented here.
basically, each request, have set jsonserializer
newtonsoftjsonserializer
. example:
var request = new restrequest(); request.jsonserializer = new newtonsoftjsonserializer();
the source newtonsoftjsonserializer
below:
public newtonsoftjsonserializer() { contenttype = "application/json"; _serializer = new jsonserializer { missingmemberhandling = missingmemberhandling.ignore, nullvaluehandling = nullvaluehandling.include, defaultvaluehandling = defaultvaluehandling.include }; } public newtonsoftjsonserializer(jsonserializer serializer) { contenttype = "application/json"; _serializer = serializer; } public string serialize(object obj) { using (var stringwriter = new stringwriter()) { using (var jsontextwriter = new jsontextwriter(stringwriter)) { jsontextwriter.formatting = formatting.indented; jsontextwriter.quotechar = '"'; _serializer.serialize(jsontextwriter, obj); var result = stringwriter.tostring(); return result; } } } public string dateformat { get; set; } public string rootelement { get; set; } public string namespace { get; set; } public string contenttype { get; set; } }
hope solves problem!
Comments
Post a Comment