Skip to content

Commit

Permalink
Dispose JsonDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
annelo-msft committed Nov 11, 2022
1 parent 8118f74 commit 795e2ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
30 changes: 25 additions & 5 deletions sdk/core/Azure.Core/src/JsonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,53 @@ public class JsonData : IDynamicMetaObjectProvider, IEquatable<JsonData>
/// </summary>
/// <param name="utf8Json">A UTF8 encoded string representing a JSON value.</param>
/// <returns>A <see cref="JsonData"/> representation of the value.</returns>
internal static JsonData Parse(BinaryData utf8Json) => new JsonData(JsonDocument.Parse(utf8Json));
internal static JsonData Parse(BinaryData utf8Json)
{
using var doc = JsonDocument.Parse(utf8Json);
return new JsonData(doc);
}

/// <summary>
/// Parses text representing a single JSON value into a <see cref="JsonData"/>.
/// </summary>
/// <param name="json">The JSON text to parse</param>
/// <returns>A <see cref="JsonData"/> representation of the value.</returns>
internal static JsonData Parse(string json) => new JsonData(JsonDocument.Parse(json));
internal static JsonData Parse(string json)
{
using var doc = JsonDocument.Parse(json);
return new JsonData(doc);
}

/// <summary>
/// </summary>
/// <param name="utf8Json">.</param>
/// <returns>A <see cref="JsonData"/> representation of the value.</returns>
internal static JsonData Parse(ReadOnlyMemory<byte> utf8Json) => new JsonData(JsonDocument.Parse(utf8Json));
internal static JsonData Parse(ReadOnlyMemory<byte> utf8Json)
{
using var doc = JsonDocument.Parse(utf8Json);
return new JsonData(doc);
}

/// <summary>
/// </summary>
/// <param name="utf8Json">.</param>
/// <returns>A <see cref="JsonData"/> representation of the value.</returns>
internal static JsonData Parse(Stream utf8Json) => new JsonData(JsonDocument.Parse(utf8Json));
internal static JsonData Parse(Stream utf8Json)
{
using var doc = JsonDocument.Parse(utf8Json);
return new JsonData(doc);
}

/// <summary>
/// </summary>
/// <param name="utf8Json">.</param>
/// <param name="cancellationToken"></param>
/// <returns>A <see cref="JsonData"/> representation of the value.</returns>
internal static async Task<JsonData> ParseAsync(Stream utf8Json, CancellationToken cancellationToken) => new JsonData(await JsonDocument.ParseAsync(utf8Json, cancellationToken: cancellationToken).ConfigureAwait(false));
internal static async Task<JsonData> ParseAsync(Stream utf8Json, CancellationToken cancellationToken)
{
using var doc = await JsonDocument.ParseAsync(utf8Json, cancellationToken: cancellationToken).ConfigureAwait(false);
return new JsonData(doc);
}

/// <summary>
/// Gets or sets a value at the given index in an array.
Expand Down
8 changes: 8 additions & 0 deletions sdk/core/Azure.Core/tests/public/JsonDataPublicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ public void CanAccessArrayValues()
Assert.AreEqual(3, (int)jsonData.array[2]);
}

[Test]
public void CanAccessJsonPropertiesWithDotnetIllegalCharacters()
{
dynamic jsonData = new BinaryData("{ \"$foo\":\"Hello\" }").ToDynamic();

Assert.AreEqual("Hello", (string)jsonData["$foo"]);
}

[Test]
public void FloatUnderflowThrows()
{
Expand Down

0 comments on commit 795e2ff

Please sign in to comment.