/// <summary> /// Serialize objects into JSON strings /// </summary> /// <typeparam name="T">Value type</typeparam> /// <param name="value">Value</param> /// <param name="option">Serialize option</param> /// <returns>JSON strings</returns> public static string ToJson <T>(T value, JsonSerializerOption option = null) { var handler = new JsonSerializerHandler(new System.Text.StringBuilder()) { Option = option ?? defaultSerializerOption }; Serializer.FormattingProvider <T> .Get(value, handler); return(handler.ToString()); }
/// <summary> /// Serialize Object from the StreamWriter /// </summary> /// <typeparam name="T">Value type</typeparam> /// <param name="value">Value</param> /// <param name="streamWriter">Stream</param> /// <param name="option">Serialize option</param> public static void ToJson <T>(T value, StreamWriter streamWriter, JsonSerializerOption option = null) { streamWriter.Flush();//posting set 0 bool isAutoFlush = false; if (streamWriter.AutoFlush) { isAutoFlush = true; streamWriter.AutoFlush = false; } var handler = new JsonSerializerHandler(streamWriter) { Option = option ?? defaultSerializerOption }; Serializer.FormattingProvider <T> .Get(value, handler); if (isAutoFlush) { streamWriter.AutoFlush = true; } }
/// <summary> /// 序列化时 - Model的Value的格式化器 /// Serialization time - Value formatter for Model /// </summary> /// <param name="value">需要被格式化的源元素数据,Source element data that needs to be formatted</param> /// <param name="type">值的类型,The type of the value</param> /// <param name="handler">用于提供一些配置选项,Used to provide some configuration options</param> /// <param name="isValueFormat">决定最终是否进行值格式化,Determines whether the value is ultimately formatted</param> /// <returns>格式化后的结果,Formatted results</returns> public virtual string WriteValueFormat(object value, Type type, JsonSerializerHandler handler, out bool isValueFormat) { isValueFormat = false; return(null); }