コード例 #1
0
        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());
        }
コード例 #2
0
        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());
        }