public void Serialize(ref JsonWriter writer, T[] value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            writer.WriteBeginArray();
            var formatter = formatterResolver.GetFormatterWithVerify <T>();

            if (value.Length != 0)
            {
                formatter.Serialize(ref writer, value[0], formatterResolver);
            }
            for (int i = 1; i < value.Length; i++)
            {
                writer.WriteValueSeparator();
                formatter.Serialize(ref writer, value[i], formatterResolver);
            }
            writer.WriteEndArray();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Serialize to stream(write async) with specified resolver.
        /// </summary>
        public static async System.Threading.Tasks.Task SerializeAsync <T>(Stream stream, T value, IJsonFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = DefaultResolver;
            }

            var buf = MemoryPool.Rent();

            try
            {
                var writer    = new JsonWriter(buf);
                var formatter = resolver.GetFormatterWithVerify <T>();
                formatter.Serialize(ref writer, value, resolver);
                var buffer = writer.GetBuffer();
                await stream.WriteAsync(buffer.Array, buffer.Offset, buffer.Count).ConfigureAwait(false);
            }
            finally
            {
                MemoryPool.Return(buf);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Serialize to binary with specified resolver. Get the raw memory pool byte[]. The result can not share across thread and can not hold, so use quickly.
        /// </summary>
        public static ArraySegment <byte> SerializeUnsafe <T>(T value, IJsonFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = DefaultResolver;
            }

            var buffer = MemoryPool.Rent();

            try
            {
                var writer    = new JsonWriter(buffer);
                var formatter = resolver.GetFormatterWithVerify <T>();
                formatter.Serialize(ref writer, value, resolver);
                var arraySegment = writer.GetBuffer();
                return(new ArraySegment <byte>(BinaryUtil.ToArray(ref arraySegment)));
            }
            finally
            {
                MemoryPool.Return(buffer);
            }
        }
Ejemplo n.º 4
0
        public void Serialize(JsonWriter writer, IList value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            var formatter = formatterResolver.GetFormatterWithVerify<object>();

            writer.WriteBeginArray();
            if (value.Count != 0)
            {
                formatter.Serialize(writer, value[0], formatterResolver);
            }
            for (int i = 1; i < value.Count; i++)
            {
                writer.WriteValueSeparator();
                formatter.Serialize(writer, value[i], formatterResolver);
            }
            writer.WriteEndArray();
        }
Ejemplo n.º 5
0
        public void Serialize(JsonWriter writer, ArraySegment<T> value, IJsonFormatterResolver formatterResolver)
        {
            if (value.Array == null) { writer.WriteNull(); return; }

            var array = value.Array;
            var offset = value.Offset;
            var count = value.Count;

            writer.WriteBeginArray();
            var formatter = formatterResolver.GetFormatterWithVerify<T>();
            if (count != 0)
            {
                formatter.Serialize(writer, value.Array[offset], formatterResolver);
            }

            for (int i = 1; i < count; i++)
            {
                writer.WriteValueSeparator();
                formatter.Serialize(writer, array[offset + i], formatterResolver);
            }
            writer.WriteEndArray();
        }
Ejemplo n.º 6
0
        public Tuple <T1> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(null);
            }


            T1 item1 = default(T1);


            var count = 0;

            reader.ReadIsBeginObjectWithVerify();
            while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref count))
            {
                var keyString = reader.ReadPropertyNameSegmentRaw();
                int key;
#if NETSTANDARD
                dictionary.TryGetValue(keyString, out key);
#else
                dictionary.TryGetValueSafe(keyString, out key);
#endif

                switch (key)
                {
                case 0:
                    item1 = formatterResolver.GetFormatterWithVerify <T1>().Deserialize(ref reader, formatterResolver);
                    break;

                default:
                    reader.ReadNextBlock();
                    break;
                }
            }

            return(new Tuple <T1>(item1));
        }
Ejemplo n.º 7
0
        public void DeserializeTo(ref List <T> value, ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                // null, do nothing
                return;
            }

            var count     = 0;
            var formatter = formatterResolver.GetFormatterWithVerify <T>();
            var list      = value; // use the reference

            if (deserializeToBehaviour == CollectionDeserializeToBehaviour.OverwriteReplace)
            {
                list.Clear(); // clear before add.
            }

            reader.ReadIsBeginArrayWithVerify();
            while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref count))
            {
                list.Add(formatter.Deserialize(ref reader, formatterResolver));
            }
        }
