您可以扩展 JSON.NET 中用于集合的默认 JsonConverter 吗?
我正在尝试编写一个自定义 JsonConverter,用于处理一个人对列表或集合进行子类化的情况,然后向子类添加额外的属性(请参阅 这里)。 JSON.NET 的当前实现只是将列表更改为子对象数组,并忽略所有添加的属性。因此,我想编写一个新的 JsonConverter ,将对象视为不是列表,并正常序列化其他所有内容,然后在序列化中添加一个名为“_Items”的新属性实际数据数组的存储位置。
现在我已经编写了一个类,它可以为我们的特定 List 子类执行此操作,但我必须手动一一指定所有属性。但是,如果我可以编写一个转换器,将其视为普通对象,然后手动处理这些项目,那我就很高兴了。我什至不在乎我是否最终复制了另一个类的一半(甚至更多!),但我很乐意为这些情况制作一个可重复使用的转换器。但是,正如我所说,我找不到可以启动的默认转换器。
那么...有人知道那是哪里吗?
I'm trying to write a custom JsonConverter for cases where a person subclasses a list or collection, but then adds extra properties to the subclass (see here). The current implementation of JSON.NET just changes the list into an array of child objects and ignores all of the added properties. So I want to write a new JsonConverter that treats the object as if it wasn't a List and to just serialize everything else as normal, but then to add a new property in the serialization called '_Items' where the actual array of data is stored.
Now I've already written a class that does exactly this for our specific List subclass, but I had to manually specify all the properties one-by-one. But if I could write a converter that treats this just as a normal object, then manually handle the items, I'd be golden. I don't even care if I end up duplicating half of another class (or even more!) but I'd love to make a reusable converter for these cases. However, as I said, I can't find the default converter to start from.
So... anyone know where that is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JSON.NET 中没有“默认转换器”。
如果您能够检查 JsonSerializerInternalWriter< /a> 类,查看
SerializeValue
方法。其中,顶部有“转换器查找和执行”阶段。但是,如果没有匹配的转换器,它将诉诸契约类型序列化(switch 语句)。我还没有找到一种方法(正确的方法或优雅的黑客)能够通过实体上的扩展自定义序列化执行通用合同序列化(例如:正常解析对象)(我假设您正在尝试做)。
There is no 'default converter' in JSON.NET.
If you are able to examine the JsonSerializerInternalWriter class, look at the
SerializeValue
method. In it, there is the 'converter find and perform' phase at the top. However, if there is no converter that matches, it resorts to the contract type serialization (the switch statement).I haven't found a way (a correct way or a graceful hack) to be able to perform a generic contract serialization (ex: parsing an object as normal) with an extended custom serialization on an entity (which I assume you are trying to do).
正如 @dbc 所说,您可以覆盖
CanRead
和CanWrite
返回false
并为您的属性注册CustomAsDefaultConvertor : JsonConverter
。我的案例:
MyModel:
在启动注册中,我已经覆盖了自定义的默认 JsonSerialization,但对于属性
JObject Arguments
我已经指定了本例中所需的转换器。As @dbc said here, you can override
CanRead
andCanWrite
to returnfalse
and registerCustomAsDefaultConvertor : JsonConverter
for your property.My case:
MyModel:
In startUp registration I have overrided default JsonSerialization on my custom, but for property
JObject Arguments
I have specified converter that I need in this case.不确定从何时起以及这是否是答案,但有属性
JsonConvert.DefaultSettings
似乎用于在内部获取JsonSerializerSettings
https://www.newtonsoft.com/json/help/html/DefaultSettings.htm
Not sure since when and if this is the answer but there is property
JsonConvert.DefaultSettings
which seem used to getJsonSerializerSettings
internallyhttps://www.newtonsoft.com/json/help/html/DefaultSettings.htm