コード例 #1
0
 internal static void ThrowInvalidOperationException_MessagePackType(byte code)
 {
     throw GetException();
     InvalidOperationException GetException()
     {
         return(new InvalidOperationException("Invalid MessagePackType:" + MessagePackCode.ToFormatName(code)));
     }
 }
コード例 #2
0
 private static Exception ThrowInvalidCode(byte code)
 {
     throw new InvalidOperationException(string.Format("code is invalid. code: {0} format: {1}", code, MessagePackCode.ToFormatName(code)));
 }
コード例 #3
0
 internal static void ThrowInvalidOperationException_Code(byte code)
 {
     throw GetException();
     InvalidOperationException GetException()
     {
         return(new InvalidOperationException($"code is invalid. code:{code} format:{MessagePackCode.ToFormatName(code)}"));
     }
 }
コード例 #4
0
 private static InvalidOperationException GetInvalidOperationException(byte code)
 {
     return(new InvalidOperationException($"code is invalid. code:{code} format:{MessagePackCode.ToFormatName(code)}"));
 }
コード例 #5
0
        private static void ToJsonCore(ref MessagePackReader reader, TextWriter writer)
        {
            var type = reader.NextMessagePackType;

            switch (type)
            {
            case MessagePackType.Integer:
                if (MessagePackCode.IsSignedInteger(reader.NextCode))
                {
                    writer.Write(reader.ReadInt64().ToString(CultureInfo.InvariantCulture));
                }
                else
                {
                    writer.Write(reader.ReadUInt64().ToString(CultureInfo.InvariantCulture));
                }

                break;

            case MessagePackType.Boolean:
                writer.Write(reader.ReadBoolean() ? "true" : "false");
                break;

            case MessagePackType.Float:
                if (reader.NextCode == MessagePackCode.Float32)
                {
                    writer.Write(reader.ReadSingle().ToString(CultureInfo.InvariantCulture));
                }
                else
                {
                    writer.Write(reader.ReadDouble().ToString(CultureInfo.InvariantCulture));
                }

                break;

            case MessagePackType.String:
                WriteJsonString(reader.ReadString(), writer);
                break;

            case MessagePackType.Binary:
                writer.Write("\"" + Convert.ToBase64String(reader.ReadBytes().ToArray()) + "\"");
                break;

            case MessagePackType.Array:
            {
                int length = reader.ReadArrayHeader();
                writer.Write("[");
                for (int i = 0; i < length; i++)
                {
                    ToJsonCore(ref reader, writer);

                    if (i != length - 1)
                    {
                        writer.Write(",");
                    }
                }
                writer.Write("]");
                return;
            }

            case MessagePackType.Map:
            {
                int length = reader.ReadMapHeader();
                writer.Write("{");
                for (int i = 0; i < length; i++)
                {
                    // write key
                    {
                        var keyType = reader.NextMessagePackType;
                        if (keyType == MessagePackType.String || keyType == MessagePackType.Binary)
                        {
                            ToJsonCore(ref reader, writer);
                        }
                        else
                        {
                            writer.Write("\"");
                            ToJsonCore(ref reader, writer);
                            writer.Write("\"");
                        }
                    }

                    writer.Write(":");

                    // write body
                    {
                        ToJsonCore(ref reader, writer);
                    }

                    if (i != length - 1)
                    {
                        writer.Write(",");
                    }
                }
                writer.Write("}");

                return;
            }

            case MessagePackType.Extension:
                var extHeader = reader.ReadExtensionFormatHeader();
                if (extHeader.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
                {
                    var dt = reader.ReadDateTime(extHeader);
                    writer.Write("\"");
                    writer.Write(dt.ToString("o", CultureInfo.InvariantCulture));
                    writer.Write("\"");
                }
#if !UNITY
                else if (extHeader.TypeCode == TypelessFormatter.ExtensionTypeCode)
                {
                    // prepare type name token
                    var privateBuilder             = new StringBuilder();
                    var typeNameTokenBuilder       = new StringBuilder();
                    var positionBeforeTypeNameRead = reader.Position;
                    ToJsonCore(ref reader, new StringWriter(typeNameTokenBuilder));
                    int typeNameReadSize = (int)reader.Sequence.Slice(positionBeforeTypeNameRead, reader.Position).Length;
                    if (extHeader.Length > typeNameReadSize)
                    {
                        // object map or array
                        var typeInside = reader.NextMessagePackType;
                        if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
                        {
                            privateBuilder.Append("{");
                        }

                        ToJsonCore(ref reader, new StringWriter(privateBuilder));
                        // insert type name token to start of object map or array
                        if (typeInside != MessagePackType.Array)
                        {
                            typeNameTokenBuilder.Insert(0, "\"$type\":");
                        }

                        if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
                        {
                            privateBuilder.Append("}");
                        }

                        if (privateBuilder.Length > 2)
                        {
                            typeNameTokenBuilder.Append(",");
                        }

                        privateBuilder.Insert(1, typeNameTokenBuilder.ToString());

                        writer.Write(privateBuilder.ToString());
                    }
                    else
                    {
                        writer.Write("{\"$type\":\"" + typeNameTokenBuilder.ToString() + "}");
                    }
                }
#endif
                else
                {
                    var ext = reader.ReadExtensionFormat();
                    writer.Write("[");
                    writer.Write(ext.TypeCode);
                    writer.Write(",");
                    writer.Write("\"");
                    writer.Write(Convert.ToBase64String(ext.Data.ToArray()));
                    writer.Write("\"");
                    writer.Write("]");
                }
                break;

            case MessagePackType.Nil:
                reader.Skip();
                writer.Write("null");
                break;

            default:
                throw new NotSupportedException($"code is invalid. code: {reader.NextCode} format: {MessagePackCode.ToFormatName(reader.NextCode)}");
            }
        }