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 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 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 Culture()
        {
            StringWriter sw = new StringWriter();
            JsonTextWriter writer = new JsonTextWriter(sw);
            writer.Formatting = Formatting.Indented;
            writer.DateFormatString = "yyyy tt";
            writer.Culture = new CultureInfo("en-NZ");
            writer.QuoteChar = '\'';

            writer.WriteStartArray();

            writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc));
            writer.WriteValue(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));

            writer.WriteEnd();

            StringAssert.Equal(@"[
  '2000 a.m.',
  '2000 a.m.'
]", sw.ToString());
        }
        public void QuoteChar()
        {
            StringWriter sw = new StringWriter();
            JsonTextWriter writer = new JsonTextWriter(sw);
            writer.Formatting = Formatting.Indented;
            writer.QuoteChar = '\'';

            writer.WriteStartArray();

            writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc));
            writer.WriteValue(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));

            writer.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
            writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc));
            writer.WriteValue(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));

            writer.DateFormatString = "yyyy gg";
            writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc));
            writer.WriteValue(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));

            writer.WriteValue(new byte[] { 1, 2, 3 });
            writer.WriteValue(TimeSpan.Zero);
            writer.WriteValue(new Uri("http://www.google.com/"));
            writer.WriteValue(Guid.Empty);

            writer.WriteEnd();

            StringAssert.Equal(@"[
  '2000-01-01T01:01:01Z',
  '2000-01-01T01:01:01+00:00',
  '\/Date(946688461000)\/',
  '\/Date(946688461000+0000)\/',
  '2000 A.D.',
  '2000 A.D.',
  'AQID',
  '00:00:00',
  'http://www.google.com/',
  '00000000-0000-0000-0000-000000000000'
]", sw.ToString());
        }
        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());
        }