| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Text.Json; |
| | | 5 | | |
| | | 6 | | namespace System.Runtime.InteropServices |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// An unsafe class that provides a set of methods to access the underlying data representations of JSON types. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class JsonMarshal |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets a <see cref="ReadOnlySpan{T}"/> view over the raw JSON data of the given <see cref="JsonElement"/>. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="element">The JSON element from which to extract the span.</param> |
| | | 17 | | /// <returns>The span containing the raw JSON data of<paramref name="element"/>.</returns> |
| | | 18 | | /// <exception cref="ObjectDisposedException">The underlying <see cref="JsonDocument"/> has been disposed.</exce |
| | | 19 | | /// <remarks> |
| | | 20 | | /// While the method itself does check for disposal of the underlying <see cref="JsonDocument"/>, |
| | | 21 | | /// it is possible that it could be disposed after the method returns, which would result in |
| | | 22 | | /// the span pointing to a buffer that has been returned to the shared pool. Callers should take |
| | | 23 | | /// extra care to make sure that such a scenario isn't possible to avoid potential data corruption. |
| | | 24 | | /// </remarks> |
| | | 25 | | public static ReadOnlySpan<byte> GetRawUtf8Value(JsonElement element) |
| | 0 | 26 | | { |
| | 0 | 27 | | return element.GetRawValue().Span; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets a <see cref="ReadOnlySpan{T}"/> view over the raw JSON data of the given <see cref="JsonProperty"/> nam |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="property">The JSON property from which to extract the span.</param> |
| | | 34 | | /// <returns>The span containing the raw JSON data of the <paramref name="property"/> name. This will not includ |
| | | 35 | | /// <exception cref="ObjectDisposedException">The underlying <see cref="JsonDocument"/> has been disposed.</exce |
| | | 36 | | /// <remarks> |
| | | 37 | | /// <para> |
| | | 38 | | /// While the method itself does check for disposal of the underlying <see cref="JsonDocument"/>, |
| | | 39 | | /// it is possible that it could be disposed after the method returns, which would result in |
| | | 40 | | /// the span pointing to a buffer that has been returned to the shared pool. Callers should take |
| | | 41 | | /// extra care to make sure that such a scenario isn't possible to avoid potential data corruption. |
| | | 42 | | /// </para> |
| | | 43 | | /// </remarks> |
| | | 44 | | public static ReadOnlySpan<byte> GetRawUtf8PropertyName(JsonProperty property) |
| | 0 | 45 | | { |
| | 0 | 46 | | return property.NameSpan; |
| | 0 | 47 | | } |
| | | 48 | | } |
| | | 49 | | } |