-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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 APIs to JSON Contract customization #71123
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsMain issue #63686 will be fixed soon with #70435 API changesnamespace System.Text.Json.Serialization.Metadata {
public static partial class JsonMetadataServices
{
public static System.Text.Json.Serialization.JsonConverter<T?> GetNullableConverter<T>(System.Text.Json.JsonSerializerOptions options) where T : struct { throw null; }
}
public abstract partial class JsonTypeInfo<T> : System.Text.Json.Serialization.Metadata.JsonTypeInfo
{
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public System.Action<System.Text.Json.Utf8JsonWriter, T>? SerializeHandler { get { throw null; } } // existing API, previously entire class was EditorBrowsable.Never
}
// TODO: add JsonPropertyInfo.UnderlyingMemberInfo
} Remaining work
|
// Maps to JsonExtensionDataAttribute
// Alternatively this could be a nullable property on JsonTypeInfo property.
public bool IsExtensionData { get; set; } = false; IsExtensionData on a type doesn't make sense to me. It's metadata for a property. How does |
@JamesNK the alternative proposal is: public class JsonTypeInfo
{
public JsonPropertyInfo? ExtensionDataProperty { get; set; }
} basically that would mean you cannot accidentally set two properties with One interesting case is the JsonExtensionDataAttribute on single property on base and derived type which we have discussed - possible solution could be ignoring the extension property on the base when it's on the derived |
There can be at most one properties marked ExtensionData per type. On serialization it will serialize key/value pairs as inlined properties of the underlying JSON object and conversely, it will be populated with deserialized properties not matching with any of the other properties. See also https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonextensiondataattribute?view=net-6.0#remarks |
Looks good as proposed.
namespace System.Text.Json.Serialization.Metadata
{
public class JsonTypeInfo
{
// Maps to IJsonOnSerializing
public Action<object>? OnSerializing { get; set; } = null;
// Maps to IJsonOnSerialized
public Action<object>? OnSerialized { get; set; } = null;
// Maps to IJsonOnDeserializing
public Action<object>? OnDeserializing { get; set; } = null;
// Maps to IJsonOnDeserialized
public Action<object>? OnDeserialized { get; set; } = null;
}
public abstract partial class JsonPropertyInfo
{
// Abstracts backing member, DefaultJsonTypeInfoResolver will return instance of PropertyInfo/FieldInfo
public System.Reflection.ICustomAttributeProvider? AttributeProvider { get; set; } = null;
// Maps to JsonPropertyOrderAttribute
public int Order { get; set; } = 0;
// Maps to JsonExtensionDataAttribute
// Alternatively this could be a nullable JsonPropertyInfo on JsonTypeInfo.
public bool IsExtensionData { get; set; } = false;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public static partial class JsonMetadataServices
{
public static JsonConverter<T?> GetNullableConverter<T>(JsonSerializerOptions options) where T : struct { throw null; }
}
}
namespace System.Text.Json
{
public partial class JsonSerializerOptions
{
// Existing GetConverter API
public JsonConverter GetConverter(Type type);
// Gets metadata that has been cached by this options instance
// Can be used to plug metadata directly into JsonSerializer overloads that accept JsonTypeInfo<T>
public JsonTypeInfo GetTypeInfo(Type type);
}
} |
Closing in favor of #63686, I moved part of the checklist to the "Progress" section there for more visibility. |
Continuation of #63686 - some API additions we wanted to make in this release.
API changes
Sample
Remaining work
JsonSerializerOptions.SerializerContext
property as combined resolvers might invalidate their intentions.StaticInitialization_DeserializationWithJsonTypeInfoWithoutSettingTypeInfoResolverThrows
andStaticInitialization_SerializationWithJsonTypeInfoWithoutSettingTypeInfoResolverThrows
<summary>
sections should be one-liners, should use<see />
elements where applicable, should use<remarks>
sections where applicable, etc. Wording seems to deviate with how we document elsewhere.JsonTypeInfo<T>
.The text was updated successfully, but these errors were encountered: