Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonNode feature #51025

Merged
merged 10 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
13 changes: 13 additions & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.InboxOnly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
steveharter marked this conversation as resolved.
Show resolved Hide resolved
// The .NET Foundation licenses this file to you under the MIT license.
// ------------------------------------------------------------------------------
// Changes to this file must follow the https://aka.ms/api-review process.
// ------------------------------------------------------------------------------

namespace System.Text.Json.Node
{
public partial class JsonNode : System.Dynamic.IDynamicMetaObjectProvider
{
System.Dynamic.DynamicMetaObject System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(System.Linq.Expressions.Expression parameter) { throw null; }
}
}
167 changes: 167 additions & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
<ItemGroup>
<Compile Include="System.Text.Json.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<Compile Include="System.Text.Json.InboxOnly.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != '$(NetCoreAppCurrent)'">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Memory\ref\System.Memory.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Linq.Expressions\ref\System.Linq.Expressions.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Reference Include="System.Memory" />
Expand Down
30 changes: 30 additions & 0 deletions src/libraries/System.Text.Json/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,34 @@
<data name="SerializerConverterFactoryReturnsJsonConverterFactory" xml:space="preserve">
<value>The converter '{0}' cannot return an instance of JsonConverterFactory.</value>
</data>
<data name="NodeDynamicObjectResultNotAssignable" xml:space="preserve">
<value>The result type '{0}' of the dynamic binding produced by the object with type '{1}' for the binder '{2}' is not compatible with the result type '{3}' expected by the call site.</value>
</data>
<data name="NodeElementWrongType" xml:space="preserve">
<value>The element must be of type '{0}'</value>
</data>
<data name="NodeElementCannotBeObjectOrArray" xml:space="preserve">
<value>The element cannot be an object or array.</value>
</data>
<data name="NodeAlreadyHasParent" xml:space="preserve">
<value>The node already has a parent.</value>
</data>
<data name="NodeCycleDetected" xml:space="preserve">
<value>A node cycle was detected.</value>
</data>
<data name="NodeUnableToConvert" xml:space="preserve">
<value>A value of type '{0}' cannot be converted to a '{1}'.</value>
</data>
<data name="NodeUnableToConvertElement" xml:space="preserve">
<value>An element of type '{0}' cannot be converted to a '{1}'.</value>
</data>
<data name="NodeValueNotAllowed" xml:space="preserve">
<value>A JsonNode cannot be used as a value.</value>
</data>
<data name="NodeWrongType" xml:space="preserve">
<value>The node must be of type '{0}'.</value>
</data>
<data name="ValueCannotBeNull" xml:space="preserve">
<value>Value cannot be null. (Parameter '{0}')</value>
</data>
</root>
24 changes: 24 additions & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
<Compile Include="System\Text\Json\JsonHelpers.Date.cs" />
<Compile Include="System\Text\Json\JsonHelpers.Escaping.cs" />
<Compile Include="System\Text\Json\JsonTokenType.cs" />
<Compile Include="System\Text\Json\Node\JsonArray.cs" />
<Compile Include="System\Text\Json\Node\JsonArray.IList.cs" />
<Compile Include="System\Text\Json\Node\JsonNode.cs" />
<Compile Include="System\Text\Json\Node\JsonNode.Dynamic.cs" Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'" />
<Compile Include="System\Text\Json\Node\JsonNode.Operators.cs" />
<Compile Include="System\Text\Json\Node\JsonNode.Parse.cs" />
<Compile Include="System\Text\Json\Node\JsonNode.To.cs" />
<Compile Include="System\Text\Json\Node\JsonNodeOptions.cs" />
<Compile Include="System\Text\Json\Node\JsonObject.cs" />
<Compile Include="System\Text\Json\Node\JsonObject.Dynamic.cs" Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'" />
<Compile Include="System\Text\Json\Node\JsonObject.IDictionary.cs" />
<Compile Include="System\Text\Json\Node\JsonValue.cs" />
<Compile Include="System\Text\Json\Node\JsonValueOfT.cs" />
<Compile Include="System\Text\Json\Node\MetaDynamic.cs" Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'" />
<Compile Include="System\Text\Json\Reader\ConsumeNumberResult.cs" />
<Compile Include="System\Text\Json\Reader\ConsumeTokenResult.cs" />
<Compile Include="System\Text\Json\Reader\JsonReaderException.cs" />
Expand Down Expand Up @@ -95,6 +109,11 @@
<Compile Include="System\Text\Json\Serialization\Converters\Collection\ListOfTConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Collection\QueueOfTConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Collection\StackOfTConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonArrayConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonNodeConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonNodeConverterFactory.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonObjectConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Node\JsonValueConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Object\JsonObjectConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Object\KeyValuePairConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Object\ObjectConverterFactory.cs" />
Expand Down Expand Up @@ -169,6 +188,7 @@
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.Converters.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.cs" />
<Compile Include="System\Text\Json\Serialization\JsonStringEnumConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonUnknownTypeHandling.cs" />
<Compile Include="System\Text\Json\Serialization\MemberAccessor.cs" />
<Compile Include="System\Text\Json\Serialization\MetadataPropertyName.cs" />
<Compile Include="System\Text\Json\Serialization\ParameterRef.cs" />
Expand All @@ -189,6 +209,7 @@
<Compile Include="System\Text\Json\Serialization\WriteStack.cs" />
<Compile Include="System\Text\Json\Serialization\WriteStackFrame.cs" />
<Compile Include="System\Text\Json\ThrowHelper.cs" />
<Compile Include="System\Text\Json\ThrowHelper.Node.cs" />
<Compile Include="System\Text\Json\ThrowHelper.Serialization.cs" />
<Compile Include="System\Text\Json\Writer\JsonWriterHelper.cs" />
<Compile Include="System\Text\Json\Writer\JsonWriterHelper.Date.cs" />
Expand Down Expand Up @@ -259,6 +280,9 @@
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Reference Include="System.Diagnostics.Tools" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)' or '$(TargetFramework)' == 'netcoreapp3.0'">
<Reference Include="System.Linq.Expressions" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or $(TargetFramework.StartsWith('net4'))">
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,39 @@ public static JsonDocument Parse(Stream utf8Json, JsonDocumentOptions options =
}
}

