public static void SerializeCustom(MessageWrapper2 mw2, TextWriter tw) { JsonSerializerSettings s = new JsonSerializerSettings(); s.TypeNameHandling = TypeNameHandling.Objects; using (JsonWriter jw = new JsonTextWriter(tw)) { jw.WriteStartObject(); jw.WritePropertyName("Headers"); jw.WriteRawValue(JsonConvert.SerializeObject(mw2.Headers)); jw.WritePropertyName("Body"); jw.WriteRawValue(JsonConvert.SerializeObject(mw2.Body, Formatting.Indented, s)); jw.WriteEndObject(); jw.Flush(); } }
public static void Test1() { JsonSerializerSettings s = new JsonSerializerSettings(); s.TypeNameHandling = TypeNameHandling.Objects; JsonSerializer ser = JsonSerializer.Create(s); MessageWrapper2 mw2 = new MessageWrapper2(); mw2.Headers["DeliverAt"] = "yesterday!"; mw2.Body = new TestMessage1 { Id = 893239 }; StringWriter sw = new StringWriter(); ser.Serialize(sw, mw2); log.Info("Serialized: {0}", sw.ToString()); MessageWrapper2 mw3 = (MessageWrapper2)ser.Deserialize(new JsonTextReader(new StringReader(sw.ToString()))); sw = new StringWriter(); SerializeCustom(mw2, sw); log.Info("Serialized custom: {0}", sw.ToString()); mw3 = (MessageWrapper2)ser.Deserialize(new JsonTextReader(new StringReader(sw.ToString()))); }