/// <summary> /// Writes the underlying data to stream in JSON format. /// Does not include the Key value in JSON object. /// </summary> /// <param name="stream"></param> public void WriteAsJSON(TextWriter writer) { object data = Data; if (null == data) { writer.Write("{}"); return; } if (data is IJsonSerializable) { /* * using (MemoryStream tmpStream = new MemoryStream()) * { * StreamWriter tmpWriter = new StreamWriter(tmpStream); * * ((IJsonSerializable)data).WriteAsJSON(tmpWriter); * tmpWriter.Flush(); * tmpStream.Seek(0, SeekOrigin.Begin); * StreamReader tmpReader = new StreamReader(tmpStream); * string json = tmpReader.ReadToEnd(); * if (string.IsNullOrEmpty(json)) * { * writer.Write("{}"); //empty object * } * else * { * writer.Write(json); * } * * } * */ ((IJsonSerializable)data).WriteAsJSON(writer); return; } else if (data is string) { writer.Write(JsonData.JSSafeQuote((string)data)); } else if (data is IDictionary <string, object> || data is IDictionary <string, string> ) { DictionaryJsonSerializer ser = new DictionaryJsonSerializer(data as Dictionary <string, object>); ser.AllowReflectiveSerialization = AllowReflectiveJsonSerialization; ser.WriteAsJSON(writer); } else if (data is IEnumerable) { EnumerableJsonSerializer ser = new EnumerableJsonSerializer(data as IEnumerable); ser.WriteAsJSON(writer); } else { if (AllowReflectiveJsonSerialization) { writer.Write(BaseJsonSerializer.ExpensiveGetJsonDataAsString(data, AllowReflectiveJsonSerialization)); } else { writer.Write(JsonData.JSSafeQuote(data.ToString())); } } }