-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
JsonMarshal.GetRawUtf8Value
(#104595)
* Add JsonMarshal.TryGetRawValue * Add escaping test. * Apply API review suggestions. * Address feedback
- Loading branch information
1 parent
a48a39f
commit 635f9af
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/libraries/System.Text.Json/src/System/Runtime/InteropServices/JsonMarshal.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.Json; | ||
|
||
namespace System.Runtime.InteropServices | ||
{ | ||
/// <summary> | ||
/// An unsafe class that provides a set of methods to access the underlying data representations of JSON types. | ||
/// </summary> | ||
public static class JsonMarshal | ||
{ | ||
/// <summary> | ||
/// Gets a <see cref="ReadOnlySpan{T}"/> view over the raw JSON data of the given <see cref="JsonElement"/>. | ||
/// </summary> | ||
/// <param name="element">The JSON element from which to extract the span.</param> | ||
/// <returns>The span containing the raw JSON data of<paramref name="element"/>.</returns> | ||
/// <exception cref="ObjectDisposedException">The underlying <see cref="JsonDocument"/> has been disposed.</exception> | ||
/// <remarks> | ||
/// While the method itself does check for disposal of the underlying <see cref="JsonDocument"/>, | ||
/// it is possible that it could be disposed after the method returns, which would result in | ||
/// the span pointing to a buffer that has been returned to the shared pool. Callers should take | ||
/// extra care to make sure that such a scenario isn't possible to avoid potential data corruption. | ||
/// </remarks> | ||
public static ReadOnlySpan<byte> GetRawUtf8Value(JsonElement element) | ||
{ | ||
return element.GetRawValue().Span; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters