使用System.Text.Json的可选属性的自定义JSON序列化器

如何解决使用System.Text.Json的可选属性的自定义JSON序列化器

我正在尝试实现一种同时处理null和缺少的JSON值的JSON序列化机制,以便能够在需要时执行部分更新(以使该值不触及数据库中的字段)丢失,但在将值显式设置为null时将其清除。

我创建了一个自定义结构,该结构是从Roslyn的Optional<T>类型复制的:

public readonly struct Optional<T>
{
    public Optional(T value)
    {
        this.HasValue = true;
        this.Value = value;
    }

    public bool HasValue { get; }
    public T Value { get; }
    public static implicit operator Optional<T>(T value) => new Optional<T>(value);
    public override string ToString() => this.HasValue ? (this.Value?.ToString() ?? "null") : "unspecified";
}

现在,我希望能够从JSON序列化/反序列化,以便在通过Optional<T>对象进行往返处理时保留JSON中所有丢失的字段:

public class CustomType
{
    [JsonPropertyName("foo")]
    public Optional<int?> Foo { get; set; }

    [JsonPropertyName("bar")]
    public Optional<int?> Bar { get; set; }

    [JsonPropertyName("baz")]
    public Optional<int?> Baz { get; set; }
}

然后:

var options = new JsonSerializerOptions();
options.Converters.Add(new OptionalConverter());

string json = @"{""foo"":0,""bar"":null}";
CustomType parsed = JsonSerializer.Deserialize<CustomType>(json,options);
string roundtrippedJson = JsonSerializer.Serialize(parsed,options);

// json and roundtrippedJson should be equivalent
Console.WriteLine("json:             " + json);
Console.WriteLine("roundtrippedJson: " + roundtrippedJson);

我基于JsonConverterFactory开始了一个实现,但是如果可选的HasValuefalse,我似乎找不到正确的方法来在序列化期间忽略该属性:

public class OptionalConverter : JsonConverterFactory
{
    public override bool CanConvert(Type typeToConvert)
    {
        if (!typeToConvert.IsGenericType) { return false; }
        if (typeToConvert.GetGenericTypeDefinition() != typeof(Optional<>)) { return false; }
        return true;
    }

    public override JsonConverter CreateConverter(Type typeToConvert,JsonSerializerOptions options)
    {
        Type valueType = typeToConvert.GetGenericArguments()[0];

        return (JsonConverter)Activator.CreateInstance(
            type: typeof(OptionalConverterInner<>).MakeGenericType(new Type[] { valueType }),bindingAttr: BindingFlags.Instance | BindingFlags.Public,binder: null,args: null,culture: null
        );
    }

    private class OptionalConverterInner<T> : JsonConverter<Optional<T>>
    {
        public override Optional<T> Read(ref Utf8JsonReader reader,Type typeToConvert,JsonSerializerOptions options)
        {
            T value = JsonSerializer.Deserialize<T>(ref reader,options);
            return new Optional<T>(value);
        }

        public override void Write(Utf8JsonWriter writer,Optional<T> value,JsonSerializerOptions options)
        {
            // Does not work (produces invalid JSON).
            // Problem: the object's key has already been written in the JSON writer at this point.
            if (value.HasValue)
            {
                JsonSerializer.Serialize(writer,value.Value,options);
            }
        }
    }
}

问题:这将产生以下输出,但无效:

json:             {"foo":0,"bar":null}
roundtrippedJson: {"foo":0,"bar":null,"baz":}

我该如何解决?

解决方法

自定义JsonConverter<T>不能阻止转换器应用的值的序列化,请参见 [System.Text.Json] Converter-level conditional serialization #36275 进行确认。

在.Net 5中,将有一个选项可以忽略默认值,该默认值应满足您的需要,请参见已关闭的 System.Text.Json option to ignore default values during serialization #779 在.Net 5中实现。此版本引入了JsonIgnoreCondition.WhenWritingDefault

