-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Feature request: treat Memory/ReadOnlyMemory<T> as T[] in System.Text.Json #86442
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsI would like System.Text.Json to serialize/deserialize This implies:
The main motivation is that I have an array from
|
Provisional API proposal: namespace System.Text.Json.Serialization.Metadata;
public partial class JsonMetadataServices
{
// Similar to byte[] serializes as Base64 strings
public static JsonConverter<Memory<byte>> MemoryByteConverter { get; }
public static JsonConverter<ReadOnlyMemory<byte>> ReadOnlyMemoryByteConverter { get; }
// Other element types serialize as JSON arrays
public static JsonTypeInfo<Memory<TElement>> CreateMemoryInfo<TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<Memory<TElement>> collectionInfo);
public static JsonTypeInfo<ReadOnlyMemory<TElement>> CreateReadOnlyMemoryInfo<TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<ReadOnlyMemory<TElement>> collectionInfo);
} |
Looks good as proposed namespace System.Text.Json.Serialization.Metadata;
public partial class JsonMetadataServices
{
// Similar to byte[] serializes as Base64 strings
public static JsonConverter<Memory<byte>> MemoryByteConverter { get; }
public static JsonConverter<ReadOnlyMemory<byte>> ReadOnlyMemoryByteConverter { get; }
// Other element types serialize as JSON arrays
public static JsonTypeInfo<Memory<TElement>> CreateMemoryInfo<TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<Memory<TElement>> collectionInfo);
public static JsonTypeInfo<ReadOnlyMemory<TElement>> CreateReadOnlyMemoryInfo<TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<ReadOnlyMemory<TElement>> collectionInfo);
} |
EDIT: see #86442 (comment) for API proposal.
Original Proposal
I would like System.Text.Json to serialize/deserialize
Memory/ReadOnlyMemory<T>
using the same logic as it uses forT[]
.This implies:
Memory<byte>
should be base64-encoded just likebyte[]
isMemory<FooBar>
should be serialized as appropriate for the typeFooBar
, same asFooBar[]
would.The main motivation is that I have an array from
ArrayPool<T>.Shared
, which can have space for more items than I actually need. To serialize only a segment of the array, I would like to useMemory<T>
to identify only use "used" part of the array.The text was updated successfully, but these errors were encountered: