Пример #1
0
        static void ToJsonCore(MsgPackStream stream, StringBuilder builder)
        {
            var code = stream.Peek();
            var type = StreamReader.GetType(stream);

            switch (type)
            {
            case FormatType.Integer:
                builder.Append(FormatCode.IsSignedInteger(code)
                        ? StreamReader.ReadInt64(stream).ToString(CultureInfo.InvariantCulture)
                        : StreamReader.ReadUInt64(stream).ToString(CultureInfo.InvariantCulture));
                break;

            case FormatType.Boolean:
                builder.Append(StreamReader.ReadBool(stream) ? "true" : "false");
                break;

            case FormatType.Float:
                builder.Append(code == FormatCode.Float32
                        ? StreamReader.ReadSingle(stream).ToString(CultureInfo.InvariantCulture)
                        : StreamReader.ReadDouble(stream).ToString(CultureInfo.InvariantCulture));
                break;

            case FormatType.String:
                WriteJsonString(StreamReader.ReadString(stream), builder);
                break;

            case FormatType.Binary:
                builder.Append("\"" + System.Convert.ToBase64String(StreamReader.ReadBytes(stream).ToArray()) + "\"");
                break;

            case FormatType.Array:
            {
                var length = StreamReader.ReadArrayHeader(stream);

                builder.Append("[");
                for (int i = 0; i < length; i++)
                {
                    ToJsonCore(stream, builder);
                    if (i != length - 1)
                    {
                        builder.Append(",");
                    }
                }
                builder.Append("]");
                return;
            }

            case FormatType.Map:
            {
                var length = StreamReader.ReadMapHeader(stream);
                builder.Append("{");
                for (int i = 0; i < length; i++)
                {
                    var keyType = StreamReader.GetType(stream);
                    if (keyType == FormatType.String || keyType == FormatType.Binary)
                    {
                        ToJsonCore(stream, builder);
                    }
                    else
                    {
                        builder.Append("\"");
                        ToJsonCore(stream, builder);
                        builder.Append("\"");
                    }

                    builder.Append(":");
                    ToJsonCore(stream, builder);

                    if (i != length - 1)
                    {
                        builder.Append(",");
                    }
                }
                builder.Append("}");

                return;
            }

            case FormatType.Extension:
                var header = StreamReader.ReadExtensionHeader(stream);
                if (header.TypeCode == DateTimeFormatter.TypeCode)
                {
                    var dt = DateTimeFormatter.Unpack(stream, header.Length);
                    builder.Append("\"");
                    builder.Append(dt.ToString("o", CultureInfo.InvariantCulture));
                    builder.Append("\"");
                }
                else
                {
                    builder.Append("[");
                    builder.Append(header.TypeCode);
                    builder.Append(",");
                    builder.Append("\"");
                    builder.Append(System.Convert.ToBase64String(stream.ReadBytes(header.Length).ToArray()));
                    builder.Append("\"");
                    builder.Append("]");
                }
                break;

            default:
                builder.Append("null");
                StreamReader.Skip(stream);
                break;
            }
        }