/// <summary>
 /// From Json String to MessagePack binary
 /// </summary>
 public virtual void ConvertFromJson(TextReader reader, ref MessagePackWriter writer)
 {
     using (var jr = new TinyJsonReader(reader, false))
     {
         FromJsonCore(jr, ref writer);
     }
 }
Beispiel #2
0
 /// <summary>From Json String to MessagePack binary</summary>
 public static byte[] FromJson(TextReader reader)
 {
     using (var jr = new TinyJsonReader(reader, false))
     {
         var idx    = 0;
         var writer = new MessagePackWriter(true);
         FromJsonCore(jr, ref writer, ref idx);
         return(writer.ToArray(idx));
     }
 }
Beispiel #3
0
 /// <summary>return buffer is from memory pool, be careful to use.</summary>
 internal static IOwnedBuffer <byte> FromJsonUnsafe(TextReader reader)
 {
     using (var jr = new TinyJsonReader(reader, false))
     {
         var idx    = 0;
         var writer = new MessagePackWriter(true);
         FromJsonCore(jr, ref writer, ref idx);
         return(writer.ToOwnedBuffer(idx));
     }
 }
Beispiel #4
0
        /// <summary>
        /// return buffer is from memory pool, be careful to use.
        /// </summary>
        internal static ArraySegment <byte> FromJsonUnsafe(TextReader reader)
        {
            var offset = 0;

            byte[] binary = InternalMemoryPool.GetBuffer();  // from memory pool.
            using (var jr = new TinyJsonReader(reader, false))
            {
                FromJsonCore(jr, ref binary, ref offset);
            }
            return(new ArraySegment <byte>(binary, 0, offset));
        }
        /// <summary>
        /// From Json String to MessagePack binary
        /// </summary>
        public static byte[] FromJson(TextReader reader)
        {
            var offset = 0;

            byte[] binary = null;
            using (var jr = new TinyJsonReader(reader, false))
            {
                FromJsonCore(jr, ref binary, ref offset);
            }
            MessagePackBinary.FastResize(ref binary, offset);
            return(binary);
        }
        static uint FromJsonCore(TinyJsonReader jr, ref byte[] binary, ref int offset)
        {
            uint count = 0;

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

                case TinyJsonToken.StartObject:
                {
                    var startOffset = offset;
                    offset += 5;
                    var mapCount = FromJsonCore(jr, ref binary, ref offset);
                    mapCount = mapCount / 2;         // remove propertyname string count.
                    MessagePackBinary.WriteMapHeaderForceMap32Block(ref binary, startOffset, mapCount);
                    count++;
                    break;
                }

                case TinyJsonToken.EndObject:
                    return(count);    // break

                case TinyJsonToken.StartArray:
                {
                    var startOffset = offset;
                    offset += 5;
                    var arrayCount = FromJsonCore(jr, ref binary, ref offset);
                    MessagePackBinary.WriteArrayHeaderForceArray32Block(ref binary, startOffset, arrayCount);
                    count++;
                    break;
                }

                case TinyJsonToken.EndArray:
                    return(count);    // break

                case TinyJsonToken.Number:
                    var v = jr.Value;
                    if (v is double)
                    {
                        offset += MessagePackBinary.WriteDouble(ref binary, offset, (double)v);
                    }
                    else if (v is long)
                    {
                        offset += MessagePackBinary.WriteInt64(ref binary, offset, (long)v);
                    }
                    else if (v is ulong)
                    {
                        offset += MessagePackBinary.WriteUInt64(ref binary, offset, (ulong)v);
                    }
                    else if (v is decimal)
                    {
                        offset += DecimalFormatter.Instance.Serialize(ref binary, offset, (decimal)v, null);
                    }
                    count++;
                    break;

                case TinyJsonToken.String:
                    offset += MessagePackBinary.WriteString(ref binary, offset, (string)jr.Value);
                    count++;
                    break;

                case TinyJsonToken.True:
                    offset += MessagePackBinary.WriteBoolean(ref binary, offset, true);
                    count++;
                    break;

                case TinyJsonToken.False:
                    offset += MessagePackBinary.WriteBoolean(ref binary, offset, false);
                    count++;
                    break;

                case TinyJsonToken.Null:
                    offset += MessagePackBinary.WriteNil(ref binary, offset);
                    count++;
                    break;

                default:
                    break;
                }
            }
            return(count);
        }
        private static uint FromJsonCore(TinyJsonReader jr, ref MessagePackWriter writer)
        {
            uint count = 0;

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

                case TinyJsonToken.StartObject:
                    // Set up a scratch area to serialize the collection since we don't know its length yet, which must be written first.
                    using (var scratch = new Sequence <byte>())
                    {
                        var scratchWriter = writer.Clone(scratch);
                        var mapCount      = FromJsonCore(jr, ref scratchWriter);
                        scratchWriter.Flush();

                        mapCount = mapCount / 2;     // remove propertyname string count.
                        writer.WriteMapHeader(mapCount);
                        writer.WriteRaw(scratch.AsReadOnlySequence);
                    }

                    count++;
                    break;

                case TinyJsonToken.EndObject:
                    return(count);    // break

                case TinyJsonToken.StartArray:
                    // Set up a scratch area to serialize the collection since we don't know its length yet, which must be written first.
                    using (var scratch = new Sequence <byte>())
                    {
                        var scratchWriter = writer.Clone(scratch);
                        var arrayCount    = FromJsonCore(jr, ref scratchWriter);
                        scratchWriter.Flush();

                        writer.WriteArrayHeader(arrayCount);
                        writer.WriteRaw(scratch.AsReadOnlySequence);
                    }

                    count++;
                    break;

                case TinyJsonToken.EndArray:
                    return(count);    // break

                case TinyJsonToken.Number:
                    var v = jr.ValueType;
                    if (v == ValueType.Double)
                    {
                        writer.Write(jr.DoubleValue);
                    }
                    if (v == ValueType.Float)
                    {
                        writer.Write(jr.FloatValue);
                    }
                    else if (v == ValueType.Long)
                    {
                        writer.Write(jr.LongValue);
                    }
                    else if (v == ValueType.ULong)
                    {
                        writer.Write(jr.ULongValue);
                    }
                    else if (v == ValueType.Decimal)
                    {
                        DecimalFormatter.Instance.Serialize(ref writer, jr.DecimalValue, null);
                    }
                    count++;
                    break;

                case TinyJsonToken.String:
                    writer.Write(jr.StringValue);
                    count++;
                    break;

                case TinyJsonToken.True:
                    writer.Write(true);
                    count++;
                    break;

                case TinyJsonToken.False:
                    writer.Write(false);
                    count++;
                    break;

                case TinyJsonToken.Null:
                    writer.WriteNil();
                    count++;
                    break;

                default:
                    break;
                }
            }
            return(count);
        }
