Example #1
0
        DateTime IFormatter <DateTime> .Read(MsgPackStream stream, IContext context)
        {
            // Reference:
            // ExtensionValue value = deserialize_ext_type();
            //     struct timespec result;
            // switch(value.length) {
            //     case 4:
            //         uint32_t data32 = value.payload;
            //         result.tv_nsec = 0;
            //         result.tv_sec = data32;
            //     case 8:
            //         uint64_t data64 = value.payload;
            //         result.tv_nsec = data64 >> 34;
            //         result.tv_sec = data64 & 0x00000003ffffffffL;
            //     case 12:
            //         uint32_t data32 = value.payload;
            //         uint64_t data64 = value.payload + 4;
            //         result.tv_nsec = data32;
            //         result.tv_sec = data64;
            //     default:
            //     // error
            // }

            var code   = stream.Peek();
            var header = StreamReader.ReadExtensionHeader(stream);

            if (header.TypeCode != TypeCode)
            {
                throw new MsgPackExtensionException(header.TypeCode, TypeCode);
            }

            return(Unpack(stream, header.Length));
        }
Example #2
0
        public static bool TryReadNil(MsgPackStream stream)
        {
            if (stream.Peek() == FormatCode.Nil)
            {
                stream.Skip(1);
                return(true);
            }

            return(false);
        }
        decimal IFormatter <decimal> .Read(MsgPackStream stream, IContext context)
        {
            var code = stream.Peek();
            var type = StreamReader.GetType(stream);

            switch (type)
            {
            case FormatType.Integer:
                return(FormatCode.IsSignedInteger(code)
                        ? (decimal)context.ResolveFormatter <long>().Read(stream, context)
                        : context.ResolveFormatter <ulong>().Read(stream, context));

            case FormatType.Float:
                return(code == FormatCode.Float32
                        ? (decimal)context.ResolveFormatter <float>().Read(stream, context)
                        : (decimal)context.ResolveFormatter <double>().Read(stream, context));

            default:
                throw new MsgPackException("Invalid decimal type code: {0} ({1})", code, type);
            }
        }
Example #4
0
        public static FormatType GetType(MsgPackStream stream)
        {
            var code = stream.Peek();

            switch (FormatMask.Match(code))
            {
            case FormatMask.PositiveFixInt: return(FormatType.Integer);

            case FormatMask.NegativeFixInt: return(FormatType.Integer);

            case FormatMask.FixString: return(FormatType.String);

            case FormatMask.FixArray: return(FormatType.Array);

            case FormatMask.FixMap: return(FormatType.Map);
            }

            switch (code)
            {
            case FormatCode.Nil: return(FormatType.Nil);

            case FormatCode.NeverUsed: return(FormatType.Unknown);

            case FormatCode.False: return(FormatType.Boolean);

            case FormatCode.True: return(FormatType.Boolean);

            case FormatCode.Int8: return(FormatType.Integer);

            case FormatCode.UInt8: return(FormatType.Integer);

            case FormatCode.Int16: return(FormatType.Integer);

            case FormatCode.UInt16: return(FormatType.Integer);

            case FormatCode.Int32: return(FormatType.Integer);

            case FormatCode.UInt32: return(FormatType.Integer);

            case FormatCode.Int64: return(FormatType.Integer);

            case FormatCode.UInt64: return(FormatType.Integer);

            case FormatCode.Float32: return(FormatType.Float);

            case FormatCode.Float64: return(FormatType.Float);

            case FormatCode.Str8: return(FormatType.String);

            case FormatCode.Str16: return(FormatType.String);

            case FormatCode.Str32: return(FormatType.String);

            case FormatCode.Bin8: return(FormatType.Binary);

            case FormatCode.Bin16: return(FormatType.Binary);

            case FormatCode.Bin32: return(FormatType.Binary);

            case FormatCode.Array16: return(FormatType.Array);

            case FormatCode.Array32: return(FormatType.Array);

            case FormatCode.Map16: return(FormatType.Map);

            case FormatCode.Map32: return(FormatType.Map);

            case FormatCode.FixExt1: return(FormatType.Extension);

            case FormatCode.FixExt2: return(FormatType.Extension);

            case FormatCode.FixExt4: return(FormatType.Extension);

            case FormatCode.FixExt8: return(FormatType.Extension);

            case FormatCode.FixExt16: return(FormatType.Extension);

            case FormatCode.Ext8: return(FormatType.Extension);

            case FormatCode.Ext16: return(FormatType.Extension);

            case FormatCode.Ext32: return(FormatType.Extension);

            default: return(FormatType.Unknown);
            }
        }
Example #5
0
        public object Read(MsgPackStream stream, IContext context)
        {
            if (StreamReader.TryReadNil(stream))
            {
                return(null);
            }

            var code = stream.Peek();

            switch (StreamReader.GetType(stream))
            {
            case FormatType.Boolean:
                return(context.ResolveFormatter <bool>().Read(stream, context));

            case FormatType.Integer:
                return(FormatCode.IsSignedInteger(code)
                        ? (object)context.ResolveFormatter <long>().Read(stream, context)
                        : context.ResolveFormatter <ulong>().Read(stream, context));

            case FormatType.Float:
                return(code == FormatCode.Float32
                        ? (object)context.ResolveFormatter <float>().Read(stream, context)
                        : context.ResolveFormatter <double>().Read(stream, context));

            case FormatType.String:
                return(context.ResolveFormatter <string>().Read(stream, context));

            case FormatType.Binary:
                return(context.ResolveFormatter <byte[]>().Read(stream, context));

            case FormatType.Array:
                var count = StreamReader.ReadArrayHeader(stream);
                var array = new object[count];
                for (var i = 0; i < count; ++i)
                {
                    array[i] = Read(stream, context);
                }
                return(array);

            case FormatType.Map:
                var size = (int)StreamReader.ReadMapHeader(stream);
                var map  = new Dictionary <object, object>(size);
                for (var i = 0; i < size; ++i)
                {
                    var key   = Read(stream, context);
                    var value = Read(stream, context);
                    map[key] = value;
                }
                return(map);

            case FormatType.Extension:
                var header = StreamReader.ReadExtensionHeader(stream);
                if (header.TypeCode == DateTimeFormatter.TypeCode)
                {
                    return(DateTimeFormatter.Unpack(stream, header.Length));
                }

                return(stream.ReadBytes(header.Length).ToArray());

            default:
                throw new MsgPackException("Unsupported type code {0}", code);
            }
        }