自定义JSON.NET更改空值和属性名

2023/7/6 02:13:32

"在.NET平台中要序列化JSON对象,有很多轻量级框架可供选择,我通常使用Newtonsoft的JSON.NET框架,虽然它足够的灵活,提供很多的转换器,但在实际开发中发现下面两种需求JSON.NET自身的转换器将变得无能为力:

1、序列化时将对象的null值转换为空字符串(双引号),而不是输出null值。

2、在序列化和反序列化时需要修改属性名、转换属性名大小写,或者通过自己的规则映射属性名。

3、在.NET中属性采用PascalCase规则(首字母大写),在JavaScript中属性名使用CamelCase规则(首字母小写)

基于特殊的需求,为了不影响原本框架,零度自己实现了个性化的转换器CustomJsonConverter用于解决上述问题,CustomJsonConverter.cs将在本文末尾提供下载,您可以直接添加到自己的项目中,下面是自定义转换器的使用示例。

将NULL空值转换为空字符串

<span style=background: white; color: blue;>public class <span style=background: white; color: #2b91af;>Computer <span style=background: white; color: black;>{ <span style=background: white; color: blue;>public string <span style=background: white; color: black;>Name { <span style=background: white; color: blue;>get<span style=background: white; color: black;>; <span style=background: white; color: blue;>set<span style=background: white; color: black;>; } <span style=background: white; color: blue;>public decimal <span style=background: white; color: black;>SalePrice { <span style=background: white; color: blue;>get<span style=background: white; color: black;>; <span style=background: white; color: blue;>set<span style=background: white; color: black;>; } <span style=background: white; color: blue;>public int<span style=background: white; color: black;>? StockCount { <span style=background: white; color: blue;>get<span style=background: white; color: black;>; <span style=background: white; color: blue;>set<span style=background: white; color: black;>; } }

<span style=background: white; color: #2b91af;>Computer <span style=background: white; color: black;>computer = <span style=background: white; color: blue;>new <span style=background: white; color: #2b91af;>Computer<span style=background: white; color: black;>(); computer.Name = <span style=background: white; color: #a31515;>零度计算机<span style=background: white; color: black;>; computer.SalePrice = 1023.55M; computer.StockCount = <span style=background: white; color: blue;>null<span style=background: white; color: black;>; <span style=background: white; color: #2b91af;>CustomJsonConverter <span style=background: white; color: black;>convert = <span style=background: white; color: blue;>new <span style=background: white; color: #2b91af;>CustomJsonConverter<span style=background: white; color: black;>(); <span style=background: white; color: green;>//自定义转换器 <span style=background: white; color: black;>convert.PropertyNullValueReplaceValue = <span style=background: white; color: #a31515;>""""<span style=background: white; color: black;>; <span style=background: white; color: green;>//设置替换NULL值得字符串 <span style=background: white; color: blue;>string <span style=background: white; color: black;>str = <span style=background: white; color: #2b91af;>JsonConvert<span style=background: white; color: black;>.SerializeObject(computer, convert);

序列化后的结果如下所示,已将StockCount的null转换为空字符串。