Beispiel #8
0
        static uint FromJsonCore(TinyJsonReader jr, ref MessagePackWriter writer, ref int idx)
        {
            uint count = 0;

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

                case TinyJsonToken.StartObject:
                {
                    var startOffset = idx;
                    idx += 5;
                    var mapCount = FromJsonCore(jr, ref writer, ref idx);
                    mapCount = mapCount / 2;         // remove propertyname string count.
                    MessagePackBinary.WriteMapHeaderForceMap32Block(ref writer.PinnableAddress, startOffset, mapCount);
                    count++;
                    break;
                }

                case TinyJsonToken.EndObject:
                    return(count);    // break

                case TinyJsonToken.StartArray:
                {
                    var startOffset = idx;
                    idx += 5;
                    var arrayCount = FromJsonCore(jr, ref writer, ref idx);
                    MessagePackBinary.WriteArrayHeaderForceArray32Block(ref writer.PinnableAddress, startOffset, arrayCount);
                    count++;
                    break;
                }

                case TinyJsonToken.EndArray:
                    return(count);    // break

                case TinyJsonToken.Number:
                    var v = jr.ValueType;
                    if (v == ValueType.Double)
                    {
                        writer.WriteDouble(jr.DoubleValue, ref idx);
                    }
                    else if (v == ValueType.Long)
                    {
                        writer.WriteInt64(jr.LongValue, ref idx);
                    }
                    else if (v == ValueType.ULong)
                    {
                        writer.WriteUInt64(jr.ULongValue, ref idx);
                    }
                    else if (v == ValueType.Decimal)
                    {
                        DecimalFormatter.Instance.Serialize(ref writer, ref idx, jr.DecimalValue, null);
                    }
                    count++;
                    break;

                case TinyJsonToken.String:
                    writer.WriteString(jr.StringValue, ref idx);
                    count++;
                    break;

                case TinyJsonToken.True:
                    writer.WriteBoolean(true, ref idx);
                    count++;
                    break;

                case TinyJsonToken.False:
                    writer.WriteBoolean(false, ref idx);
                    count++;
                    break;

                case TinyJsonToken.Null:
                    writer.WriteNil(ref idx);
                    count++;
                    break;

                default: break;
                }
            }
            return(count);
        }