Ejemplo n.º 8
0
        public void Serialize(ref JsonWriter writer, TCollection value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
            }
            else
            {
                writer.WriteBeginArray();
                var formatter = formatterResolver.GetFormatterWithVerify <TElement>();

                // Unity's foreach struct enumerator causes boxing so iterate manually.
                var e = GetSourceEnumerator(value);
                try
                {
                    var isFirst = true;
                    while (e.MoveNext())
                    {
                        if (isFirst)
                        {
                            isFirst = false;
                        }
                        else
                        {
                            writer.WriteValueSeparator();
                        }
                        formatter.Serialize(ref writer, e.Current, formatterResolver);
                    }
                }
                finally
                {
                    e.Dispose();
                }

                writer.WriteEndArray();
            }
        }
Ejemplo n.º 9
0
        public T[] Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(null);
            }

            var count     = 0;
            var formatter = formatterResolver.GetFormatterWithVerify <T>();

            var workingArea = arrayPool.Rent();

            try
            {
                var array = workingArea;
                reader.ReadIsBeginArrayWithVerify();
                while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref count))
                {
                    if (array.Length < count)
                    {
                        // ReSharper disable once RedundantTypeArgumentsOfMethod
                        Array.Resize <T>(ref array, array.Length * 2);
                    }

                    array[count - 1] = formatter.Deserialize(ref reader, formatterResolver);
                }

                var result = new T[count];
                Array.Copy(array, result, count);
                Array.Clear(workingArea, 0, Math.Min(count, workingArea.Length));
                return(result);
            }
            finally
            {
                arrayPool.Return(workingArea);
            }
        }
        public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(default(T));
            }
            else
            {
                var valueFormatter = formatterResolver.GetFormatterWithVerify <object>();

                reader.ReadIsBeginObjectWithVerify();

                var dict = new T();
                var i    = 0;
                while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref i))
                {
                    var key   = reader.ReadPropertyName();
                    var value = valueFormatter.Deserialize(ref reader, formatterResolver);
                    dict.Add(key, value);
                }

                return(dict);
            }
        }
Ejemplo n.º 11
0
        public ArraySegment <T> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(default(ArraySegment <T>));
            }

            var count     = 0;
            var formatter = formatterResolver.GetFormatterWithVerify <T>();

            var workingArea = arrayPool.Rent(65536);

            try
            {
                var array = workingArea;
                reader.ReadIsBeginArrayWithVerify();
                while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref count))
                {
                    if (array.Length < count)
                    {
                        Array.Resize <T>(ref array, array.Length * 2);
                    }

                    array[count - 1] = formatter.Deserialize(ref reader, formatterResolver);
                }

                var result = new T[count];
                Array.Copy(array, result, count);
                Array.Clear(workingArea, 0, Math.Min(count, workingArea.Length));
                return(new ArraySegment <T>(result, 0, result.Length));
            }
            finally
            {
                arrayPool.Return(workingArea);
            }
        }
Ejemplo n.º 12
0
        public System.Collections.IDictionary Deserialize(JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(null);
            }
            else
            {
                var valueFormatter = formatterResolver.GetFormatterWithVerify <object>();

                reader.ReadIsBeginObjectWithVerify();

                var dict = new Dictionary <object, object>();
                var i    = 0;
                while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref i))
                {
                    var key   = reader.ReadPropertyName();
                    var value = valueFormatter.Deserialize(reader, formatterResolver);
                    dict.Add(key, value);
                }

                return(dict);
            }
        }
Ejemplo n.º 13
0
 public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
 {
     formatterResolver.GetFormatterWithVerify <string>()
     .Serialize(ref writer, value.ToString(), formatterResolver);
 }
