/// <summary> /// 对象转换 JSON 字符串(单线程模式) /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> /// <param name="config">配置参数</param> /// <returns>JSON 字符串</returns> public static SerializeResult SerializeThread <valueType>(valueType value, SerializeConfig config = null) { Serializer jsonSerializer = ThreadStatic.Get().Serializer; try { string json = jsonSerializer.serializeThread <valueType>(ref value, config); return(new SerializeResult { Json = json, Warning = jsonSerializer.Warning }); } finally { jsonSerializer.freeThread(); } }
/// <summary> /// 对象转换 JSON 字符串 /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> /// <param name="config">配置参数</param> /// <returns>JSON 字符串</returns> public static SerializeResult Serialize <valueType>(valueType value, SerializeConfig config = null) { Serializer jsonSerializer = YieldPool.Default.Pop() ?? new Serializer(); try { string json = jsonSerializer.serialize <valueType>(ref value, config); return(new SerializeResult { Json = json, Warning = jsonSerializer.Warning }); } finally { jsonSerializer.Free(); } }
internal void Free() { Config = null; if (isLoopObject) { objectIndexs.Clear(); } else if (forefatherCount != 0) { System.Array.Clear(forefather, 0, forefatherCount); forefatherCount = 0; } YieldPool.Default.PushNotNull(this); }
/// <summary> /// 对象转换JSON字符串 /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> /// <param name="config">配置参数</param> /// <returns>Json字符串</returns> private string serialize <valueType>(ref valueType value, SerializeConfig config) { Config = config ?? DefaultConfig; byte *buffer = AutoCSer.UnmanagedPool.Default.Get(); try { CharStream.Reset((byte *)buffer, AutoCSer.UnmanagedPool.DefaultSize); using (CharStream) { serialize(ref value); return(CharStream.ToString()); } } finally { AutoCSer.UnmanagedPool.Default.Push(buffer); } }
private void freeThread() { Config = null; #if AutoCSer if (isLoopObject) { objectIndexs.Clear(); } else #endif if (forefatherCount != 0) { System.Array.Clear(forefather, 0, forefatherCount); forefatherCount = 0; } }
/// <summary> /// 对象转换 JSON 字符串 /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> /// <param name="onSerializeStream">序列化以后的数据流处理事件</param> /// <param name="config">配置参数</param> public static SerializeWarning Serialize <valueType>(ref valueType value, Action <CharStream> onSerializeStream, SerializeConfig config = null) { if (onSerializeStream == null) { throw new ArgumentNullException(); } Serializer jsonSerializer = YieldPool.Default.Pop() ?? new Serializer(); try { jsonSerializer.serialize <valueType>(ref value, onSerializeStream, config); return(jsonSerializer.Warning); } finally { jsonSerializer.Free(); } }
/// <summary> /// 对象转换JSON字符串 /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> /// <param name="onSerializeStream">序列化以后的数据流处理事件</param> /// <param name="config">配置参数</param> private void serialize <valueType>(ref valueType value, Action <CharStream> onSerializeStream, SerializeConfig config) { Config = config ?? DefaultConfig; byte *buffer = AutoCSer.UnmanagedPool.Default.Get(); try { CharStream.Reset((byte *)buffer, AutoCSer.UnmanagedPool.DefaultSize); using (CharStream) { serialize(ref value); onSerializeStream(CharStream); } } finally { AutoCSer.UnmanagedPool.Default.Push(buffer); } }
/// <summary> /// 序列化 /// </summary> /// <typeparam name="valueType"></typeparam> /// <param name="value"></param> /// <param name="stream"></param> /// <param name="config"></param> internal static void Serialize <valueType>(valueType value, UnmanagedStream stream, SerializeConfig config) { int index = stream.AddSize(sizeof(int)); Serializer jsonSerializer = YieldPool.Default.Pop() ?? new Serializer(); try { jsonSerializer.serialize <valueType>(ref value, stream, config); } finally { jsonSerializer.Free(); } int length = stream.ByteSize - index; *(int *)(stream.Data.Byte + index - sizeof(int)) = length; if ((length & 2) != 0) { stream.Write(' '); } }
/// <summary> /// 对象转换 JSON 字符串 /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> /// <param name="jsonStream">JSON 输出缓冲区</param> /// <param name="config">配置参数</param> public static SerializeWarning Serialize <valueType>(ref valueType value, CharStream jsonStream, SerializeConfig config = null) { Serializer jsonSerializer = YieldPool.Default.Pop() ?? new Serializer(); try { jsonSerializer.serialize <valueType>(ref value, jsonStream, config); return(jsonSerializer.Warning); } finally { jsonSerializer.Free(); } }
private void serialize <valueType>(ref valueType value, UnmanagedStream stream, SerializeConfig config) { Config = config ?? DefaultConfig; CharStream.From(stream); try { serialize(ref value); } finally { stream.From(CharStream); } }
private void serialize <valueType>(ref valueType value, CharStream jsonStream, SerializeConfig config) { Config = config ?? DefaultConfig; CharStream.From(jsonStream); try { serialize(ref value); } finally { jsonStream.From(CharStream); } }
private SerializeResult serializeResult <valueType>(ref valueType value, SerializeConfig config) { return(new SerializeResult { Json = serialize(ref value, config), Warning = Warning }); }
/// <summary> /// 对象转换 JSON 字符串 /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> /// <param name="config">配置参数</param> /// <returns>JSON 序列化结果</returns> public static SerializeResult SerializeResult <valueType>(ref valueType value, SerializeConfig config = null) { Serializer jsonSerializer = YieldPool.Default.Pop() ?? new Serializer(); try { return(jsonSerializer.serializeResult <valueType>(ref value, config)); } finally { jsonSerializer.Free(); } }