public enum JsonIgnoreCondition
{
    /// <summary>
    /// Property is never ignored during serialization or deserialization.
    /// </summary>
    Never = 0,/// <summary>
    /// Property is always ignored during serialization and deserialization.
    /// </summary>
    Always = 1,/// <summary>
    /// If the value is the default,the property is ignored during serialization.
    /// This is applied to both reference and value-type properties and fields.
    /// </summary>
    WhenWritingDefault = 2,/// <summary>
    /// If the value is <see langword="null"/>,the property is ignored during  serialization.
    /// This is applied only to reference-type properties and fields.
    /// </summary>
    WhenWritingNull = 3,}

您将能够通过[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]或通过设置JsonSerializerOptions.DefaultIgnoreCondition将条件应用于特定属性。

因此,在.Net 5中,您的类如下所示:

public class CustomType
{
    [JsonPropertyName("foo")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
    public Optional<int?> Foo { get; set; }

    [JsonPropertyName("bar")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
    public Optional<int?> Bar { get; set; }

    [JsonPropertyName("baz")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
    public Optional<int?> Baz { get; set; }
}

(。Net 5当然仍处于预览状态,因此最终的API可能会更改。)

在.Net 3中,由于System.Text.Json中没有条件序列化机制,因此,有条件地省略没有值的可选属性的唯一选择是写一个custom JsonConverter<T> 用于所有包含可选属性的类。 JsonSerializer does not provide any access to its internal contract information的事实使这一点变得不容易,因此我们需要为每种此类类型手工制作一个转换器,或者通过反射编写我们自己的通用代码。

这是创建此类通用代码的一种尝试:

public interface IHasValue
{
    bool HasValue { get; }
    object GetValue();
}

public readonly struct Optional<T> : IHasValue
{
    public Optional(T value)
    {
        this.HasValue = true;
        this.Value = value;
    }

    public bool HasValue { get; }
    public T Value { get; }
    public object GetValue() => Value;
    public static implicit operator Optional<T>(T value) => new Optional<T>(value);
    public override string ToString() => this.HasValue ? (this.Value?.ToString() ?? "null") : "unspecified";
}

public class TypeWithOptionalsConverter<T> : JsonConverter<T> where T : class,new()
{
    class TypeWithOptionalsConverterContractFactory : JsonObjectContractFactory<T>
    {
        protected override Expression CreateSetterCastExpression(Expression e,Type t)
        {
            // (Optional<Nullable<T>>)(object)default(T) does not work,even though (Optional<Nullable<T>>)default(T) does work.
            // To avoid the problem we need to first cast to Nullable<T>,then to Optional<Nullable<T>>
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Optional<>))
                return Expression.Convert(Expression.Convert(e,t.GetGenericArguments()[0]),t);
            return base.CreateSetterCastExpression(e,t);
        }
    }
    
    static readonly TypeWithOptionalsConverterContractFactory contractFactory = new TypeWithOptionalsConverterContractFactory();
    
    public override T Read(ref Utf8JsonReader reader,Type typeToConvert,JsonSerializerOptions options)
    {
        var properties = contractFactory.GetProperties(typeToConvert);

        if (reader.TokenType == JsonTokenType.Null)
            return null;
        if (reader.TokenType != JsonTokenType.StartObject)
            throw new JsonException();
        var value = new T();
        while (reader.Read())
        {
            if (reader.TokenType == JsonTokenType.EndObject)
                return value;
            if (reader.TokenType != JsonTokenType.PropertyName)
                throw new JsonException();
            string propertyName = reader.GetString();
            if (!properties.TryGetValue(propertyName,out var property) || property.SetValue == null)
            {
                reader.Skip();
            }
            else
            {
                var type = property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Optional<>) 
                    ? property.PropertyType.GetGenericArguments()[0] : property.PropertyType;
                var item = JsonSerializer.Deserialize(ref reader,type,options);
                property.SetValue(value,item);
            }
        }
        throw new JsonException();
    }           

    public override void Write(Utf8JsonWriter writer,T value,JsonSerializerOptions options)
    {
        writer.WriteStartObject();
        foreach (var property in contractFactory.GetProperties(value.GetType()))
        {
            if (options.IgnoreReadOnlyProperties && property.Value.SetValue == null)
                continue;
            var item = property.Value.GetValue(value);
            if (item is IHasValue hasValue)
            {
                if (!hasValue.HasValue)
                    continue;
                writer.WritePropertyName(property.Key);
                JsonSerializer.Serialize(writer,hasValue.GetValue(),options);
            }
            else
            {
                if (options.IgnoreNullValues && item == null)
                    continue;
                writer.WritePropertyName(property.Key);
                JsonSerializer.Serialize(writer,item,property.Value.PropertyType,options);
            }
        }
        writer.WriteEndObject();
    }
}

