Saturday, 15 March 2014

c# - Creating a collection with custom Json serializer -



c# - Creating a collection with custom Json serializer -

this code part of asp.net web api controller, have special requirement modify values in final json, before sending across ui

my current code looks following:

// web api controller public partial class viewrcontroller { [httpget] public datamaplist fetchemployeedatalist() { datamaplist returndatamaplist = new datamaplist(); homecoming (returndatamaplist); } } // type definition , details [jsonobject] [jsonconverter(typeof(datamaplistserializer))] public class datamaplist { public list<dictionary<object, object>> employeesalarymappinglist = null; public datamaplist() { employeesalarymappinglist = new list<dictionary<object, object>>(); employeesalarymappinglist.add(new dictionary<object, object>()); employeesalarymappinglist[0].add(1, 10000); employeesalarymappinglist[0].add(2, 13000); employeesalarymappinglist[0].add(3, 15000); employeesalarymappinglist.add(new dictionary<object, object>()); employeesalarymappinglist[1].add(4, 9000); employeesalarymappinglist[1].add(5, 12000); employeesalarymappinglist[1].add(6, 11000); } } // custom json serializer public class datamaplistserializer : jsonconverter { public override void writejson(jsonwriter writer, object value, jsonserializer serializer) { var employeesalarydatamaplist = value datamaplist; var employeesalarymappinglist = employeesalarydatamaplist.employeesalarymappinglist; writer.writestartobject(); foreach (var employeesalarymappingdictionary in employeesalarymappinglist) { writer.writepropertyname("ed"); writer.writestartobject(); foreach (var keyvaluepair in employeesalarymappingdictionary) { writer.writepropertyname("id-" + keyvaluepair.key); serializer.serialize(writer, keyvaluepair.value); } writer.writeendobject(); } writer.writeendobject(); } public override bool canconvert(type objecttype) { homecoming typeof(datamaplist).isassignablefrom(objecttype); } }

issue face next result:

{ "ed": { "id-1": 10000, "id-2": 13000, "id-3": 15000 }, "ed": { "id-4": 9000, "id-5": 12000, "id-6": 11000 } }

however expect next result, since serializing collection (.net list type, .net collection)

[ "ed": { "id-1": 10000, "id-2": 13000, "id-3": 15000 }, "ed": { "id-4": 9000, "id-5": 12000, "id-6": 11000 } ]

in fact current result when seek validate in json lint, shows valid json terminates part of it,so shows next valid json, that's diversion

{ "ed": { "id-4": 9000, "id-5": 12000, "id-6": 11000 } }

i have tried replacing writer.writestartobject(); , writer.writeendobject(); writer.writestartarray(); , writer.writeendarray();, before first foreach loop traverse through list in type, leads exception, doesnt help

"exceptionmessage": "token startarray in state objectstart result in invalid json object. path '

any suggestion resolve issue allow me know if need clarification

in json, name-value pairs must within object {}; cannot straight within array []. json trying create not valid, , why getting exception:

[ "ed": { "id-1": 10000, "id-2": 13000, "id-3": 15000 }, "ed": { "id-4": 9000, "id-5": 12000, "id-6": 11000 } ]

in order create valid collection, each of ed name-value pairs must wrapped in object this:

[ { "ed": { "id-1": 10000, "id-2": 13000, "id-3": 15000 } }, { "ed": { "id-4": 9000, "id-5": 12000, "id-6": 11000 } } ]

once see that, solution becomes clear: in datamaplistserializer need utilize writer.writestartarray() , writer.writeendarray() outside loop, , need add together writer.writestartobject() , writer.writeendobject() within loop.

writer.writestartarray(); foreach (var employeesalarymappingdictionary in employeesalarymappinglist) { writer.writestartobject(); writer.writepropertyname("ed"); writer.writestartobject(); foreach (var keyvaluepair in employeesalarymappingdictionary) { writer.writepropertyname("id-" + keyvaluepair.key); serializer.serialize(writer, keyvaluepair.value); } writer.writeendobject(); writer.writeendobject(); } writer.writeendarray();

fiddle: https://dotnetfiddle.net/ot4hkm

c# json serialization asp.net-web-api json.net

No comments:

Post a Comment