Пример #1
0
        public static void WriteBytes(BufferSegment value, MsgPackStream stream)
        {
            if (value.Array == null)
            {
                stream.WriteUInt8(FormatCode.Nil);
                return;
            }

            var length = value.Length;

            if (length <= byte.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Bin8);
                stream.WriteUInt8(unchecked ((byte)length));
            }
            else if (length <= ushort.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Bin16);
                stream.WriteUInt16(unchecked ((ushort)length));
            }
            else if (length < uint.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Bin32);
                stream.WriteUInt32(unchecked ((uint)length));
            }
            else
            {
                throw new MsgPackException("Max bin length exceeded");
            }

            stream.WriteBytes(value);
        }
Пример #2
0
        private static uint FromJsonCore(TinyJsonReader jr, MsgPackStream stream)
        {
            uint count = 0;

            while (jr.Read())
            {
                switch (jr.TokenType)
                {
                case TinyJsonToken.None: break;

                case TinyJsonToken.EndObject: return(count);

                case TinyJsonToken.EndArray: return(count);

                case TinyJsonToken.StartObject:
                {
                    var innerStream = new MsgPackStream(1024);
                    var mapCount    = FromJsonCore(jr, innerStream) / 2;
                    StreamWriter.WriteMapHeader(mapCount, stream);
                    stream.WriteBytes(innerStream.Output);

                    count++;
                    break;
                }

                case TinyJsonToken.StartArray:
                {
                    var innerStream = new MsgPackStream(1024);
                    var arrayCount  = FromJsonCore(jr, innerStream);
                    StreamWriter.WriteArrayHeader(arrayCount, stream);
                    stream.WriteBytes(innerStream.Output);
                    count++;
                    break;
                }

                case TinyJsonToken.Number:
                    var v = jr.ValueType;
                    if (v == ValueType.Double)
                    {
                        StreamWriter.WriteDouble(jr.DoubleValue, stream);
                    }
                    else if (v == ValueType.Long)
                    {
                        StreamWriter.WriteInt64(jr.LongValue, stream);
                    }
                    else if (v == ValueType.ULong)
                    {
                        StreamWriter.WriteUInt64(jr.ULongValue, stream);
                    }
                    else if (v == ValueType.Decimal)
                    {
                        StreamWriter.WriteString(jr.DecimalValue.ToString(CultureInfo.InvariantCulture), stream);
                    }
                    count++;
                    break;

                case TinyJsonToken.String:
                    StreamWriter.WriteString(jr.StringValue, stream);
                    count++;
                    break;

                case TinyJsonToken.True:
                    StreamWriter.WriteBool(true, stream);
                    count++;
                    break;

                case TinyJsonToken.False:
                    StreamWriter.WriteBool(false, stream);
                    count++;
                    break;

                case TinyJsonToken.Null:
                    StreamWriter.WriteNil(stream);
                    count++;
                    break;
                }
            }

            return(count);
        }