Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add WritePropertyName standlone API on the Utf8JsonWriter. #38864

Merged
5 commits merged into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ public void WriteNumberValue(float value) { }
public void WriteNumberValue(uint value) { }
[System.CLSCompliantAttribute(false)]
public void WriteNumberValue(ulong value) { }
public void WritePropertyName(System.ReadOnlySpan<byte> utf8PropertyName) { }
public void WritePropertyName(System.ReadOnlySpan<char> propertyName) { }
public void WritePropertyName(string propertyName) { }
public void WritePropertyName(System.Text.Json.JsonEncodedText propertyName) { }
public void WriteStartArray() { }
public void WriteStartArray(System.ReadOnlySpan<byte> utf8PropertyName) { }
public void WriteStartArray(System.ReadOnlySpan<char> propertyName) { }
Expand Down
6 changes: 6 additions & 0 deletions src/System.Text.Json/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@
<data name="CannotWritePropertyWithinArray" xml:space="preserve">
<value>Cannot write a JSON property within an array or as the first JSON token. Current token type is '{0}'.</value>
</data>
<data name="CannotWritePropertyAfterProperty" xml:space="preserve">
<value>Cannot write a JSON property right after another property. A JSON value is missing.</value>
ahsonkhan marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="CannotWriteValueAfterPrimitive" xml:space="preserve">
<value>Cannot write a JSON value after a single JSON value. Current token type is '{0}'.</value>
</data>
Expand Down Expand Up @@ -243,6 +246,9 @@
<data name="MismatchedObjectArray" xml:space="preserve">
<value>'{0}' is invalid without a matching open.</value>
</data>
<data name="CannotWriteEndAfterProperty" xml:space="preserve">
<value>'{0}' is invalid right after a property name.</value>
ahsonkhan marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="ObjectDepthTooLarge" xml:space="preserve">
<value>The maximum configured depth of {0} has been exceeded. Cannot read next JSON object.</value>
</data>
Expand Down
18 changes: 16 additions & 2 deletions src/System.Text.Json/src/System/Text/Json/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,14 @@ private static string GetResourceString(ExceptionResource resource, int currentD
{
case ExceptionResource.MismatchedObjectArray:
Debug.Assert(token == JsonConstants.CloseBracket || token == JsonConstants.CloseBrace);
message = SR.Format(SR.MismatchedObjectArray, (char)token);
if (tokenType == JsonTokenType.PropertyName)
{
message = SR.Format(SR.CannotWriteEndAfterProperty, (char)token);
}
else
{
message = SR.Format(SR.MismatchedObjectArray, (char)token);
}
ahsonkhan marked this conversation as resolved.
Show resolved Hide resolved
break;
case ExceptionResource.DepthTooLarge:
message = SR.Format(SR.DepthTooLarge, currentDepth & JsonConstants.RemoveFlagsBitMask, JsonConstants.MaxWriterDepth);
Expand All @@ -476,7 +483,14 @@ private static string GetResourceString(ExceptionResource resource, int currentD
message = SR.Format(SR.CannotWriteValueWithinObject, tokenType);
break;
case ExceptionResource.CannotWritePropertyWithinArray:
message = SR.Format(SR.CannotWritePropertyWithinArray, tokenType);
if (tokenType == JsonTokenType.PropertyName)
{
message = SR.Format(SR.CannotWritePropertyAfterProperty);
}
else
{
message = SR.Format(SR.CannotWritePropertyWithinArray, tokenType);
}
ahsonkhan marked this conversation as resolved.
Show resolved Hide resolved
break;
case ExceptionResource.CannotWriteValueAfterPrimitive:
message = SR.Format(SR.CannotWriteValueAfterPrimitive, tokenType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ private void WriteBase64Indented(ReadOnlySpan<char> escapedPropertyName, ReadOnl
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -349,6 +351,8 @@ private void WriteBase64Indented(ReadOnlySpan<byte> escapedPropertyName, ReadOnl
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, DateTim
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -356,6 +358,8 @@ private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, DateTim
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, DateTim
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -356,6 +358,8 @@ private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, DateTim
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, decimal
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -335,6 +337,8 @@ private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, decimal
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, double
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -339,6 +341,8 @@ private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, double
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, float v
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -339,6 +341,8 @@ private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, float v
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, Guid va
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -347,6 +349,8 @@ private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, Guid va
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void ValidateWritingProperty()
{
if (!Options.SkipValidation)
{
if (!_inObject)
if (!_inObject || _tokenType == JsonTokenType.PropertyName)
{
Debug.Assert(_tokenType != JsonTokenType.StartObject);
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWritePropertyWithinArray, currentDepth: default, token: default, _tokenType);
Expand All @@ -50,7 +50,7 @@ private void ValidateWritingProperty(byte token)
{
if (!Options.SkipValidation)
{
if (!_inObject)
if (!_inObject || _tokenType == JsonTokenType.PropertyName)
{
Debug.Assert(_tokenType != JsonTokenType.StartObject);
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWritePropertyWithinArray, currentDepth: default, token: default, _tokenType);
Expand Down Expand Up @@ -109,6 +109,8 @@ private void WritePropertyNameIndented(ReadOnlySpan<byte> escapedPropertyName, b
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -180,6 +182,8 @@ private void WritePropertyNameIndented(ReadOnlySpan<char> escapedPropertyName, b
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ private void WriteLiteralIndented(ReadOnlySpan<char> escapedPropertyName, ReadOn
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -427,6 +429,8 @@ private void WriteLiteralIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOn
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, long va
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down Expand Up @@ -404,6 +406,8 @@ private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, long va
output[BytesPending++] = JsonConstants.ListSeparator;
}

Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
ahsonkhan marked this conversation as resolved.
Show resolved Hide resolved

if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
Expand Down
Loading