csharp Json可空字段(字符串,数组)转换器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Json可空字段(字符串,数组)转换器相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using Newtonsoft.Json;

namespace Newtonsoft.Json.Serialization
{
    /// <summary>
    /// json 필드 중 string 또는 array의 값이 null일 경우 string.Empty 또는 "[]"으로 치환하도록 한다.
    /// cf. http://stackoverflow.com/questions/23830206/json-convert-empty-string-instead-of-null
    /// usage)
    /// 
    /// JsonSerializerSettings settings = new JsonSerializerSettings();
    /// settings.ContractResolver = new JsonNullToEmptyContractResolver();
    /// vat json = JsonConvert.SerializeObject(value, settings);
    /// </summary>
    public class JsonNullToEmptyContractResolver : DefaultContractResolver // or CamelCasePropertyNamesContractResolver
    {
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);
            if (property != null)
            {
                property.ValueProvider = new NullToEmptyValueProvider(property.PropertyType, property.ValueProvider);
            }

            return property;
        }

        private class NullToEmptyValueProvider : IValueProvider
        {
            private readonly Type _propType;
            private readonly IValueProvider _provider;

            public NullToEmptyValueProvider(Type propType, IValueProvider provider)
            {
                if (propType == null)
                {
                    throw new ArgumentNullException(nameof(propType));
                }

                if (provider == null)
                {
                    throw new ArgumentNullException(nameof(provider));
                }

                _propType = propType;
                _provider = provider;
            }

            public object GetValue(object target)
            {
                var value = _provider.GetValue(target);
                if (value == null)
                {
                    if (_propType == typeof(string))
                    {
                        value = string.Empty;
                    }
                    else if (_propType.IsArray)
                    {
                        value = Enumerable.Empty<object>();
                    }
                    else if (typeof(IEnumerable).IsAssignableFrom(_propType))
                    {
                        value = Enumerable.Empty<object>();
                    }
                }

                return value;
            }

            public void SetValue(object target, object value)
            {
                _provider.SetValue(target, value);
            }
        }
    }
}

以上是关于csharp Json可空字段(字符串,数组)转换器的主要内容,如果未能解决你的问题,请参考以下文章

AWS Glue 将字符串值从 postgres 转换为 json 数组

csharp Linq加入多个可空列

csharp 使用可空类型的示例。

csharp 使用可空类型的示例。

查询Mysql表之后将结果转换为json时如何能够保持字段的原有数据类型?

嵌套js代码中可空值的可空数组首先graphql