将`DefaultValue`应用于序列化中的每个字符串属性

时间:2019-01-16 14:52:37

标签: c# json json.net

DefaultValue应用于每个字符串属性。

上下文:

以下类是自动生成的。编辑它们以添加那些属性是一个负担。由于生成的类很大,并且生成的代码具有很多现有的属性等。我制作了一个程序,该程序打开CS文件并使用一些正则表达式添加该属性。但仍然必须维护程序以匹配生成的文件名。


使用Json.Net,为了在序列化结果中隐藏空字符串,我必须使用[DefaultValue("")]定义那些字符串的默认值。

在以下示例中,我们有一些类和嵌套类。 目的是在序列化Foo项时隐藏Bar中的空字符串。

public class Foo
{
    public int Prop1;
    [DefaultValue("")]
    public string Prop2;
    public int? Prop3;
    public Bar Prop4;
    public Bar[] Prop5;
};

public class Bar
{
    public int Prop1;
    public string Prop2;
    public int? Prop3;
};

public void NullOrEmptyStringTerminator()
{
    var bar = new Bar { Prop1 = 0, Prop2 = "", Prop3 = 0 };
    var bar2 = new Bar { Prop1 = 0, Prop3 = 0 };

    var inputs = new[] {
       new Foo{ Prop1= 1, Prop2="", Prop4= bar, Prop5 = new []{bar} },
       new Foo{ Prop1= 1, Prop4= bar, Prop5 = new []{bar2} },
    };

    var jsonResults = 
            inputs
                .Select(x =>
                    new
                    {
                        Item = x,
                        NormalSerialisation =
                            JsonConvert.SerializeObject(
                                x,
                                Formatting.Indented,
                                new JsonSerializerSettings { }
                            ),
                        CustomSerialisation =
                             JsonConvert.SerializeObject(
                                x,
                                Formatting.Indented,
                                new JsonSerializerSettings
                                {
                                    NullValueHandling = NullValueHandling.Ignore,
                                    DefaultValueHandling = DefaultValueHandling.Ignore,
                                })
                    }
                ).ToList();
}

测试案例:

class Foo : 
  int    Prop1 -> Real value 1
  string Prop2 -> No value either "" or null
  int?   Prop3 -> No value null
  Bar    Prop4 -> Real value Bar
  Bar[]  Prop5 -> Real value Bar[]

class Bar :
  int    Prop1 -> No value 0.
  string Prop2 -> Same test than Foo.Prop2
  int?   Prop3 -> Real value 0; Not null

两个输入Json的预期结果。

{
  "Prop1": 1,
  "Prop4": {
    "Prop3": 0
  },
  "Prop5": [
    {
      "Prop3": 0
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

您必须在Bar类中设置DefaultValueAttribute。另一种选择是将属性设置为null

https://dotnetfiddle.net/aEoBlT

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        var inputs = new List<object>()
        {new Foo{Prop2 = "" , Bar = new Bar()}};
        foreach (object input in inputs)
        {
            string NormalSerialisation = JsonConvert.SerializeObject(input, Formatting.Indented, new JsonSerializerSettings{});
            string CustomSerialisation = JsonConvert.SerializeObject(input, Formatting.Indented, new JsonSerializerSettings{NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore});
            Console.WriteLine("normal:");
            Console.WriteLine(NormalSerialisation);
            Console.WriteLine("custom:");
            Console.WriteLine(CustomSerialisation);
        }
    }

    public class Foo
    {

        public string Prop2
        {
            get;
            set;
        }

        public Bar Bar
        {
            get;
            set;
        }
    }

    public class Bar
    {
        [DefaultValue("")]
        public string Prop2;
    }
}

输出:

normal:
{
  "Prop2": "",
  "Bar": {
    "Prop2": null
  }
}
custom:
{
  "Prop2": "",
  "Bar": {}
}
相关问题