美文网首页
Newtonsoft Json自定义Serialization过

Newtonsoft Json自定义Serialization过

作者: stack | 来源:发表于2021-07-15 13:35 被阅读0次

    The IContractResolver interface provides a way to customize how the JsonSerializer serializes and deserializes .NET objects to JSON without placing attributes on your classes.

    Anything that can be set on an object, collection, property, etc, using attributes or methods to control serialization can also be set using an IContractResolver.

    For performance you should create a contract resolver once and reuse instances when possible. Resolving contracts is slow and implementations of IContractResolver typically cache contracts.

    未自定义之前的数据结构 使用了自定义序列化的camelcase的response result

    以上例子是直接使用newtonsoft提供的camelCase的resolver,当然我们也可以overrider default resolver所提供的的CreateContract方法,定义我们自己serialization逻辑。

    示例代码

    上述示例是将object中的所有的datetime和datetimeoffset类型都转换成JavaScript中是时间类型,也就是时间戳类型。

    PS: demo中还使用了Bogus第三库,来生成随机对象。

    Bogus生成faker对象

    相关文章

      网友评论

          本文标题:Newtonsoft Json自定义Serialization过

          本文链接:https://www.haomeiwen.com/subject/uwoepltx.html