Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
aeb2103
Move JsonReader related files to the Reader directory.
ahsonkhan Jan 7, 2019
76a459b
Update S.T.Json ref to include new JsonWriter APIs.
ahsonkhan Jan 7, 2019
f1eae6f
Port initial JsonWriter files from corefxlab.
ahsonkhan Jan 7, 2019
0e1a283
Auto-generate System.Text.Json ref to update the ordering for
ahsonkhan Jan 7, 2019
fd8bd6d
Fixed some TODOs, delete dead code, and formatting cleanup.
ahsonkhan Jan 7, 2019
39fc64d
Make use of self-descriptive constants where possible.
ahsonkhan Jan 7, 2019
41b7b2c
Fix leftover TODOs and update throw helper/exception messages.
ahsonkhan Jan 8, 2019
7988444
Add xml comments/docs to the public surface area.
ahsonkhan Jan 8, 2019
8276f4c
Add JsonWriter unit tests.
ahsonkhan Jan 8, 2019
9bec478
Change GetCurrentState back to a property and explicitly Flush on the
ahsonkhan Jan 8, 2019
5385574
Save current depth as part of state and update tests.
ahsonkhan Jan 8, 2019
a9f99be
Fix constant names, account for quotes in bytes needed, and add fixed…
ahsonkhan Jan 10, 2019
65b38cb
Fix inconsistent use of braces by adding them for single line ifs
ahsonkhan Jan 10, 2019
8d62b8a
Update parameter name to exclude encoding.
ahsonkhan Jan 10, 2019
c4fc141
Remove JsonWriterException and use InvalidOperationException instead.
ahsonkhan Jan 10, 2019
534ebf2
Use Rune and Utf8Formatter/TryFormat in more places and remove
ahsonkhan Jan 10, 2019
424835a
Fix nits, typos, and reorder field assignment and method calls.
ahsonkhan Jan 10, 2019
1c09b09
Pass spans by in (or by value) instead of by ref.
ahsonkhan Jan 12, 2019
a91c702
Update comments and remove unnecessary test.
ahsonkhan Jan 12, 2019
f1cdc28
Remove some aggressive inlining and pass spans by value rather than in
ahsonkhan Jan 12, 2019
baa5ac2
Update comments, dont compute bytes needed, and use if instead of loo…
ahsonkhan Jan 12, 2019
644118c
Reduce code bloat by removing duplciate calls to ValidateX.
ahsonkhan Jan 12, 2019
baa4931
Add details on how .NET types are formatted to comments.
ahsonkhan Jan 12, 2019
2b91e6c
Reduce code duplication when writing values.
ahsonkhan Jan 12, 2019
6c43954
Change the StandardFormat used for DateTime(Offset) to 'O'
ahsonkhan Jan 12, 2019
eb33db6
Refactor calculating the maximum escaped length to a helper.
ahsonkhan Jan 12, 2019
171f6c3
Remove unnecessary checks and rename locals to be more descriptive.
ahsonkhan Jan 12, 2019
a14a57e
Rename suppressEscaping to escape and flip default from false to true.
ahsonkhan Jan 12, 2019
712502b
Merge branch 'master' of https://github.com/dotnet/corefx into AddJso…
ahsonkhan Jan 15, 2019
3bc364c
Comment cleanup, add debug.asserts, and move transcoding helpers to a
ahsonkhan Jan 15, 2019
56382db
Increase the deicmal max size to account for sign and add tests.
ahsonkhan Jan 15, 2019
2900bc5
Rename ROS<byte> property name and value params to include utf8 in the
ahsonkhan Jan 15, 2019
6f41164
Remove redundant code where idx is set to 0 unnecessarily.
ahsonkhan Jan 15, 2019
c3c83e4
Remove dead code (dont escape forward slash) and make tests culture
ahsonkhan Jan 15, 2019
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
105 changes: 105 additions & 0 deletions src/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ public enum JsonTokenType : byte
String = (byte)6,
True = (byte)8,
}
public partial struct JsonWriterOptions
{
private object _dummy;
public bool Indented { get { throw null; } set { } }
public bool SkipValidation { get { throw null; } set { } }
}
public partial struct JsonWriterState
{
private object _dummy;
public JsonWriterState(System.Text.Json.JsonWriterOptions options = default(System.Text.Json.JsonWriterOptions)) { throw null; }
public long BytesCommitted { get { throw null; } }
public long BytesWritten { get { throw null; } }
Comment thread
ahsonkhan marked this conversation as resolved.
public System.Text.Json.JsonWriterOptions Options { get { throw null; } }
}
public ref partial struct Utf8JsonReader
{
private object _dummy;
Expand All @@ -71,4 +85,95 @@ public ref partial struct Utf8JsonReader
public bool TryGetInt64Value(out long value) { throw null; }
public bool TryGetSingleValue(out float value) { throw null; }
}
public ref partial struct Utf8JsonWriter
{
private object _dummy;
public Utf8JsonWriter(System.Buffers.IBufferWriter<byte> bufferWriter, System.Text.Json.JsonWriterState state = default(System.Text.Json.JsonWriterState)) { throw null; }
public long BytesCommitted { get { throw null; } }
public long BytesWritten { get { throw null; } }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these commonly accessed on the writer? Wondering when you'd use these rather than getting the CurrentState and using its BytesCommitted/Written.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these commonly accessed on the writer?

I can see them being useful if the user is doing synchronous writing and wants to get how much has been written so far. For that pattern, the user never really has to deal with state, so forcing them to call state to get meaningful data that the writer already tracks seems unnecessary.

For example, @KrzysztofCwalina needs it for his scenario: https://github.com/Azure/azure-sdk-for-net-lab/blob/4d8369fe96e99514c108dacb2cd39bc589b0042a/Configuration/Azure.Configuration/ConfigurationSettingParser.cs#L30

With the change from #34425 (comment), BytesCommitted == BytesWritten when you access them on the CurrentState (since it forces you to flush). If the user wants to know the difference between how much has been written versus committed so far (without flushing). they would need the properties on Utf8JsonWriter.

We could remove one of BytesCommitted or BytesWritten from the CurrentState though.

public int CurrentDepth { get { throw null; } }
public void Flush(bool isFinalBlock = true) { }
public System.Text.Json.JsonWriterState GetCurrentState() { throw null; }
public void WriteBoolean(System.ReadOnlySpan<byte> utf8PropertyName, bool value, bool escape = true) { }
public void WriteBoolean(System.ReadOnlySpan<char> propertyName, bool value, bool escape = true) { }
public void WriteBoolean(string propertyName, bool value, bool escape = true) { }
public void WriteBooleanValue(bool value) { }
public void WriteCommentValue(System.ReadOnlySpan<byte> utf8Value, bool escape = true) { }
public void WriteCommentValue(System.ReadOnlySpan<char> value, bool escape = true) { }
public void WriteCommentValue(string value, bool escape = true) { }
public void WriteEndArray() { }
public void WriteEndObject() { }
public void WriteNull(System.ReadOnlySpan<byte> utf8PropertyName, bool escape = true) { }
public void WriteNull(System.ReadOnlySpan<char> propertyName, bool escape = true) { }
public void WriteNull(string propertyName, bool escape = true) { }
public void WriteNullValue() { }
public void WriteNumber(System.ReadOnlySpan<byte> utf8PropertyName, decimal value, bool escape = true) { }
public void WriteNumber(System.ReadOnlySpan<byte> utf8PropertyName, double value, bool escape = true) { }
public void WriteNumber(System.ReadOnlySpan<byte> utf8PropertyName, int value, bool escape = true) { }
public void WriteNumber(System.ReadOnlySpan<byte> utf8PropertyName, long value, bool escape = true) { }
public void WriteNumber(System.ReadOnlySpan<byte> utf8PropertyName, float value, bool escape = true) { }
[System.CLSCompliantAttribute(false)]
public void WriteNumber(System.ReadOnlySpan<byte> utf8PropertyName, uint value, bool escape = true) { }
[System.CLSCompliantAttribute(false)]
public void WriteNumber(System.ReadOnlySpan<byte> utf8PropertyName, ulong value, bool escape = true) { }
public void WriteNumber(System.ReadOnlySpan<char> propertyName, decimal value, bool escape = true) { }
public void WriteNumber(System.ReadOnlySpan<char> propertyName, double value, bool escape = true) { }
public void WriteNumber(System.ReadOnlySpan<char> propertyName, int value, bool escape = true) { }
public void WriteNumber(System.ReadOnlySpan<char> propertyName, long value, bool escape = true) { }
public void WriteNumber(System.ReadOnlySpan<char> propertyName, float value, bool escape = true) { }
[System.CLSCompliantAttribute(false)]
public void WriteNumber(System.ReadOnlySpan<char> propertyName, uint value, bool escape = true) { }
[System.CLSCompliantAttribute(false)]
public void WriteNumber(System.ReadOnlySpan<char> propertyName, ulong value, bool escape = true) { }
public void WriteNumber(string propertyName, decimal value, bool escape = true) { }
public void WriteNumber(string propertyName, double value, bool escape = true) { }
public void WriteNumber(string propertyName, int value, bool escape = true) { }
public void WriteNumber(string propertyName, long value, bool escape = true) { }
public void WriteNumber(string propertyName, float value, bool escape = true) { }
[System.CLSCompliantAttribute(false)]
public void WriteNumber(string propertyName, uint value, bool escape = true) { }
[System.CLSCompliantAttribute(false)]
public void WriteNumber(string propertyName, ulong value, bool escape = true) { }
public void WriteNumberValue(decimal value) { }
public void WriteNumberValue(double value) { }
public void WriteNumberValue(int value) { }
public void WriteNumberValue(long value) { }
public void WriteNumberValue(float value) { }
[System.CLSCompliantAttribute(false)]
public void WriteNumberValue(uint value) { }
[System.CLSCompliantAttribute(false)]
public void WriteNumberValue(ulong value) { }
Comment thread
ahsonkhan marked this conversation as resolved.
public void WriteStartArray() { }
public void WriteStartArray(System.ReadOnlySpan<byte> utf8PropertyName, bool escape = true) { }
public void WriteStartArray(System.ReadOnlySpan<char> propertyName, bool escape = true) { }
public void WriteStartArray(string propertyName, bool escape = true) { }
public void WriteStartObject() { }
public void WriteStartObject(System.ReadOnlySpan<byte> utf8PropertyName, bool escape = true) { }
public void WriteStartObject(System.ReadOnlySpan<char> propertyName, bool escape = true) { }
public void WriteStartObject(string propertyName, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.DateTime value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.DateTimeOffset value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.Guid value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.ReadOnlySpan<byte> utf8Value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.ReadOnlySpan<char> value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, string value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.DateTime value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.DateTimeOffset value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.Guid value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.ReadOnlySpan<byte> utf8Value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.ReadOnlySpan<char> value, bool escape = true) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, string value, bool escape = true) { }
public void WriteString(string propertyName, System.DateTime value, bool escape = true) { }
public void WriteString(string propertyName, System.DateTimeOffset value, bool escape = true) { }
public void WriteString(string propertyName, System.Guid value, bool escape = true) { }
public void WriteString(string propertyName, System.ReadOnlySpan<byte> utf8Value, bool escape = true) { }
public void WriteString(string propertyName, System.ReadOnlySpan<char> value, bool escape = true) { }
public void WriteString(string propertyName, string value, bool escape = true) { }
public void WriteStringValue(System.DateTime value) { }
public void WriteStringValue(System.DateTimeOffset value) { }
public void WriteStringValue(System.Guid value) { }
public void WriteStringValue(System.ReadOnlySpan<byte> utf8Value, bool escape = true) { }
public void WriteStringValue(System.ReadOnlySpan<char> value, bool escape = true) { }
public void WriteStringValue(string value, bool escape = true) { }
}
}
45 changes: 45 additions & 0 deletions src/System.Text.Json/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,36 @@
<data name="ArrayDepthTooLarge" xml:space="preserve">
<value>CurrentDepth ({0}) is larger than the maximum configured depth of {1}. Cannot read next JSON array.</value>
</data>
<data name="CallFlushToAvoidDataLoss" xml:space="preserve">
<value>The JSON writer needs to be flushed before getting the current state. There are {0} bytes that have not been committed to the output.</value>
</data>
<data name="CannotStartObjectArrayAfterPrimitiveOrClose" xml:space="preserve">
<value>Cannot write the start of an object/array after a single JSON value or outside of an existing closed object/array. Current token type is '{0}'.</value>
</data>
<data name="CannotStartObjectArrayWithoutProperty" xml:space="preserve">
<value>Cannot write the start of an object or array without a property name. Current token type is '{0}'.</value>
</data>
<data name="CannotWriteInvalidUTF16" xml:space="preserve">
<value>Cannot write invalid UTF-16 text as JSON. Invalid surrogate pair: '{0}'.</value>
</data>
<data name="CannotWriteInvalidUTF8" xml:space="preserve">
<value>Cannot write invalid UTF-8 text as JSON. Invalid input: '{0}'.</value>
</data>
<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="CannotWriteValueAfterPrimitive" xml:space="preserve">
<value>Cannot write a JSON value after a single JSON value. Current token type is '{0}'.</value>
</data>
<data name="CannotWriteValueWithinObject" xml:space="preserve">
<value>Cannot write a JSON value within an object without a property name. Current token type is '{0}'.</value>
</data>
<data name="DepthTooLarge" xml:space="preserve">
<value>CurrentDepth ({0}) is equal to or larger than the maximum allowed depth of {1}. Cannot write the next JSON object or array.</value>
</data>
<data name="EmptyJsonIsInvalid" xml:space="preserve">
<value>Writing an empty JSON payload (excluding comments) is invalid.</value>
</data>
<data name="EndOfCommentNotFound" xml:space="preserve">
<value>Expected end of comment, but instead reached end of data.</value>
</data>
Expand Down Expand Up @@ -159,6 +189,12 @@
<data name="ExpectedValueAfterPropertyNameNotFound" xml:space="preserve">
<value>Expected a value, but instead reached end of data.</value>
</data>
<data name="FailedToGetLargerSpan" xml:space="preserve">
<value>The 'IBufferWriter' could not provide an output buffer that is large enough to continue writing.</value>
</data>
<data name="FailedToGetMinimumSizeSpan" xml:space="preserve">
<value>The 'IBufferWriter' could not provide an output buffer that is large enough to continue writing. Need at least {0} bytes.</value>
</data>
<data name="FoundInvalidCharacter" xml:space="preserve">
<value>'{0}' is invalid after a value. Expected either ',', '}}', or ']'.</value>
</data>
Expand Down Expand Up @@ -186,6 +222,9 @@
<data name="ObjectDepthTooLarge" xml:space="preserve">
<value>CurrentDepth ({0}) is larger than the maximum configured depth of {1}. Cannot read next JSON object.</value>
</data>
<data name="PropertyNameTooLarge" xml:space="preserve">
<value>The JSON property name of length {0} is too large and not supported by the JSON writer.</value>
</data>
<data name="RequiredDigitNotFoundAfterDecimal" xml:space="preserve">
<value>'{0}' is invalid within a number, immediately after a decimal point ('.'). Expected a digit ('0'-'9').</value>
</data>
Expand All @@ -195,6 +234,12 @@
<data name="RequiredDigitNotFoundEndOfData" xml:space="preserve">
<value>Expected a digit ('0'-'9'), but instead reached end of data.</value>
</data>
<data name="SpecialNumberValuesNotSupported" xml:space="preserve">
<value>.NET number values such as positive and negative infinity cannot be written as valid JSON.</value>
</data>
<data name="ValueTooLarge" xml:space="preserve">
<value>The JSON value of length {0} is too large and not supported by the JSON writer.</value>
</data>
<data name="ZeroDepthAtEnd" xml:space="preserve">
<value>Expected CurrentDepth ({0}) to be zero at the end of the JSON payload. There is an open JSON object or array that should be closed.</value>
</data>
Expand Down
49 changes: 40 additions & 9 deletions src/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,49 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Text\Json\BitStack.cs" />
<Compile Include="System\Text\Json\ConsumeNumberResult.cs" />
<Compile Include="System\Text\Json\ConsumeTokenResult.cs" />
<Compile Include="System\Text\Json\JsonCommentHandling.cs" />
<Compile Include="System\Text\Json\JsonConstants.cs" />
<Compile Include="System\Text\Json\JsonReaderException.cs" />
<Compile Include="System\Text\Json\JsonReaderHelper.cs" />
<Compile Include="System\Text\Json\JsonReaderOptions.cs" />
<Compile Include="System\Text\Json\JsonReaderState.cs" />
<Compile Include="System\Text\Json\JsonTokenType.cs" />
<Compile Include="System\Text\Json\ThrowHelper.cs" />
<Compile Include="System\Text\Json\Utf8JsonReader.cs" />
<Compile Include="System\Text\Json\Utf8JsonReader.MultiSegment.cs" />
<Compile Include="System\Text\Json\Utf8JsonReader.TryGet.cs" />
<Compile Include="System\Text\Json\Reader\ConsumeNumberResult.cs" />
Comment thread
ahsonkhan marked this conversation as resolved.
<Compile Include="System\Text\Json\Reader\ConsumeTokenResult.cs" />
<Compile Include="System\Text\Json\Reader\JsonReaderException.cs" />
<Compile Include="System\Text\Json\Reader\JsonReaderHelper.cs" />
<Compile Include="System\Text\Json\Reader\JsonReaderOptions.cs" />
<Compile Include="System\Text\Json\Reader\JsonReaderState.cs" />
<Compile Include="System\Text\Json\Reader\Utf8JsonReader.cs" />
<Compile Include="System\Text\Json\Reader\Utf8JsonReader.MultiSegment.cs" />
<Compile Include="System\Text\Json\Reader\Utf8JsonReader.TryGet.cs" />
<Compile Include="System\Text\Json\Writer\JsonWriterHelper.cs" />
<Compile Include="System\Text\Json\Writer\JsonWriterHelper.Escaping.cs" />
<Compile Include="System\Text\Json\Writer\JsonWriterHelper.Transcoding.cs" />
<Compile Include="System\Text\Json\Writer\JsonWriterOptions.cs" />
<Compile Include="System\Text\Json\Writer\JsonWriterState.cs" />
<Compile Include="System\Text\Json\Writer\SequenceValidity.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.DateTime.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.DateTimeOffset.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Decimal.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Double.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Float.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Guid.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Helpers.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Literal.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.SignedNumber.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.String.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.UnsignedNumber.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Comment.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.DateTime.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.DateTimeOffset.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Decimal.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Double.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Float.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Guid.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Helpers.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Literal.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.SignedNumber.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.String.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.UnsignedNumber.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Collections\src\System.Collections.csproj" />
Expand All @@ -30,6 +60,7 @@
<ProjectReference Include="..\..\System.Numerics.Vectors\src\System.Numerics.Vectors.csproj" />
<ProjectReference Include="..\..\System.Resources.ResourceManager\src\System.Resources.ResourceManager.csproj" />
<ProjectReference Include="..\..\System.Runtime\src\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Runtime.Extensions\src\System.Runtime.Extensions.csproj" />
<ProjectReference Include="..\..\System.Text.Encoding.Extensions\src\System.Text.Encoding.Extensions.csproj" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading