/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
+ public void WriteAsProperty(string propertyName, Utf8JsonWriter writer)
+ => WriteAsProperty(propertyName.AsSpan(), writer);
+
+ /// <summary>
+ /// Write the element into the provided writer as a named object property.
+ /// </summary>
+ /// <param name="propertyName">The name for this value within the JSON object.</param>
+ /// <param name="writer">The writer.</param>
+ /// <exception cref="InvalidOperationException">
+ /// This value's <see cref="Type"/> is <see cref="JsonValueType.Undefined"/>.
+ /// </exception>
+ /// <exception cref="ObjectDisposedException">
+ /// The parent <see cref="JsonDocument"/> has been disposed.
+ /// </exception>
public void WriteAsProperty(ReadOnlySpan<char> propertyName, Utf8JsonWriter writer)
{
CheckValidInstance();
[Theory]
[InlineData(false)]
[InlineData(true)]
+ public static void WriteNullStringAsProperty(bool indented)
+ {
+ WritePropertyValueBothForms(
+ indented,
+ null,
+ "\"\"",
+ @"{
+ """": """"
+}",
+ "{\"\":\"\"}");
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
public static void WriteNumberAsProperty(bool indented)
{
WritePropertyValueBothForms(
{
foreach (JsonElement val in root.EnumerateArray())
{
+ val.WriteAsProperty(CharLabel, writer);
val.WriteAsProperty(CharLabel.AsSpan(), writer);
val.WriteAsProperty(byteUtf8, writer);
}
writer.Flush();
AssertContents(
- "\"char\":null,\"byte\":null," +
- "\"char\":false,\"byte\":false," +
- "\"char\":true,\"byte\":true," +
- "\"char\":\"hi\",\"byte\":\"hi\"," +
- "\"char\":5,\"byte\":5," +
- "\"char\":{},\"byte\":{}," +
- "\"char\":[],\"byte\":[]",
+ "\"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
{
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>(
{
WritePropertyValue(
indented,
+ propertyName,
+ jsonIn,
+ expectedIndent,
+ expectedMinimal);
+
+ WritePropertyValue(
+ indented,
propertyName.AsSpan(),
jsonIn,
expectedIndent,
WritePropertyValue(
indented,
- Encoding.UTF8.GetBytes(propertyName),
+ Encoding.UTF8.GetBytes(propertyName ?? ""),
jsonIn,
expectedIndent,
expectedMinimal);
private static void WritePropertyValue(
bool indented,
+ string propertyName,
+ string jsonIn,
+ string expectedIndent,
+ string expectedMinimal)
+ {
+ var buffer = new ArrayBufferWriter<byte>(1024);
+ string temp = $" [ {jsonIn} ]";
+ using (JsonDocument doc = JsonDocument.Parse(temp, s_readerOptions))
+ {
+ JsonElement target = doc.RootElement[0];
+
+ var options = new JsonWriterOptions
+ {
+ Indented = indented,
+ };
+
+ var writer = new Utf8JsonWriter(buffer, options);
+
+ writer.WriteStartObject();
+ target.WriteAsProperty(propertyName, writer);
+ writer.WriteEndObject();
+ writer.Flush();
+
+ if (indented && s_replaceNewlines)
+ {
+ AssertContents(
+ expectedIndent.Replace(CompiledNewline, Environment.NewLine),
+ buffer);
+ }
+
+ AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
+ }
+ }
+
+ private static void WritePropertyValue(
+ bool indented,
ReadOnlySpan<char> propertyName,
string jsonIn,
string expectedIndent,