/// <summary> /// 对象转换JSON字符串 /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> private void serialize <valueType>(ref valueType value) { if (Config.GetLoopObject == null || Config.SetLoopObject == null) { if (Config.GetLoopObject != null) { Warning = SerializeWarning.LessSetLoop; } else if (Config.SetLoopObject != null) { Warning = SerializeWarning.LessGetLoop; } else { Warning = SerializeWarning.None; } if (checkLoopDepth != Config.CheckLoopDepth || isLoopObject) { if (Config.CheckLoopDepth <= 0) { checkLoopDepth = 0; if (forefather == null) { forefather = new object[sizeof(int)]; } } else { checkLoopDepth = Config.CheckLoopDepth; } } isLoopObject = false; } else { if (!isLoopObject) { isLoopObject = true; if (objectIndexs == null) { objectIndexs = ReusableDictionary <ObjectReference> .Create <string>(); } } Warning = SerializeWarning.None; checkLoopDepth = Config.CheckLoopDepth <= 0 ? SerializeConfig.DefaultCheckLoopDepth : Config.CheckLoopDepth; } TypeSerializer <valueType> .Serialize(this, value); }
/// <summary> /// 序列化 /// </summary> /// <typeparam name="valueType">目标数据类型</typeparam> /// <param name="value">数据对象</param> /// <param name="stream"></param> internal void SerializeTcpServer <valueType>(valueType value, UnmanagedStream stream) { if (value == null) { stream.Write(NullValue); } else { //CurrentMemberMap = MemberMap = null; //Warning = SerializeWarning.None; if (isReferenceMember == TypeSerializer <valueType> .IsReferenceMember) { if (points != null) { points.Clear(); } } else if (isReferenceMember) { isReferenceMember = false; } else { isReferenceMember = true; if (points == null) { points = ReusableDictionary <ObjectReference> .Create <int>(); } else { points.Clear(); } } //streamStartIndex = Stream.OffsetLength; streamStartIndex = stream.ByteSize; isReferenceArray = true; Config.UnsafeWriteHeader(stream); Stream.From(stream); try { //Warning = SerializeWarning.None; TypeSerializer <valueType> .SerializeTcpServer(this, ref value); } finally { stream.From(Stream); } //Stream.Write(Stream.OffsetLength - streamStartIndex); stream.Write(stream.ByteSize - streamStartIndex); } }
/// <summary> /// GIF文件写入器 /// </summary> /// <param name="filename">文件名称</param> /// <param name="width">素数宽度</param> /// <param name="height">素数高度</param> /// <param name="globalColors">全局颜色列表</param> /// <param name="backgroundColorIndex">背景颜色在全局颜色列表中的索引,如果没有全局颜色列表,该值没有意义</param> /// <param name="log">日志处理</param> public unsafe FileWriter(string filename, short width, short height, Color[] globalColors = null, byte backgroundColorIndex = 0, ILog log = null) { this.log = log ?? AutoCSer.Log.Pub.Log; if (width <= 0) { throw new IndexOutOfRangeException("width[" + width.toString() + "] <= 0"); } if (height <= 0) { throw new IndexOutOfRangeException("height[" + height.toString() + "] <= 0"); } if (string.IsNullOrEmpty(filename)) { throw new ArgumentNullException("filename is null or empty"); } fileStream = new FileStream(filename, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1, FileOptions.WriteThrough); Width = width; Height = height; globalColorCount = globalColors.length(); int pixel = 0; if (globalColorCount != 0) { if (globalColorCount < 256) { pixel = ((uint)globalColorCount).bits() - 1; if (globalColorCount != (1 << pixel)) { ++pixel; } } else { globalColorCount = 256; pixel = 7; } pixel |= 0x80; } fixed(byte *bufferFixed = fileBuffer) { *(ulong *)bufferFixed = fileVersion | ((ulong)width << 48); *(uint *)(bufferFixed + 8) = (uint)(int)height | (globalColorCount == 0 ? 0 : ((uint)pixel << 16)) | (7 << (16 + 4)) | (backgroundColorIndex >= globalColorCount ? 0 : ((uint)backgroundColorIndex << 24)); bufferIndex = 13; if (globalColorCount != 0) { byte *currentBuffer = bufferFixed + 13; fixed(Color *colorFixed = globalColors) { for (Color *currentColor = colorFixed, colorEnd = colorFixed + globalColorCount; currentColor != colorEnd; ++currentColor) { Color color = *currentColor; * currentBuffer++ = color.Red; * currentBuffer++ = color.Green; * currentBuffer++ = color.Blue; } } bufferIndex += 3 << (pixel ^ 0x80); } } colors = new Color[(int)Width * Height]; colorCounts = new int[colors.Length]; colorIndexs = ReusableDictionary <Color> .Create <int>(); }