Ejemplo n.º 14
0
 public void Serialize(ref JsonWriter writer, ValueTuple <T1> value, IJsonFormatterResolver formatterResolver)
 {
     writer.WriteRaw(Cache[0]);
     formatterResolver.GetFormatterWithVerify <T1>().Serialize(ref writer, value.Item1, formatterResolver);
     writer.WriteEndObject();
 }
        public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            // TODO 7.0 mutator is not used, should it?
            var mutator        = formatterResolver.GetConnectionSettings().DefaultFieldNameInferrer;
            var keyFormatter   = formatterResolver.GetFormatterWithVerify <TKey>() as IObjectPropertyNameFormatter <TKey>;
            var valueFormatter = formatterResolver.GetFormatterWithVerify <TValue>();

            writer.WriteBeginObject();

            var e = GetSourceEnumerator(value);

            try
            {
                if (keyFormatter != null)
                {
                    if (e.MoveNext())
                    {
                        var item = e.Current;
                        keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
                        writer.WriteNameSeparator();
                        valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                    }
                    else
                    {
                        goto END;
                    }

                    while (e.MoveNext())
                    {
                        writer.WriteValueSeparator();
                        var item = e.Current;
                        keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
                        writer.WriteNameSeparator();
                        valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                    }
                }
                else
                {
                    if (e.MoveNext())
                    {
                        var item = e.Current;
                        writer.WriteString(item.Key.ToString());
                        writer.WriteNameSeparator();
                        valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                    }
                    else
                    {
                        goto END;
                    }

                    while (e.MoveNext())
                    {
                        writer.WriteValueSeparator();
                        var item = e.Current;
                        writer.WriteString(item.Key.ToString());
                        writer.WriteNameSeparator();
                        valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                    }
                }
            }
            finally
            {
                e.Dispose();
            }

END:
            writer.WriteEndObject();
        }
Ejemplo n.º 16
0
 public void Serialize(ref JsonWriter writer, HogeHogeHoge value, IJsonFormatterResolver formatterResolver)
 {
     formatterResolver.GetFormatterWithVerify <IEnumerable <int> >().Serialize(ref writer, value, formatterResolver);
 }
Ejemplo n.º 17
0
        public static void WriteInnerSettings(ref JsonWriter writer, ContainerSetting value, IJsonFormatterResolver formatterResolver)
        {
            foreach (var inner in value.InnerSettings)
            {
                dynamic genericFormatter;

                if (inner is HardCodedSetting)
                {
                    genericFormatter = formatterResolver.GetFormatterWithVerify <StringSetting>();
                }
                else if (inner is HexadecimalSetting)
                {
                    new TypedFormatter <uint>().Serialize(ref writer, (HexadecimalSetting)inner, formatterResolver);
                    continue;
                }
                else if (inner is UnsignedNumberArraySetting)
                {
                    var fmt = formatterResolver.GetFormatterWithVerify <UnsignedNumberArraySetting>();
                    fmt.Serialize(ref writer, (UnsignedNumberArraySetting)inner, formatterResolver);
                    continue;
                }
                else if (inner is SignedNumberArraySetting)
                {
                    var fmt = formatterResolver.GetFormatterWithVerify <SignedNumberArraySetting>();
                    fmt.Serialize(ref writer, (SignedNumberArraySetting)inner, formatterResolver);
                    continue;
                }
                else if (inner is StringArraySetting)
                {
                    var fmt = formatterResolver.GetFormatterWithVerify <StringArraySetting>();
                    fmt.Serialize(ref writer, (StringArraySetting)inner, formatterResolver);
                    continue;
                }
                else if (inner is RegexSetting)
                {
                    new StringSettingFormatter().Serialize(ref writer, (RegexSetting)inner, formatterResolver);
                    continue;
                }
                else if (inner is BoolSetting)
                {
                    new BoolSettingFormatter().Serialize(ref writer, (BoolSetting)inner, formatterResolver);
                    continue;
                }
                else
                {
                    genericFormatter = formatterResolver.GetFormatterDynamic(inner.GetType());
                }

                if (inner is StringSetting typed)
                {
                    genericFormatter.Serialize(ref writer, typed, formatterResolver);
                }
                else if (inner is NumberSetting num)
                {
                    genericFormatter.Serialize(ref writer, num, formatterResolver);
                }
                else if (inner is UnsignedNumberSetting unum)
                {
                    genericFormatter.Serialize(ref writer, unum, formatterResolver);
                }
                else if (inner is AdditiveContainerSetting adt)
                {
                    genericFormatter.Serialize(ref writer, adt, formatterResolver);
                }
                else if (inner is MultipleAdditiveContainerSetting mlt)
                {
                    genericFormatter.Serialize(ref writer, mlt, formatterResolver);
                }
                else if (inner is SettingsTemplate tmp)
                {
                    genericFormatter.Serialize(ref writer, tmp, formatterResolver);
                }
                else if (inner is ContainerSetting cnt)
                {
                    genericFormatter.Serialize(ref writer, cnt, formatterResolver);
                }
                else if (inner is EnumSetting en)
                {
                    genericFormatter.Serialize(ref writer, en, formatterResolver);
                }
                else
                {
                    genericFormatter.Serialize(ref writer, inner, formatterResolver);
                }
            }
        }
