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 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a19d268
Add an in-box array-backed IBufferWriter<T>
ahsonkhan Apr 15, 2019
b522e0c
Update Utf8JsonWriter ref and main writer file.
ahsonkhan Apr 15, 2019
341d937
Fix JsonWriter WriteValue APIs.
ahsonkhan Apr 15, 2019
9caf27c
Use Environment.NewLine static and invert some stream conditions.
ahsonkhan Apr 15, 2019
38a3834
Update JsonWriter properties and fix serializer build breaks.
ahsonkhan Apr 16, 2019
61f51c4
Update JsonWriter unit tests.
ahsonkhan Apr 16, 2019
1406858
Add xml comments, clean up, and improve test coverage.
ahsonkhan Apr 17, 2019
449d935
Update JsonDocument and JsonSerializer to react to JsonWriter changes.
ahsonkhan Apr 17, 2019
ec4d03f
Normalize the reference assembly.
ahsonkhan Apr 17, 2019
5a060d1
Do not escape/validate comments and update issue number.
ahsonkhan Apr 17, 2019
fc5c82a
Do not escape comments and validate they don't contain embedded
ahsonkhan Apr 17, 2019
b44b6b8
Merge branch 'master' of https://github.com/dotnet/corefx into Redesi…
ahsonkhan Apr 17, 2019
d6213e3
Remove dead code and update issue number in comments.
ahsonkhan Apr 17, 2019
50e832c
Throw InvalidOEx instead of ArgEx if IBW doesn't give requested memory.
ahsonkhan Apr 17, 2019
f94ca69
Fix test build breaks for netfx.
ahsonkhan Apr 17, 2019
6aa5efc
Remove dead code and fix source packaging.
ahsonkhan Apr 18, 2019
5af4802
Merge branch 'master' of https://github.com/dotnet/corefx into Redesi…
ahsonkhan Apr 18, 2019
695c9df
Address PR feedback.
ahsonkhan Apr 18, 2019
4cecd6a
Disable writing floats test on windows
ahsonkhan Apr 18, 2019
4d7a90e
8 digit floats don't work well on older TFMs. Reduce to 7.
ahsonkhan Apr 18, 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
Prev Previous commit
Next Next commit
Update JsonWriter unit tests.
  • Loading branch information
ahsonkhan committed Apr 16, 2019
commit 61f51c436ac79b309fafdd84f0a65149fdf37f70
3 changes: 3 additions & 0 deletions src/System.Text.Json/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,7 @@
<data name="SerializerOptionsImmutable" xml:space="preserve">
<value>Serializer options cannot be changed once serialization or deserialization has occurred.</value>
</data>
<data name="StreamNotWritable" xml:space="preserve">
<value>Stream was not writable.</value>
Copy link
Member

Choose a reason for hiding this comment

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

Nit: was => is ?

Copy link
Author

@ahsonkhan ahsonkhan Apr 18, 2019

Choose a reason for hiding this comment

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

</data>
</root>
20 changes: 15 additions & 5 deletions src/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ public Utf8JsonWriter(IBufferWriter<byte> bufferWriter, JsonWriterOptions option
/// </exception>
public Utf8JsonWriter(Stream utf8Json, JsonWriterOptions options = default)
{
_stream = utf8Json ?? throw new ArgumentNullException(nameof(utf8Json));
if (utf8Json == null)
throw new ArgumentNullException(nameof(utf8Json));
if (!utf8Json.CanWrite)
throw new ArgumentException(SR.StreamNotWritable);

_stream = utf8Json;
_arrayBufferWriter = new ArrayBufferWriter<byte>();
_output = _arrayBufferWriter;

Expand Down Expand Up @@ -166,11 +171,16 @@ public void Reset()
/// but now write to the passed in <see cref="Stream" /> as the new destination.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// Thrown when the instance of <see cref="IBufferWriter{Byte}" /> that is passed in is null.
/// Thrown when the instance of <see cref="Stream" /> that is passed in is null.
/// </exception>
public void Reset(Stream utf8Json)
{
_stream = utf8Json ?? throw new ArgumentNullException(nameof(utf8Json));
if (utf8Json == null)
throw new ArgumentNullException(nameof(utf8Json));
if (!utf8Json.CanWrite)
throw new ArgumentException(SR.StreamNotWritable);

_stream = utf8Json;
if (_arrayBufferWriter == null)
{
_arrayBufferWriter = new ArrayBufferWriter<byte>();
Expand All @@ -193,7 +203,7 @@ public void Reset(Stream utf8Json)
/// but now write to the passed in <see cref="IBufferWriter{Byte}" /> as the new destination.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// Thrown when the instance of <see cref="Stream" /> that is passed in is null.
/// Thrown when the instance of <see cref="IBufferWriter{Byte}" /> that is passed in is null.
/// </exception>
public void Reset(IBufferWriter<byte> bufferWriter)
{
Expand Down Expand Up @@ -792,7 +802,7 @@ private void WriteEndIndented(byte token)
}

Debug.Assert(indent <= 2 * JsonConstants.MaxWriterDepth);
Debug.Assert(_tokenType != JsonTokenType.None);
Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.None);

int maxRequired = indent + 3; // 1 end token, 1-2 bytes for new line

Expand Down
2 changes: 2 additions & 0 deletions src/System.Text.Json/tests/FixedSizedBufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public void Clear()

public byte[] Formatted => _buffer.AsSpan(0, _count).ToArray();

public int FormattedCount => _count;

public Memory<byte> GetMemory(int minimumLength = 0) => _buffer.AsMemory(_count);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
72 changes: 31 additions & 41 deletions src/System.Text.Json/tests/JsonElementWriteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -831,15 +831,14 @@ public static void WritePropertyOutsideObject(bool skipValidation)
using (var doc = JsonDocument.Parse("[ null, false, true, \"hi\", 5, {}, [] ]", s_readerOptions))
{
JsonElement root = doc.RootElement;
JsonWriterState state = new JsonWriterState(
new JsonWriterOptions
{
SkipValidation = skipValidation,
});
var options = new JsonWriterOptions
{
SkipValidation = skipValidation,
};

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

if (skipValidation)
{
Expand Down Expand Up @@ -874,11 +873,7 @@ public static void WritePropertyOutsideObject(bool skipValidation)
(ref Utf8JsonWriter w) => val.WriteAsProperty(byteUtf8, ref w));
}

JsonTestHelper.AssertThrows<InvalidOperationException>(
ref writer,
(ref Utf8JsonWriter w) => w.Flush());

writer.Flush(isFinalBlock: false);
writer.Flush();

AssertContents("", buffer);
}
Expand All @@ -894,13 +889,12 @@ public static void WriteValueInsideObject(bool skipValidation)
using (var doc = JsonDocument.Parse("[ null, false, true, \"hi\", 5, {}, [] ]", s_readerOptions))
{
JsonElement root = doc.RootElement;
JsonWriterState state = new JsonWriterState(
new JsonWriterOptions
{
SkipValidation = skipValidation,
});
var options = new JsonWriterOptions
{
SkipValidation = skipValidation,
};

Utf8JsonWriter writer = new Utf8JsonWriter(buffer, state);
var writer = new Utf8JsonWriter(buffer, options);
writer.WriteStartObject();

if (skipValidation)
Expand Down Expand Up @@ -941,13 +935,12 @@ private static void WriteSimpleValue(bool indented, string jsonIn, string jsonOu
{
JsonElement target = doc.RootElement[0];

var state = new JsonWriterState(
new JsonWriterOptions
{
Indented = indented,
});
var options = new JsonWriterOptions
{
Indented = indented,
};

var writer = new Utf8JsonWriter(buffer, state);
var writer = new Utf8JsonWriter(buffer, options);

target.WriteAsValue(ref writer);
writer.Flush();
Expand All @@ -967,13 +960,12 @@ private static void WriteComplexValue(
{
JsonElement target = doc.RootElement[0];

var state = new JsonWriterState(
new JsonWriterOptions
{
Indented = indented,
});
var options = new JsonWriterOptions
{
Indented = indented,
};

var writer = new Utf8JsonWriter(buffer, state);
var writer = new Utf8JsonWriter(buffer, options);

target.WriteAsValue(ref writer);
writer.Flush();
Expand Down Expand Up @@ -1023,13 +1015,12 @@ private static void WritePropertyValue(
{
JsonElement target = doc.RootElement[0];

var state = new JsonWriterState(
new JsonWriterOptions
{
Indented = indented,
});
var options = new JsonWriterOptions
{
Indented = indented,
};

var writer = new Utf8JsonWriter(buffer, state);
var writer = new Utf8JsonWriter(buffer, options);

writer.WriteStartObject();
target.WriteAsProperty(propertyName, ref writer);
Expand Down Expand Up @@ -1059,13 +1050,12 @@ private static void WritePropertyValue(
{
JsonElement target = doc.RootElement[0];

var state = new JsonWriterState(
new JsonWriterOptions
{
Indented = indented,
});
var options = new JsonWriterOptions
{
Indented = indented,
};

var writer = new Utf8JsonWriter(buffer, state);
var writer = new Utf8JsonWriter(buffer, options);

writer.WriteStartObject();
target.WriteAsProperty(propertyName, ref writer);
Expand Down
56 changes: 56 additions & 0 deletions src/System.Text.Json/tests/JsonWriterOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.Text.Json.Tests
{
public static partial class JsonWriterOptionsTests
{
[Fact]
public static void JsonWriterOptionsDefaultCtor()
{
JsonWriterOptions options = default;

var expectedOption = new JsonWriterOptions
{
Indented = false,
SkipValidation = false
};
Assert.Equal(expectedOption, options);
}

[Fact]
public static void JsonWriterOptionsCtor()
{
var options = new JsonWriterOptions();

var expectedOption = new JsonWriterOptions
{
Indented = false,
SkipValidation = false
};
Assert.Equal(expectedOption, options);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public static void JsonWriterOptions(bool indented, bool skipValidation)
{
var options = new JsonWriterOptions();
options.Indented = indented;
options.SkipValidation = skipValidation;

var expectedOption = new JsonWriterOptions
{
Indented = indented,
SkipValidation = skipValidation
};
Assert.Equal(expectedOption, options);
}
}
}
56 changes: 0 additions & 56 deletions src/System.Text.Json/tests/JsonWriterStateTests.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/System.Text.Json/tests/System.Text.Json.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<Compile Include="JsonNumberTestData.cs" />
<Compile Include="JsonReaderStateAndOptionsTests.cs" />
<Compile Include="JsonTestHelper.cs" />
<Compile Include="JsonWriterStateTests.cs" />
<Compile Include="JsonWriterOptionsTests.cs" />
<Compile Include="ResizableArray.cs" />
<Compile Include="Serialization\Array.ReadTests.cs" />
<Compile Include="Serialization\Array.WriteTests.cs" />
Expand Down
Loading