c# - How to pass dictionary as part of the payload in POST request in Advanced Rest Client -
i trying use advanced rest client chrome extension testing webapi locally , pass dictionary part of payload post request trying make. while debugging found out dictionary though passing in json format doesn't deserialize correctly , count dictionary remains zero.
payload example :
{ "dictionaryforevaluationtelemetries" :{ "b":"{\"counter\":\"500\"}"} }
this simple code object part of dictionary
[datacontract] class class1 { private int counter; [datamember] public int counter { { return counter; } } public void increment() { interlocked.increment(ref counter); } } [datacontract] public class telemetrypayload { [datamember] public concurrentdictionary<string, class1> dictionaryforevaluationtelemetries { get; set; } } [httppost] public void logevaluationtelemetry() { // internally below method : jsonserializer<t>.deserialize(request.content.readasstringasync().result); var telemetrypayload = request.getobjectfrombody<telemetrypayload>(); }
json deserialization done system.runtime.serialization.json
using system.io; using system.runtime.serialization.json; using system.text; public static class jsonserializer<t> t : class { public static t deserialize(string jsonobject) { byte[] array = encoding.utf8.getbytes(jsonobject); using (memorystream ms = new memorystream(array)) { datacontractjsonserializer jsonserializer = new datacontractjsonserializer(typeof(t)); return (t)jsonserializer.readobject(ms); } }
as far can tell json passing doesn't have object map class1. json should this:
{ "dictionaryforevaluationtelemetries" : {"b": {"counter": 500}} }
where dictionaryforevaluationtelemetries.b map class1.
or, depending on request.getobjectfrombody<> method does, more this:
{"counter" : 500}
Comments
Post a Comment