Ejemplo n.º 18
0
 public void Serialize(ref JsonWriter writer, ValueTask <T> value, IJsonFormatterResolver formatterResolver)
 {
     // value.Result -> wait...!
     formatterResolver.GetFormatterWithVerify <T>().Serialize(ref writer, value.Result, formatterResolver);
 }
Ejemplo n.º 19
0
        public ValueTask <T> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            var v = formatterResolver.GetFormatterWithVerify <T>().Deserialize(ref reader, formatterResolver);

            return(new ValueTask <T>(v));
        }
Ejemplo n.º 20
0
 public static void SerializeUint(ref JsonWriter writer, uint value, IJsonFormatterResolver formatterResolver, string format)
 {
     formatterResolver.GetFormatterWithVerify <string>().Serialize(ref writer, value.ToString(format), formatterResolver);
 }
Ejemplo n.º 21
0
        public void Serialize(ref JsonWriter writer, ExpandoObject value, IJsonFormatterResolver formatterResolver)
        {
            var formatter = formatterResolver.GetFormatterWithVerify <IDictionary <string, object> >();

            formatter.Serialize(ref writer, (IDictionary <string, object>)value, formatterResolver);
        }
Ejemplo n.º 22
0
 public static uint DeserializeUint(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
 {
     return(uint.Parse(formatterResolver.GetFormatterWithVerify <string>().Deserialize(ref reader, formatterResolver), NumberStyles.HexNumber));
 }
 public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
 {
     return(formatterResolver.GetFormatterWithVerify <T>().Deserialize(ref reader, formatterResolver));
 }
        public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
            }
            else
            {
                var keyFormatter   = formatterResolver.GetFormatterWithVerify <TKey>() as IObjectPropertyNameFormatter <TKey>;
                var valueFormatter = formatterResolver.GetFormatterWithVerify <TValue>();

                writer.WriteBeginObject();

                var e = GetSourceEnumerator(value);
                try
                {
                    var written = 0;
                    if (keyFormatter != null)
                    {
                        if (e.MoveNext())
                        {
                            var item = e.Current;

                            if (!SkipValue(item.Value))
                            {
                                keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
                                writer.WriteNameSeparator();
                                valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                                written++;
                            }
                        }
                        else
                        {
                            goto END;
                        }

                        while (e.MoveNext())
                        {
                            var item = e.Current;
                            if (!SkipValue(item.Value))
                            {
                                if (written > 0)
                                {
                                    writer.WriteValueSeparator();
                                }
                                keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
                                writer.WriteNameSeparator();
                                valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                                written++;
                            }
                        }
                    }
                    else
                    {
                        if (e.MoveNext())
                        {
                            var item = e.Current;
                            if (!SkipValue(item.Value))
                            {
                                written++;
                                writer.WriteString(item.Key.ToString());
                                writer.WriteNameSeparator();
                                valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                            }
                        }
                        else
                        {
                            goto END;
                        }

                        while (e.MoveNext())
                        {
                            var item = e.Current;
                            if (!SkipValue(item.Value))
                            {
                                if (written > 0)
                                {
                                    writer.WriteValueSeparator();
                                }

                                writer.WriteString(item.Key.ToString());
                                writer.WriteNameSeparator();
                                valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                                written++;
                            }
                        }
                    }
                }
                finally
                {
                    e.Dispose();
                }

END:
                writer.WriteEndObject();
            }
        }
