public static void WritePropertyOutsideObject(bool skipValidation)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            using (var doc = JsonDocument.Parse("[ null, false, true, \"hi\", 5, {}, [] ]", s_readerOptions))
            {
                JsonElement root    = doc.RootElement;
                var         options = new JsonWriterOptions
                {
                    SkipValidation = skipValidation,
                };

                const string CharLabel = "char";
                byte[]       byteUtf8  = Encoding.UTF8.GetBytes("byte");
                var          writer    = new Utf8JsonWriter(buffer, options);

                if (skipValidation)
                {
                    foreach (JsonElement val in root.EnumerateArray())
                    {
                        val.WriteAsProperty(CharLabel, writer);
                        val.WriteAsProperty(CharLabel.AsSpan(), writer);
                        val.WriteAsProperty(byteUtf8, writer);
                    }

                    writer.Flush();

                    AssertContents(
                        "\"char\":null,\"char\":null,\"byte\":null," +
                        "\"char\":false,\"char\":false,\"byte\":false," +
                        "\"char\":true,\"char\":true,\"byte\":true," +
                        "\"char\":\"hi\",\"char\":\"hi\",\"byte\":\"hi\"," +
                        "\"char\":5,\"char\":5,\"byte\":5," +
                        "\"char\":{},\"char\":{},\"byte\":{}," +
                        "\"char\":[],\"char\":[],\"byte\":[]",
                        buffer);
                }
                else
                {
                    foreach (JsonElement val in root.EnumerateArray())
                    {
                        JsonTestHelper.AssertThrows <InvalidOperationException>(
                            ref writer,
                            (ref Utf8JsonWriter w) => val.WriteAsProperty(CharLabel, w));

                        JsonTestHelper.AssertThrows <InvalidOperationException>(
                            ref writer,
                            (ref Utf8JsonWriter w) => val.WriteAsProperty(CharLabel.AsSpan(), w));

                        JsonTestHelper.AssertThrows <InvalidOperationException>(
                            ref writer,
                            (ref Utf8JsonWriter w) => val.WriteAsProperty(byteUtf8, w));
                    }

                    writer.Flush();

                    AssertContents("", buffer);
                }
            }
        }
Exemple #2
0
        public void WritePropertyOutsideObject(bool skipValidation)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            using (var doc = JsonDocument.Parse("[ null, false, true, \"hi\", 5, {}, [] ]", s_options))
            {
                JsonElement root    = doc.RootElement;
                var         options = new JsonWriterOptions
                {
                    SkipValidation = skipValidation,
                };

                const string CharLabel = "char";
                using var writer = new Utf8JsonWriter(buffer, options);

                if (skipValidation)
                {
                    foreach (JsonElement val in root.EnumerateArray())
                    {
                        writer.WritePropertyName(CharLabel);
                        val.WriteTo(writer);
                        writer.WritePropertyName(CharLabel.AsSpan());
                        val.WriteTo(writer);
                        writer.WritePropertyName("byte" u8);
                        val.WriteTo(writer);
                        writer.WritePropertyName(JsonEncodedText.Encode(CharLabel));
                        val.WriteTo(writer);
                    }

                    writer.Flush();

                    JsonTestHelper.AssertContents(
                        "\"char\":null,\"char\":null,\"byte\":null,\"char\":null," +
                        "\"char\":false,\"char\":false,\"byte\":false,\"char\":false," +
                        "\"char\":true,\"char\":true,\"byte\":true,\"char\":true," +
                        "\"char\":\"hi\",\"char\":\"hi\",\"byte\":\"hi\",\"char\":\"hi\"," +
                        "\"char\":5,\"char\":5,\"byte\":5,\"char\":5," +
                        "\"char\":{},\"char\":{},\"byte\":{},\"char\":{}," +
                        "\"char\":[],\"char\":[],\"byte\":[],\"char\":[]",
                        buffer);
                }
                else
                {
                    Assert.Throws <InvalidOperationException>(() => writer.WritePropertyName(CharLabel));
                    Assert.Throws <InvalidOperationException>(() => writer.WritePropertyName(CharLabel.AsSpan()));
                    Assert.Throws <InvalidOperationException>(() => writer.WritePropertyName("byte" u8));
                    Assert.Throws <InvalidOperationException>(() => writer.WritePropertyName(JsonEncodedText.Encode(CharLabel)));

                    writer.Flush();

                    JsonTestHelper.AssertContents("", buffer);
                }
            }
        }