Replies: 13 comments 4 replies
-
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Beta Was this translation helpful? Give feedback.
-
希望能直接定义未知类型处理派生类 |
Beta Was this translation helpful? Give feedback.
-
Native translation: This is the official example: If unknown type handler can be customized, I can specify that class directly. For example, adding a new bool property on the original attribute |
Beta Was this translation helpful? Give feedback.
-
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue Details这是官方的例子[JsonPolymorphic(
UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor)]
[JsonDerivedType(typeof(BasePoint))]
[JsonDerivedType(typeof(IPointWithTimeSeries))]
public interface IPoint { }
public interface IPointWithTimeSeries : IPoint { }
public class BasePoint : IPoint { }
public class BasePointWithTimeSeries : BasePoint, IPointWithTimeSeries { } 提议官方例子中,处理未知类型时, 诺能自定义未知处理类,就可以直接指定该类型。 比如在原有的基础上新增一个布尔属性 API Proposal// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Text.Json.Serialization
{
/// <summary>
/// When placed on a type declaration, indicates that the specified subtype should be opted into polymorphic serialization.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true, Inherited = false)]
public class JsonDerivedTypeAttribute : JsonAttribute
{
/// <summary>
/// Initializes a new attribute with specified parameters.
/// </summary>
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared based type.</param>
public JsonDerivedTypeAttribute(Type derivedType)
{
DerivedType = derivedType;
}
/// <summary>
/// Initializes a new attribute with specified parameters.
/// </summary>
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared base type.</param>
/// <param name="typeDiscriminator">The type discriminator identifier to be used for the serialization of the subtype.</param>
public JsonDerivedTypeAttribute(Type derivedType, string typeDiscriminator)
{
DerivedType = derivedType;
TypeDiscriminator = typeDiscriminator;
}
/// <summary>
/// Initializes a new attribute with specified parameters.
/// </summary>
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared base type.</param>
/// <param name="typeDiscriminator">The type discriminator identifier to be used for the serialization of the subtype.</param>
public JsonDerivedTypeAttribute(Type derivedType, int typeDiscriminator)
{
DerivedType = derivedType;
TypeDiscriminator = typeDiscriminator;
}
public JsonDerivedTypeAttribute(Type derivedType, bool isDefaultHandler )
{
DerivedType = derivedType;
TypeDiscriminator = typeDiscriminator;
IsDefaultHandler = isDefaultHandler ;
}
/// <summary>
/// A derived type that should be supported in polymorphic serialization of the declared base type.
/// </summary>
public Type DerivedType { get; }
/// <summary>
/// The type discriminator identifier to be used for the serialization of the subtype.
/// </summary>
public object? TypeDiscriminator { get; }
/// <summary>
/// 是否默认处理
/// </summary>
public bool IsDefaultHandler { get; }
}
} API Usage[JsonPolymorphic(TypeDiscriminatorPropertyName = "type"]
[JsonDerivedType(typeof(UnknownMessage) , true)]
public abstract record Message
{
[JsonPropertyName("type")]
public required string RawType { get; init; }
}
public record UnknownMessage : Message
{
......
} Alternative DesignsNo response RisksNo response
|
Beta Was this translation helpful? Give feedback.
-
Note that |
Beta Was this translation helpful? Give feedback.
-
Approximate structureNo unknown typeUnknown typeOtherAlthough it can catch exceptions and solve them, I hope it can handle them by default Code block
Error
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
The exception message occurs because your |
Beta Was this translation helpful? Give feedback.
-
谢谢你的提醒,我这边创建了一个接口,Message记承了这个接口作为基类(非抽象),虽然是他已经正常工作,但他没有达到我的预期。他仅返回到了基类,但我希望他能够使用我用基类实现的未知处理类( Machine translationThank you for your reminder. I have created an interface here, and Message has inherited this interface as a base class (non-abstract). Although it has been working normally, it has not met my expectations. He just falls back to the base class, but I want him to be able to use the unknown handler class ( Image |
Beta Was this translation helpful? Give feedback.
-
我觉得可以在 JsonDerivedTypeAttribute 中添加一个布尔类型属性,或是创建一个新的Attribute,或TypeDiscriminator为null时,序列化与反序列化遇到未知的类型名称使用这个类型进行序列化和反序列化时,如果没有这个标识,在做退回到基类处理。 I think you can add a Boolean type attribute to JsonDerivedTypeAttribute, or create a new Attribute, or when TypeDiscriminator is null, serialization and deserialization encounter unknown type names and use this type for serialization and deserialization When , if there is no such flag, it will return to the base class for processing. |
Beta Was this translation helpful? Give feedback.
-
This is just a suggestion. JsonSerializer.Deserialize can directly deserialize the customization derived from this base class, so why let us convert the type in an extra layer. |
Beta Was this translation helpful? Give feedback.
-
By the way, in addition to referring to the custom unknown processing class, can you add a function to return the data of this segment. Similar to |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
这是官方的例子
https://learn.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0
提议
官方例子中,处理未知类型时,
UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToBaseType
返回上一个派生类,我觉得这样处理不太友好,比如我有一个专门处理未知的派生类,那我所有派生类都得继承这个未知处理的类,属实有点麻烦,且命名不方便。诺能自定义未知处理类,就可以直接指定该类型。
比如在原有的基础上新增一个布尔属性
[JsonDerivedType(typeof(UnknownMessage), bool unknownDefaultHandler = true)]
当然这个未知处理类得是抽象或接口实现的派生类。或者添加新的Attribute,如:JsonDefaultDerivedAttribute,至于怎么命名看你们怎么取了。API Proposal
API Usage
Alternative Designs
No response
Risks
No response
Beta Was this translation helpful? Give feedback.
All reactions