c# - How can I serialize and deserialize a type with a string member that contains "raw" JSON, without escaping the JSON in the process -


i want deserialize json object don't want deserialize nested json, nested of nested json should convert json list (please check "my expected output" clear idea) ...

// suppose have below json data, here have nested json "address" entity

string jsonemployees = "{"employees": [{"empid":1, "empname":"abc", "address":[{"addressid":1, "address":"something"},{"addressid":2, "address":"anything"}]}, {"empid":2, "empname":"xyz", "address":[{"addressid":1, "address":"something"},{"addressid":2, "address":"anything"}]}] }"  public class employee {     public int empid { get; set; }     public string empname { get; set; }     // **note** : i'm not using list<address> data type address, instead of want list of address in json string     public string address { get; set; } }  public class rootobject {     public list<employee> employees { get; set; } }  var employees = jsonconvert.deserializeobject<rootobject>(jsonemployees); 

// expected output

employees[0].empid = 1; employees[0].empname = "abc"; employees[0].address = "[{"addressid":1, "address":"something"},{"addressid":2, "address":"anything"}]";  employees[1].empid = 2; employees[1].empname = "xyz"; employees[1].address = "[{"addressid":1, "address":"something"},{"addressid":2, "address":"anything"}]"; 

please suggest me best way solve issue ...

your question is, how can serialize , deserialize type string member contains "raw" json, without escaping json in process?

this can done via custom jsonconverter reads , writes raw json using jsonwriter.writerawvalue() , jraw.create():

public class rawconverter : jsonconverter {     public override bool canconvert(type objecttype)     {         throw new notimplementedexception();     }      public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)     {         if (reader.tokentype == jsontoken.null)             return null;         var raw = jraw.create(reader);         return raw.tostring();     }      public override void writejson(jsonwriter writer, object value, jsonserializer serializer)     {         var s = (string)value;         writer.writerawvalue(s);     } } 

then apply type follows:

public class employee {     public int empid { get; set; }     public string empname { get; set; }     // **note** : i'm not using list<address> data type address, instead of want list of address in json string     [jsonconverter(typeof(rawconverter))]     public string address { get; set; } }  public class rootobject {     public list<employee> employees { get; set; } } 

sample fiddle.

note raw json string must represent valid json. if not, json created unreadable. if want guarantee json literal valid, can keep json in parsed state internally:

public class employee {     public int empid { get; set; }     public string empname { get; set; }      [jsonproperty("address")]     jtoken addresstoken { get; set; }      [jsonignore]     public string address     {                 {             if (addresstoken == null)                 return null;             return addresstoken.tostring(formatting.indented); // or formatting.none if prefer         }         set         {             if (value == null)                 addresstoken = null;             else                 // throw exception if value not valid json.                 addresstoken = jtoken.parse(value);         }     } } 

a converter not needed implementation.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -