private static void WriteListFallback(WriterState writer, ICollection list)
 {
     writer.Write((byte)SerializedType.List);
     writer.Write(list.Count);
     foreach (var item in list)
     {
         WriteValue(writer, item);
     }
 }
Beispiel #2
0
 public void Write(WriterState writer, IEnumerable list)
 {
     foreach (var item in (IEnumerable <T?>)list)
     {
         writer.Write((byte)EnumerableType.HasItem);
         Serializer.WriteValue(writer, item);
     }
     writer.Write((byte)EnumerableType.End);
 }
        private static void WriteEnumerableFallback(WriterState writer, IEnumerable enumerable)
        {
            foreach (var item in enumerable)
            {
                writer.Write((byte)EnumerableType.HasItem);
                WriteValue(writer, item);
            }

            writer.Write((byte)EnumerableType.End);
        }
 private static void WriteDictionary(WriterState writer, IDictionary dictionary)
 {
     writer.Write((byte)SerializedType.Dictionary);
     writer.Write(dictionary.Count);
     foreach (var(key, value) in dictionary.Keys.Zip(dictionary.Values))
     {
         WriteValue(writer, key);
         WriteValue(writer, value);
     }
 }
        private static void WriteObject(WriterState writer, object val)
        {
            writer.Write((byte)SerializedType.Object);

            foreach (var getter in GetterHandler.GetGetterHandlers(val.GetType()))
            {
                getter.Handle(writer, val);
            }

            writer.Write((byte)EnumerableType.End);
        }
        private static void WriteList(WriterState writer, ICollection list)
        {
            writer.Write((byte)SerializedType.List);
            writer.Write(list.Count);
            if (GenericWriter.WriteList(writer, list))
            {
                return;
            }

            // fallback to non-generic version
            WriteListFallback(writer, list);
        }
Beispiel #7
0
            public static void WriteAll <TKey, TValue>(WriterState writer, object obj)
            {
                var dictionary = (IDictionary <TKey, TValue>)obj;

                writer.Write((byte)SerializedType.Dictionary);
                writer.Write(dictionary.Count);
                foreach (var(key, value) in dictionary)
                {
                    Serializer.WriteValue(writer, key);
                    Serializer.WriteValue(writer, value);
                }
            }
        private static void WriteDictionaryAsObject(WriterState writer, IDictionary <string, object> dictionary)
        {
            writer.Write((byte)SerializedType.Object);

            foreach (var(key, value) in dictionary.Keys.Zip(dictionary.Values))
            {
                writer.Write((byte)EnumerableType.HasItem);
                writer.WriteString(key);
                WriteValue(writer, value);
            }

            writer.Write((byte)EnumerableType.End);
        }
Beispiel #9
0
        private static void WriteNull(WriterState writer, string memberName)
        {
            if (writer.SkipNullValues)
            {
                return;
            }

            WriteMemberName(writer, memberName);
            writer.Write((byte)SerializedType.Null);
        }
        public static void WriteValue(WriterState writer, object val)
        {
            if (val == null)
            {
                writer.Write((byte)SerializedType.Null);
                return;
            }

            WriteNonNullValue(writer, val);
        }
        private static void WriteEnumerable(WriterState writer, IEnumerable enumerable)
        {
            // ReSharper disable once PossibleMultipleEnumeration
            if (GenericWriter.WriteEnumerable(writer, enumerable))
            {
                return;
            }

            // fallback to non-generic version (we are not enumerating twice)
            writer.Write((byte)SerializedType.Enumerable);
            // ReSharper disable once PossibleMultipleEnumeration
            WriteEnumerableFallback(writer, enumerable);
        }
        private static void WriteObject <T>(WriterState writer, T val)
        {
            var type = val.GetType();

            if (writer.CustomObjectIdentifierProviders == null || !writer.CustomObjectIdentifierProviders.TryGetValue(typeof(T), out var customObjectIdentifierProvider))
            {
                writer.Write((byte)SerializedType.Object);
            }
            else
            {
                writer.Write((byte)SerializedType.CustomObject);
                WriteValue(writer, customObjectIdentifierProvider.GetIdentifier(type));
            }

            var getters = type == typeof(T) ? GetterHandler.GetterHandlers <T> .Getters : GetterHandler.GetGetterHandlers(type);

            foreach (var getter in getters)
            {
                getter.Handle(writer, val);
            }

            writer.Write((byte)EnumerableType.End);
        }
Beispiel #13
0
 private static void WriteMemberName(WriterState writer, string memberName)
 {
     writer.Write((byte)EnumerableType.HasItem);
     writer.WriteString(memberName);
 }