internal static JsonDocument ParseValue(Stream utf8Json, JsonDocumentOptions options)
{
Debug.Assert(utf8Json != null);

ArraySegment<byte> drained = ReadToEnd(utf8Json);
Debug.Assert(drained.Array != null);

byte[] owned = new byte[drained.Count];
Buffer.BlockCopy(drained.Array, 0, owned, 0, drained.Count);

// Holds document content, clear it before returning it.
drained.AsSpan().Clear();
ArrayPool<byte>.Shared.Return(drained.Array);

return ParseUnrented(owned.AsMemory(), options.GetReaderOptions());
}

internal static JsonDocument ParseValue(ReadOnlySpan<byte> utf8Json, JsonDocumentOptions options)
{
Debug.Assert(utf8Json != null);

byte[] owned = new byte[utf8Json.Length];
utf8Json.CopyTo(owned);

return ParseUnrented(owned.AsMemory(), options.GetReaderOptions());
}

internal static JsonDocument ParseValue(string json, JsonDocumentOptions options)
{
Debug.Assert(json != null);
return ParseValue(json.AsMemory(), options);
}

/// <summary>
/// Parse a <see cref="Stream"/> as UTF-8-encoded data representing a single JSON value into a
/// JsonDocument. The Stream will be read to completion.
Expand Down Expand Up @@ -230,6 +263,31 @@ public static JsonDocument Parse(ReadOnlyMemory<char> json, JsonDocumentOptions
}
}

internal static JsonDocument ParseValue(ReadOnlyMemory<char> json, JsonDocumentOptions options)
{
ReadOnlySpan<char> jsonChars = json.Span;
int expectedByteCount = JsonReaderHelper.GetUtf8ByteCount(jsonChars);
byte[] owned;
byte[] utf8Bytes = ArrayPool<byte>.Shared.Rent(expectedByteCount);

try
{
int actualByteCount = JsonReaderHelper.GetUtf8FromText(jsonChars, utf8Bytes);
Debug.Assert(expectedByteCount == actualByteCount);

owned = new byte[actualByteCount];
Buffer.BlockCopy(utf8Bytes, 0, owned, 0, actualByteCount);
}
finally
{
// Holds document content, clear it before returning it.
utf8Bytes.AsSpan(0, expectedByteCount).Clear();
ArrayPool<byte>.Shared.Return(utf8Bytes);
}

return ParseUnrented(owned.AsMemory(), options.GetReaderOptions());
}

/// <summary>
/// Parse text representing a single JSON value into a JsonDocument.
/// </summary>
Expand Down Expand Up @@ -639,7 +697,7 @@ private static JsonDocument Parse(
private static JsonDocument ParseUnrented(
ReadOnlyMemory<byte> utf8Json,
JsonReaderOptions readerOptions,
JsonTokenType tokenType)
JsonTokenType tokenType = JsonTokenType.None)
{
// These tokens should already have been processed.
Debug.Assert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;

namespace System.Text.Json
{
Expand Down Expand Up @@ -52,6 +53,24 @@ public static JsonElement ParseValue(ref Utf8JsonReader reader)
return document.RootElement;
}

internal static JsonElement ParseValue(Stream utf8Json, JsonDocumentOptions options)
{
JsonDocument document = JsonDocument.ParseValue(utf8Json, options);
return document.RootElement;
}

internal static JsonElement ParseValue(ReadOnlySpan<byte> utf8Json, JsonDocumentOptions options)
{
JsonDocument document = JsonDocument.ParseValue(utf8Json, options);
return document.RootElement;
}

internal static JsonElement ParseValue(string json, JsonDocumentOptions options)
{
JsonDocument document = JsonDocument.ParseValue(json, options);
return document.RootElement;
}

/// <summary>
/// Attempts to parse one JSON value (including objects or arrays) from the provided reader.
/// </summary>
Expand Down
Loading