public void QuoteNameAndStrings()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            JsonTextWriter writer = new JsonTextWriter(sw) { QuoteName = false };

            writer.WriteStartObject();

            writer.WritePropertyName("name");
            writer.WriteValue("value");

            writer.WriteEndObject();
            writer.Flush();

            Assert.Equal(@"{name:""value""}", sb.ToString());
        }
        public void NewLine()
        {
            MemoryStream ms = new MemoryStream();

            using (var streamWriter = new StreamWriter(ms, new UTF8Encoding(false)) { NewLine = "\n" })
            using (var jsonWriter = new JsonTextWriter(streamWriter)
            {
                CloseOutput = true,
                Indentation = 2,
                Formatting = Formatting.Indented
            })
            {
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("prop");
                jsonWriter.WriteValue(true);
                jsonWriter.WriteEndObject();
            }

            byte[] data = ms.ToArray();

            string json = Encoding.UTF8.GetString(data, 0, data.Length);

            Assert.Equal(@"{" + '\n' + @"  ""prop"": true" + '\n' + "}", json);
        }
        public void Path()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            string text = "Hello world.";
            byte[] data = Encoding.UTF8.GetBytes(text);

            using (JsonTextWriter writer = new JsonTextWriter(sw))
            {
                writer.Formatting = Formatting.Indented;

                writer.WriteStartArray();
                Assert.Equal("", writer.Path);
                writer.WriteStartObject();
                Assert.Equal("[0]", writer.Path);
                writer.WritePropertyName("Property1");
                Assert.Equal("[0].Property1", writer.Path);
                writer.WriteStartArray();
                Assert.Equal("[0].Property1", writer.Path);
                writer.WriteValue(1);
                Assert.Equal("[0].Property1[0]", writer.Path);
                writer.WriteStartArray();
                Assert.Equal("[0].Property1[1]", writer.Path);
                writer.WriteStartArray();
                Assert.Equal("[0].Property1[1][0]", writer.Path);
                writer.WriteStartArray();
                Assert.Equal("[0].Property1[1][0][0]", writer.Path);
                writer.WriteEndObject();
                Assert.Equal("[0]", writer.Path);
                writer.WriteStartObject();
                Assert.Equal("[1]", writer.Path);
                writer.WritePropertyName("Property2");
                Assert.Equal("[1].Property2", writer.Path);
                writer.WriteStartConstructor("Constructor1");
                Assert.Equal("[1].Property2", writer.Path);
                writer.WriteNull();
                Assert.Equal("[1].Property2[0]", writer.Path);
                writer.WriteStartArray();
                Assert.Equal("[1].Property2[1]", writer.Path);
                writer.WriteValue(1);
                Assert.Equal("[1].Property2[1][0]", writer.Path);
                writer.WriteEnd();
                Assert.Equal("[1].Property2[1]", writer.Path);
                writer.WriteEndObject();
                Assert.Equal("[1]", writer.Path);
                writer.WriteEndArray();
                Assert.Equal("", writer.Path);
            }

            StringAssert.Equal(@"[
  {
    ""Property1"": [
      1,
      [
        [
          []
        ]
      ]
    ]
  },
  {
    ""Property2"": new Constructor1(
      null,
      [
        1
      ]
    )
  }
]", sb.ToString());
        }
        public void Indentation()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.Indented;
                jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol;

                Assert.Equal(Formatting.Indented, jsonWriter.Formatting);

                jsonWriter.Indentation = 5;
                Assert.Equal(5, jsonWriter.Indentation);
                jsonWriter.IndentChar = '_';
                Assert.Equal('_', jsonWriter.IndentChar);
                jsonWriter.QuoteName = true;
                Assert.Equal(true, jsonWriter.QuoteName);
                jsonWriter.QuoteChar = '\'';
                Assert.Equal('\'', jsonWriter.QuoteChar);

                jsonWriter.WriteStartObject();

                jsonWriter.WritePropertyName("propertyName");
                jsonWriter.WriteValue(double.NaN);

                jsonWriter.IndentChar = '?';
                Assert.Equal('?', jsonWriter.IndentChar);
                jsonWriter.Indentation = 6;
                Assert.Equal(6, jsonWriter.Indentation);

                jsonWriter.WritePropertyName("prop2");
                jsonWriter.WriteValue(123);

                jsonWriter.WriteEndObject();
            }

            string expected = @"{