<span style=background: white; color: black;>{ <span style=background: white; color: #a31515;>Name<span style=background: white; color: black;>: <span style=background: white; color: #a31515;>零度计算机<span style=background: white; color: black;>, <span style=background: white; color: #a31515;>SalePrice<span style=background: white; color: black;>: 1023.55, <span style=background: white; color: #a31515;>StockCount<span style=background: white; color: black;>: <span style=background: white; color: #a31515;>"""" <span style=background: white; color: black;>}

使用CamelCase或者PascalCase命名规则将属性名首字母大小写输出

<span style=background: white; color: #2b91af;>CustomJsonConverter <span style=background: white; color: black;>convert = <span style=background: white; color: blue;>new <span style=background: white; color: #2b91af;>CustomJsonConverter<span style=background: white; color: black;>(); convert.PropertyNameCase = <span style=background: white; color: #2b91af;>ConverterPropertyNameCase<span style=background: white; color: black;>.CamelCase; <span style=background: white; color: blue;>string <span style=background: white; color: black;>str = <span style=background: white; color: #2b91af;>JsonConvert<span style=background: white; color: black;>.SerializeObject(computer, convert);

序列化后的结果将属性名首字母采用CamelCase风格小写输出,如下所示:

<span style=background: white; color: black;>{ <span style=background: white; color: #a31515;>name<span style=background: white; color: black;>: <span style=background: white; color: #a31515;>零度计算机<span style=background: white; color: black;>, <span style=background: white; color: #a31515;>salePrice<span style=background: white; color: black;>: 1023.55, <span style=background: white; color: #a31515;>stockCount<span style=background: white; color: black;>: <span style=background: white; color: blue;>null <span style=background: white; color: black;>}

使用回调函数自定义属性转换规则

<span style=background: white; color: #2b91af;>CustomJsonConverter <span style=background: white; color: black;>convert = <span style=background: white; color: blue;>new <span style=background: white; color: #2b91af;>CustomJsonConverter<span style=background: white; color: black;>(); convert.ProperyNameConverter = o => { <span style=background: white; color: blue;>return <span style=background: white; color: black;>o.ToUpper(); }; <span style=background: white; color: blue;>string <span style=background: white; color: black;>str = <span style=background: white; color: #2b91af;>JsonConvert<span style=background: white; color: black;>.SerializeObject(computer, convert);

上面的代码自定义一个回调用函数将属性名全部转换成大写形式,序列化有的结果:

<span style=background: white; color: black;>{ <span style=background: white; color: #a31515;>NAME<span style=background: white; color: black;>: <span style=background: white; color: #a31515;>零度计算机<span style=background: white; color: black;>, <span style=background: white; color: #a31515;>SALEPRICE<span style=background: white; color: black;>: 1023.55, <span style=background: white; color: #a31515;>STOCKCOUNT<span style=background: white; color: black;>: <span style=background: white; color: blue;>null <span style=background: white; color: black;>}

自定义转换器支持动态类型和匿名对象

<span style=background: white; color: blue;>dynamic <span style=background: white; color: black;>book = <span style=background: white; color: blue;>new <span style=background: white; color: #2b91af;>ExpandoObject<span style=background: white; color: black;>(); book.guid = <span style=background: white; color: #2b91af;>Guid<span style=background: white; color: black;>.NewGuid(); book.bookPrice = 32.55; book.name = <span style=background: white; color: blue;>null<span style=background: white; color: black;>; <span style=background: white; color: #2b91af;>CustomJsonConverter <span style=background: white; color: black;>convert = <span style=background: white; color: blue;>new <span style=background: white; color: #2b91af;>CustomJsonConverter<span style=background: white; color: black;>(); convert.ProperyNameConverter = o => { <span style=background: white; color: blue;>return string<span style=background: white; color: black;>.Concat(o, <span style=background: white; color: #a31515;>$<span style=background: white; color: black;>); }; convert.PropertyNullValueReplaceValue = <span style=background: white; color: blue;>string<span style=background: white; color: black;>.Empty; convert.PropertyNameCase = <span style=background: white; color: #2b91af;>ConverterPropertyNameCase<span style=background: white; color: black;>.PascalCase; <span style=background: white; color: blue;>string <span style=background: white; color: black;>str = <span style=background: white; color: #2b91af;>JsonConvert<span style=background: white; color: black;>.SerializeObject(book, convert);

上面的代码将dynamic动态类型转换为JSON,同时将属性名末尾添加一个$符号,将null转为空字符串,同时将属性名称首字母大写使用PascalCase规则输出,输出如下内容:

<span style=background: white; color: black;>{ <span style=background: white; color: #a31515;>Guid$<span style=background: white; color: black;>: <span style=background: white; color: #a31515;>e13dfaf4-f4e8-441e-b6ba-f74ac210103a<span style=background: white; color: black;>, <span style=background: white; color: #a31515;>BookPrice$<span style=background: white; color: black;>: 32.55, <span style=background: white; color: #a31515;>Name$<span style=background: white; color: black;>: <span style=background: white; color: #a31515;>"""" <span style=background: white; color: black;>}

同样,支持反序列化,将字符串反序列化为静态对象或者动态对象,规则同样适用。

<span style=background: white; color: blue;>string <span style=background: white; color: black;>json = <span style=background: white; color: #a31515;>@{ 'guid$': 'e13dfaf4-f4e8-441e-b6ba-f74ac210103a', 'bookPrice$': 32.55, 'name$': null }<span style=background: white; color: black;>; <span style=background: white; color: #2b91af;>CustomJsonConverter <span style=background: white; color: black;>convert = <span style=background: white; color: blue;>new <span style=background: white; color: #2b91af;>CustomJsonConverter<span style=background: white; color: black;>(); convert.ProperyNameConverter = o => { <span style=background: white; color: blue;>return <span style=background: white; color: black;>o.Remove(o.Length - 1); }; convert.PropertyNameCase = <span style=background: white; color: #2b91af;>ConverterPropertyNameCase<span style=background: white; color: black;>.PascalCase; convert.PropertyNullValueReplaceValue = <span style=background: white; color: #a31515;>NullValue<span style=background: white; color: black;>; <span style=background: white; color: blue;>dynamic <span style=background: white; color: black;>book = <span style=background: white; color: #2b91af;>JsonConvert<span style=background: white; color: black;>.DeserializeObject<<span style=background: white; color: blue;>dynamic<span style=background: white; color: black;>>(json, convert); <span style=background: white; color: #2b91af;>Console<span style=background: white; color: black;>.WriteLine(<span style=background: white; color: #a31515;>BookGuid={0}<span style=background: white; color: black;>,book.Guid); <span style=background: white; color: #2b91af;>Console<span style=background: white; color: black;>.WriteLine(<span style=background: white; color: #a31515;>BookPrice={0}<span style=background: white; color: black;>, book.BookPrice); <span style=background: white; color: #2b91af;>Console<span style=background: white; color: black;>.WriteLine(<span style=background: white; color: #a31515;>BookName={0}<span style=background: white; color: black;>, book.Name);

将一段不符合系统规定的JSON反序列化为规则的对象,属性首字母采用PascalCase规则,用NullValue替换null值,输出后的结果:

<span style=background: white; color: black;>BookGuid=e13dfaf4-f4e8-441e-b6ba-f74ac210103a BookPrice=32.55 BookName=NullValue

由于时间关系,自定义转换器时未经过大量的测试,如果您遇到问题,可联系博主修改,下面是转换器下载地址。

零度下载"