c# - Serialize JSON in alphabetical order -


here's situation, first generate 2 json objects of each class of model. 1 json current model version , other previous version of it. job compare each json find difference between 2 models.

the problems is, attributes of objects not serialized in same order 1 json another.

is there way 2 serialize object attribute in alphabetical order, can compare both of string ?

thanks !

if want 1 time compare on 2 json strings there's resource on web

http://tlrobinson.net/projects/javascript-fun/jsondiff

in json.net there deepequals available on jtoken so.

var json1 = jtoken.parse(jsonstring1); var json2 = jtoken.parse(jsonstring2); var jsonequal = jtoken.deepequals(json1, json2); 

if want you're asking order serialization see post.

https://stackoverflow.com/a/11309106/1181408

update - show order serialization works fine.

public class orderedcontractresolver : defaultcontractresolver {     protected override system.collections.generic.ilist<jsonproperty> createproperties(system.type type, memberserialization memberserialization)     {         return base.createproperties(type, memberserialization).orderby(p => p.propertyname).tolist();     } }  public class jsonserializationtest1 {     public string test1 { get; set; }     public string mytest2 { get; set; }     public string test2 { get; set; }     public string test10 { get; set; } }  public class jsonserializationtest2 {     public string test10 { get; set; }     public string test1 { get; set; }     public string mytest2 { get; set; }     public string test2 { get; set; } }  [testmethod]     public void testjsonorder()     {         var test1 = new jsonserializationtest1 { test1 = "abc", mytest2 = "efg", test10 = "hij", test2 = "zyx" };         var test2 = new jsonserializationtest2 { test1 = "abc", test10 = "hij", test2 = "zyx", mytest2 = "efg" };          var test1json = jsonconvert.serializeobject(test1);         var test2json = jsonconvert.serializeobject(test2);          assert.arenotequal(test1json, test2json);          var settings = new jsonserializersettings()         {             contractresolver = new orderedcontractresolver()         };          var json1 = jsonconvert.serializeobject(test1, formatting.indented, settings);         var json2 = jsonconvert.serializeobject(test2, formatting.indented, settings);          assert.areequal(json1, json2);      } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -