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

Initial Json serialization functionality #35609

Merged
merged 11 commits into from Mar 2, 2019

Conversation

steveharter
Copy link
Member

@steveharter steveharter commented Feb 27, 2019

This adds basic Json serialization functionality per https://github.com/dotnet/corefx/issues/34372

Notes:

  • Only the APIs which have been reviewed are present which includes the main entry points in JsonSerializer and the corresponding options class JsonSerializerOptions. This does not include the extensibility model that that existed in the prototype.
  • The extensibility model is currently under design and review.
  • Api documentation will be added soon to the reviewed APIs.

cc @joshfree @terrajobst @davidfowl

@steveharter steveharter added * NO MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) area-System.Text.Json labels Feb 27, 2019
@steveharter steveharter self-assigned this Feb 27, 2019

namespace System.Text.Json.Serialization
{
// Note: this is currently an internal class that will be replaced with a shared version.
Copy link
Member

Choose a reason for hiding this comment

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

My understanding from the API review is that the shared version will actually be something like a ReadOnlySequenceWriter. Will that work for the serializer's needs? If so, can it be adapted to that design now?

Copy link
Member Author

Choose a reason for hiding this comment

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

@ahsonkhan what is the status of having a public implementation of IBufferWriter?

@steveharter steveharter removed the * NO MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Feb 27, 2019
@steveharter steveharter force-pushed the JsonSerializer branch 2 times, most recently from f0940cf to a42abf4 Compare March 1, 2019 19:27
Type propertyType = PropertyType;
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = Nullable.GetUnderlyingType(propertyType);

Choose a reason for hiding this comment

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

Perhaps cache the non-nullable property type so it's not computed on each property being read?

@steveharter
Copy link
Member Author

I've addressed the active concerns and will commit this soon pending no new actionable feedback. Thanks.

Copy link
Member

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

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

Otherwise, LGTM.

// We have less than half the buffer available, double the buffer size.
bufferSize = (bufferSize < HalfMaxValue) ? bufferSize * 2 : int.MaxValue;

byte[] dest = ArrayPool<byte>.Shared.Rent(bufferSize);
Copy link
Member

Choose a reason for hiding this comment

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

Renting int.MaxValue would throw OOM. Should we cap it to something smaller (either 2GB, or maybe int.MaxValue - 56)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do we have a policy on this elsewhere? It seems like a common scenario.

Assert.Null(objCopy.GetMyString());
}

// Todo: add tests with missing object property and missing collection property.
Copy link
Member

Choose a reason for hiding this comment

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

Can you file an issue to improve test coverage here. Also to track your earlier comment:

Branch coverage is 80%-100% depending on file, but somewhat hampered because some tests are disabled because some of the APIs are not yet public (they were made internal). I do need more negative tests.

src/System.Text.Json/tests/Serialization/SpanTests.cs Outdated Show resolved Hide resolved
src/System.Text.Json/tests/System.Text.Json.Tests.csproj Outdated Show resolved Hide resolved
@steveharter steveharter merged commit 20657b5 into dotnet:master Mar 2, 2019
@steveharter steveharter deleted the JsonSerializer branch March 2, 2019 19:20
@davidfowl
Copy link
Member

W00p!

@@ -281,3 +287,30 @@ public partial struct JsonWriterState
public void WriteStringValue(string value, bool escape = true) { }
}
}
namespace System.Text.Json.Serialization
{
public static partial class JsonSerializer
Copy link
Member

Choose a reason for hiding this comment

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

Oh wait this PR removed the PipeReader and PipeWriter overloads 😢

@TylerBrinkley
Copy link

FYI, I've removed the need for Nullable converters and have added support for generic enum converters in #35754.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants