Initial Json serialization functionality #35609
Conversation
cd2031f
to
ebd9c75
Compare
9d38598
to
e06427e
Compare
|
||
namespace System.Text.Json.Serialization | ||
{ | ||
// Note: this is currently an internal class that will be replaced with a shared version. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
src/System.Text.Json/src/System/Text/Json/Serialization/ArrayBufferWriter.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/ArrayBufferWriter.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultArrayConverter.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultArrayConverter.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultConverters.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultConverters.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultEnumConverter.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonPropertyInfoChar.cs
Outdated
Show resolved
Hide resolved
...stem.Text.Json/src/System/Text/Json/Serialization/Converters/JsonPropertyInfoCharNullable.cs
Outdated
Show resolved
Hide resolved
...stem.Text.Json/src/System/Text/Json/Serialization/Converters/JsonPropertyInfoCharNullable.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonPropertyInfoDateTime.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterByte.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterChar.cs
Show resolved
Hide resolved
...ext.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterDateTimeNullable.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Stream.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs
Outdated
Show resolved
Hide resolved
f0940cf
to
a42abf4
Compare
Type propertyType = PropertyType; | ||
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) | ||
{ | ||
propertyType = Nullable.GetUnderlyingType(propertyType); |
There was a problem hiding this comment.
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?
I've addressed the active concerns and will commit this soon pending no new actionable feedback. Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise, LGTM.
src/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Span.cs
Show resolved
Hide resolved
// 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); |
There was a problem hiding this comment.
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
)?
There was a problem hiding this comment.
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.
src/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs
Outdated
Show resolved
Hide resolved
Assert.Null(objCopy.GetMyString()); | ||
} | ||
|
||
// Todo: add tests with missing object property and missing collection property. |
There was a problem hiding this comment.
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.
62e3d1c
to
796dafc
Compare
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 |
There was a problem hiding this comment.
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 😢
FYI, I've removed the need for Nullable converters and have added support for generic enum converters in #35754. |
Commit migrated from dotnet/corefx@20657b5
This adds basic Json serialization functionality per https://github.com/dotnet/corefx/issues/34372
Notes:
JsonSerializer
and the corresponding options classJsonSerializerOptions
. This does not include the extensibility model that that existed in the prototype.cc @joshfree @terrajobst @davidfowl