Esempio n. 1
0
        public static void WriteDictionary <T>(this ITreeWriter writer, IReadOnlyDictionary <int, T> dictionary) where T : ITreeSerializable
        {
            if (dictionary == null)
            {
                writer.WriteNull(); return;
            }

            writer.WriteStartArray();

            int[] keys = dictionary.Keys.ToArray();
            writer.WriteBlockArray(keys);

            writer.WriteStartArray();

            for (int i = 0; i < keys.Length; ++i)
            {
                dictionary[keys[i]].Write(writer);
            }

            writer.WriteEndArray();

            writer.WriteEndArray();
        }
Esempio n. 2
0
        public static void WriteList <T>(this ITreeWriter writer, IReadOnlyList <T> list) where T : ITreeSerializable
        {
            if (list == null)
            {
                writer.WriteNull(); return;
            }

            writer.WriteStartArray();

            foreach (T item in list)
            {
                item.Write(writer);
            }

            writer.WriteEndArray();
        }
Esempio n. 3
0
        private static void TestIntegers(TreeFormat format)
        {
            TreeSerializationSettings settings = new TreeSerializationSettings()
            {
                LeaveStreamOpen = true
            };

            using (MemoryStream stream = new MemoryStream())
            {
                using (ITreeWriter writer = Writer(format, stream, settings))
                {
                    writer.WriteStartArray();

                    for (int i = -1; i < 300; i += 7)
                    {
                        writer.WriteValue(i);
                    }

                    writer.WriteEndArray();
                }

                long bytesWritten = stream.Position;
                stream.Seek(0, SeekOrigin.Begin);

                using (ITreeReader reader = Reader(format, stream, settings))
                {
                    Assert.Equal(TreeToken.StartArray, reader.TokenType);

                    for (int i = -1; i < 300; i += 7)
                    {
                        Assert.True(reader.Read());
                        Assert.Equal(TreeToken.Integer, reader.TokenType);
                        Assert.Equal(i, reader.ReadAsInt32());
                    }

                    Assert.True(reader.Read());
                    Assert.Equal(TreeToken.EndArray, reader.TokenType);
                    Assert.False(reader.Read());
                }

                Assert.Equal(bytesWritten, stream.Position);
            }
        }
Esempio n. 4
0
 /// <summary>WriteEndArray(...) followed by WriteEndElement(...).</summary>
 public static void WriteEndArrayElement(this ITreeWriter obj, string elementName)
 {
     obj.WriteEndArray();
     obj.WriteEndElement(elementName);
 }