Ejemplo n.º 25
0
 public Utf8JsonFormatter(IJsonFormatterResolver jsonFormatterResolver)
 {
     this.jsonFormatterResolver = jsonFormatterResolver;
     jsonFormatter = jsonFormatterResolver.GetFormatterWithVerify <T>();
 }
        public T[,,,] Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(null);
            }

            var buffer    = new ArrayBuffer <ArrayBuffer <ArrayBuffer <ArrayBuffer <T> > > >(4);
            var formatter = formatterResolver.GetFormatterWithVerify <T>();

            var guessInnerLength3 = 0;
            var guessInnerLength2 = 0;
            var guessInnerLength  = 0;
            var outerCount        = 0;

            reader.ReadIsBeginArrayWithVerify();
            while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref outerCount))
            {
                var innerArray = new ArrayBuffer <ArrayBuffer <ArrayBuffer <T> > >(guessInnerLength == 0 ? 4 : guessInnerLength);
                var innerCount = 0;
                reader.ReadIsBeginArrayWithVerify();
                while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref innerCount))
                {
                    var innerArray2 = new ArrayBuffer <ArrayBuffer <T> >(guessInnerLength2 == 0 ? 4 : guessInnerLength2);
                    var innerCount2 = 0;
                    reader.ReadIsBeginArrayWithVerify();
                    while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref innerCount2))
                    {
                        var innerArray3 = new ArrayBuffer <T>(guessInnerLength3 == 0 ? 4 : guessInnerLength3);
                        var innerCount3 = 0;
                        reader.ReadIsBeginArrayWithVerify();
                        while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref innerCount3))
                        {
                            innerArray3.Add(formatter.Deserialize(ref reader, formatterResolver));
                        }

                        guessInnerLength3 = innerArray3.Size;
                        innerArray2.Add(innerArray3);
                    }

                    guessInnerLength2 = innerArray2.Size;
                    innerArray.Add(innerArray2);
                }

                guessInnerLength = innerArray.Size;
                buffer.Add(innerArray);
            }

            var t = new T[buffer.Size, guessInnerLength, guessInnerLength2, guessInnerLength3];

            for (var i = 0; i < buffer.Size; i++)
            {
                for (var j = 0; j < guessInnerLength; j++)
                {
                    for (var k = 0; k < guessInnerLength2; k++)
                    {
                        for (var l = 0; l < guessInnerLength3; l++)
                        {
                            t[i, j, k, l] = buffer.Buffer[i].Buffer[j].Buffer[k].Buffer[l];
                        }
                    }
                }
            }

            return(t);
        }
Ejemplo n.º 27
0
 public void Serialize(ref JsonWriter writer, CustomObject value, IJsonFormatterResolver formatterResolver)
 {
     formatterResolver.GetFormatterWithVerify <string>().Serialize(ref writer, value.internalId, formatterResolver);
 }
Ejemplo n.º 28
0
        public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }
            else
            {
                var keyFormatter   = formatterResolver.GetFormatterWithVerify <TKey>() as IObjectPropertyNameFormatter <TKey>;
                var valueFormatter = formatterResolver.GetFormatterWithVerify <TValue>();

                writer.WriteBeginObject();

                var e = GetSourceEnumerator(value);
                try
                {
                    if (keyFormatter != null)
                    {
                        if (e.MoveNext())
                        {
                            var item = e.Current;
                            keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
                            writer.WriteNameSeparator();
                            valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                        }
                        else
                        {
                            goto END;
                        }

                        while (e.MoveNext())
                        {
                            writer.WriteValueSeparator();
                            var item = e.Current;
                            keyFormatter.SerializeToPropertyName(ref writer, item.Key, formatterResolver);
                            writer.WriteNameSeparator();
                            valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                        }
                    }
                    else
                    {
                        if (e.MoveNext())
                        {
                            var item = e.Current;
                            // slow, but it works. now any object can be a dictionary key.
                            writer.WriteString(JsonSerializer.ToJsonString(item.Key, true));
                            writer.WriteNameSeparator();
                            valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                        }
                        else
                        {
                            goto END;
                        }

                        while (e.MoveNext())
                        {
                            writer.WriteValueSeparator();
                            var item = e.Current;
                            // slow, but it works. now any object can be a dictionary key.
                            writer.WriteString(JsonSerializer.ToJsonString(item.Key, true));
                            writer.WriteNameSeparator();
                            valueFormatter.Serialize(ref writer, item.Value, formatterResolver);
                        }
                    }
                }
                finally
                {
                    e.Dispose();
                }

END:
                writer.WriteEndObject();
            }
        }