_____'propertyName': NaN,
??????'prop2': 123
}";
            string result = sb.ToString();

            StringAssert.Equal(expected, result);
        }
        public void WriteObjectNestedInConstructor()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("con");

                jsonWriter.WriteStartConstructor("Ext.data.JsonStore");
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("aa");
                jsonWriter.WriteValue("aa");
                jsonWriter.WriteEndObject();
                jsonWriter.WriteEndConstructor();

                jsonWriter.WriteEndObject();
            }

            Assert.Equal(@"{""con"":new Ext.data.JsonStore({""aa"":""aa""})}", sb.ToString());
        }
        public void WriteRawValue()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                int i = 0;
                string rawJson = "[1,2]";

                jsonWriter.WriteStartObject();

                while (i < 3)
                {
                    jsonWriter.WritePropertyName("d" + i);
                    jsonWriter.WriteRawValue(rawJson);

                    i++;
                }

                jsonWriter.WriteEndObject();
            }

            Assert.Equal(@"{""d0"":[1,2],""d1"":[1,2],""d2"":[1,2]}", sb.ToString());
        }
        public void WriteRawInObject()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.Indented;

                jsonWriter.WriteStartObject();
                jsonWriter.WriteRaw(@"""PropertyName"":[1,2,3,4,5]");
                jsonWriter.WriteEnd();
            }

            string expected = @"{""PropertyName"":[1,2,3,4,5]}";
            string result = sb.ToString();

            Assert.Equal(expected, result);
        }
        public void State()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                Assert.Equal(WriteState.Start, jsonWriter.WriteState);

                jsonWriter.WriteStartObject();
                Assert.Equal(WriteState.Object, jsonWriter.WriteState);
                Assert.Equal("", jsonWriter.Path);

                jsonWriter.WritePropertyName("CPU");
                Assert.Equal(WriteState.Property, jsonWriter.WriteState);
                Assert.Equal("CPU", jsonWriter.Path);

                jsonWriter.WriteValue("Intel");
                Assert.Equal(WriteState.Object, jsonWriter.WriteState);
                Assert.Equal("CPU", jsonWriter.Path);

                jsonWriter.WritePropertyName("Drives");
                Assert.Equal(WriteState.Property, jsonWriter.WriteState);
                Assert.Equal("Drives", jsonWriter.Path);

                jsonWriter.WriteStartArray();
                Assert.Equal(WriteState.Array, jsonWriter.WriteState);

                jsonWriter.WriteValue("DVD read/writer");
                Assert.Equal(WriteState.Array, jsonWriter.WriteState);
                Assert.Equal("Drives[0]", jsonWriter.Path);

                jsonWriter.WriteEnd();
                Assert.Equal(WriteState.Object, jsonWriter.WriteState);
                Assert.Equal("Drives", jsonWriter.Path);

                jsonWriter.WriteEndObject();
                Assert.Equal(WriteState.Start, jsonWriter.WriteState);
                Assert.Equal("", jsonWriter.Path);
            }
        }
        public void Indenting()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.Indented;

                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("CPU");
                jsonWriter.WriteValue("Intel");
                jsonWriter.WritePropertyName("PSU");
                jsonWriter.WriteValue("500W");
                jsonWriter.WritePropertyName("Drives");
                jsonWriter.WriteStartArray();
                jsonWriter.WriteValue("DVD read/writer");
                jsonWriter.WriteComment("(broken)");
                jsonWriter.WriteValue("500 gigabyte hard drive");
                jsonWriter.WriteValue("200 gigabype hard drive");
                jsonWriter.WriteEnd();
                jsonWriter.WriteEndObject();
                Assert.Equal(WriteState.Start, jsonWriter.WriteState);
            }

            // {
            //   "CPU": "Intel",
            //   "PSU": "500W",
            //   "Drives": [
            //     "DVD read/writer"
            //     /*(broken)*/,
            //     "500 gigabyte hard drive",
            //     "200 gigabype hard drive"
            //   ]
            // }

            string expected = @"{
  ""CPU"": ""Intel"",
  ""PSU"": ""500W"",
  ""Drives"": [
    ""DVD read/writer""
    /*(broken)*/,
    ""500 gigabyte hard drive"",
    ""200 gigabype hard drive""
  ]
}";
            string result = sb.ToString();

            StringAssert.Equal(expected, result);
        }
        public void CloseWithRemainingContent()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.Indented;

                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("CPU");
                jsonWriter.WriteValue("Intel");
                jsonWriter.WritePropertyName("PSU");
                jsonWriter.WriteValue("500W");
                jsonWriter.WritePropertyName("Drives");
                jsonWriter.WriteStartArray();
                jsonWriter.WriteValue("DVD read/writer");
                jsonWriter.WriteComment("(broken)");
                jsonWriter.WriteValue("500 gigabyte hard drive");
                jsonWriter.WriteValue("200 gigabype hard drive");
                jsonWriter.Close();
            }

            string expected = @"{
  ""CPU"": ""Intel"",
  ""PSU"": ""500W"",
  ""Drives"": [
    ""DVD read/writer""
    /*(broken)*/,
    ""500 gigabyte hard drive"",
    ""200 gigabype hard drive""
  ]
}";
            string result = sb.ToString();

            StringAssert.Equal(expected, result);
        }
        public void WriteEndOnProperty()
        {
            StringWriter sw = new StringWriter();
            JsonTextWriter writer = new JsonTextWriter(sw);
            writer.QuoteChar = '\'';

            writer.WriteStartObject();
            writer.WritePropertyName("Blah");
            writer.WriteEnd();

            Assert.Equal("{'Blah':null}", sw.ToString());
        }
        public void WriteReadWrite()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw)
            {
                Formatting = Formatting.Indented
            })
            {
                jsonWriter.WriteStartArray();
                jsonWriter.WriteValue(true);

                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("integer");
                jsonWriter.WriteValue(99);
                jsonWriter.WritePropertyName("string");
                jsonWriter.WriteValue("how now brown cow?");
                jsonWriter.WritePropertyName("array");

                jsonWriter.WriteStartArray();
                for (int i = 0; i < 5; i++)
                {
                    jsonWriter.WriteValue(i);
                }

                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("decimal");
                jsonWriter.WriteValue(990.00990099m);
                jsonWriter.WriteEndObject();

                jsonWriter.WriteValue(5);
                jsonWriter.WriteEndArray();

                jsonWriter.WriteEndObject();

                jsonWriter.WriteValue("This is a string.");
                jsonWriter.WriteNull();
                jsonWriter.WriteNull();
                jsonWriter.WriteEndArray();
            }

            string json = sb.ToString();

            JsonSerializer serializer = new JsonSerializer();

            object jsonObject = serializer.Deserialize(new JsonTextReader(new StringReader(json)));

            sb = new StringBuilder();
            sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw)
            {
                Formatting = Formatting.Indented
            })
            {
                serializer.Serialize(jsonWriter, jsonObject);
            }

            Assert.Equal(json, sb.ToString());
        }