public class JsonPropertyContract<TBase>
{
    internal JsonPropertyContract(PropertyInfo property,Func<Expression,Type,Expression> setterCastExpression)
    {
        this.GetValue = ExpressionExtensions.GetPropertyFunc<TBase>(property).Compile();
        if (property.GetSetMethod() != null)
            this.SetValue = ExpressionExtensions.SetPropertyFunc<TBase>(property,setterCastExpression).Compile();
        this.PropertyType = property.PropertyType;
    }
    public Func<TBase,object> GetValue { get; }
    public Action<TBase,object> SetValue { get; }
    public Type PropertyType { get; }
}

public class JsonObjectContractFactory<TBase>
{
    protected virtual Expression CreateSetterCastExpression(Expression e,Type t) => Expression.Convert(e,t);
    
    ConcurrentDictionary<Type,ReadOnlyDictionary<string,JsonPropertyContract<TBase>>> Properties { get; } = 
        new ConcurrentDictionary<Type,JsonPropertyContract<TBase>>>();

    ReadOnlyDictionary<string,JsonPropertyContract<TBase>> CreateProperties(Type type)
    {
        if (!typeof(TBase).IsAssignableFrom(type))
            throw new ArgumentException();
        var dictionary = type
            .GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
            .Where(p => p.GetIndexParameters().Length == 0 && p.GetGetMethod() != null
                   && !Attribute.IsDefined(p,typeof(System.Text.Json.Serialization.JsonIgnoreAttribute)))
            .ToDictionary(p => p.GetCustomAttribute<System.Text.Json.Serialization.JsonPropertyNameAttribute>()?.Name ?? p.Name,p => new JsonPropertyContract<TBase>(p,(e,t) => CreateSetterCastExpression(e,t)),StringComparer.OrdinalIgnoreCase);
        return dictionary.ToReadOnly();
    }

    public IReadOnlyDictionary<string,JsonPropertyContract<TBase>> GetProperties(Type type) => Properties.GetOrAdd(type,t => CreateProperties(t));
}

public static class DictionaryExtensions
{
    public static ReadOnlyDictionary<TKey,TValue> ToReadOnly<TKey,TValue>(this IDictionary<TKey,TValue> dictionary) => 
        new ReadOnlyDictionary<TKey,TValue>(dictionary ?? throw new ArgumentNullException());
}

public static class ExpressionExtensions
{
    public static Expression<Func<T,object>> GetPropertyFunc<T>(PropertyInfo property)
    {
        // (x) => (object)x.Property;
        var arg = Expression.Parameter(typeof(T),"x");
        var getter = Expression.Property(arg,property);
        var cast = Expression.Convert(getter,typeof(object));
        return Expression.Lambda<Func<T,object>>(cast,arg);
    }   

    public static Expression<Action<T,object>> SetPropertyFunc<T>(PropertyInfo property,Expression> setterCastExpression)
    {
        //(x,y) => x.Property = (TProperty)y       
        var arg1 = Expression.Parameter(typeof(T),"x");
        var arg2 = Expression.Parameter(typeof(object),"y");
        var cast = setterCastExpression(arg2,property.PropertyType);
        var setter = Expression.Call(arg1,property.GetSetMethod(),cast);
        return Expression.Lambda<Action<T,object>>(setter,arg1,arg2);
    }   
}

注意:

  • CustomType仍然显示在您的问题中。

  • 未尝试处理JsonSerializerOptions.PropertyNamingPolicy中的命名策略。如有必要,您可以在TypeWithOptionalsConverter<T>中实现。

  • 我添加了一个非通用接口IHasValue,以便在序列化期间更轻松地访问盒装Optional<T>

演示小提琴here

或者,您可以坚持使用Json.NET,它在属性和联系人级别支持此功能。参见:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-