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

Commit

Permalink
Add WritePropertyName standlone API on the Utf8JsonWriter. (#38864)
Browse files Browse the repository at this point in the history
* Add WritePropertyName standlone API on the Utf8JsonWriter.

* Fix write indented, add more tests, and more debug.asserts.

* Remove _isProperty field and rely on _tokenType ==
JsonTokenType.PropertyName

* Auto-gen the ref.

* Address PR feedback.
  • Loading branch information
ahsonkhan authored and msftbot[bot] committed Jun 25, 2019
1 parent aba8dac commit a5b35f5
Show file tree
Hide file tree
Showing 31 changed files with 1,100 additions and 106 deletions.
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 @@ -387,6 +387,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 @@ -152,6 +152,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 name following another property name. A JSON value is missing.</value>
</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 @@ -245,6 +248,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 following a property name.</value>
</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
8 changes: 6 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 @@ -472,7 +472,9 @@ 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);
message = (tokenType == JsonTokenType.PropertyName) ?
SR.Format(SR.CannotWriteEndAfterProperty, (char)token) :
SR.Format(SR.MismatchedObjectArray, (char)token);
break;
case ExceptionResource.DepthTooLarge:
message = SR.Format(SR.DepthTooLarge, currentDepth & JsonConstants.RemoveFlagsBitMask, JsonConstants.MaxWriterDepth);
Expand All @@ -487,7 +489,9 @@ private static string GetResourceString(ExceptionResource resource, int currentD
message = SR.Format(SR.CannotWriteValueWithinObject, tokenType);
break;
case ExceptionResource.CannotWritePropertyWithinArray:
message = SR.Format(SR.CannotWritePropertyWithinArray, tokenType);
message = (tokenType == JsonTokenType.PropertyName) ?
SR.Format(SR.CannotWritePropertyAfterProperty) :
SR.Format(SR.CannotWritePropertyWithinArray, tokenType);
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);

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

0 comments on commit a5b35f5

Please sign in to comment.