public static void SerializeToWriter <T>(T value, TextWriter writer) { if (value == null) { return; } if (typeof(T) == typeof(string)) { writer.Write(value); } else if (typeof(T) == typeof(object)) { SerializeToWriter(value, value.GetType(), writer); } else if (typeof(T).IsAbstract() || typeof(T).IsInterface()) { JsState.IsWritingDynamic = false; SerializeToWriter(value, value.GetType(), writer); JsState.IsWritingDynamic = true; } else { JsvWriter <T> .WriteRootObject(writer, value); } }
public static string SerializeToString <T>(T value) { if (value == null || value is Delegate) { return(null); } if (typeof(T) == typeof(object)) { return(SerializeToString(value, value.GetType())); } if (typeof(T).IsAbstract() || typeof(T).IsInterface()) { JsState.IsWritingDynamic = true; var result = SerializeToString(value, value.GetType()); JsState.IsWritingDynamic = false; return(result); } using (var sb = StringBuilderWriter.Create()) { JsvWriter <T> .WriteRootObject(sb.Writer, value); return(sb.ToString()); } }
public static void SerializeToStream <T>(T value, Stream stream) { if (value == null) { return; } if (typeof(T) == typeof(object)) { SerializeToStream(value, value.GetType(), stream); } else if (typeof(T).IsAbstract() || typeof(T).IsInterface()) { JsState.IsWritingDynamic = false; SerializeToStream(value, value.GetType(), stream); JsState.IsWritingDynamic = true; } else { var writer = new StreamWriter(stream, UTF8Encoding); JsvWriter <T> .WriteRootObject(writer, value); writer.Flush(); } }