有没有一种方法可以在使用ServiceStack json进行序列化时忽略默认值? [英] Is there a way to ignore default values during serialization with ServiceStack json?

查看:58
本文介绍了有没有一种方法可以在使用ServiceStack json进行序列化时忽略默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework作为我的ORM.它具有ComplexTypeAttribute(用于注释POCO).复杂类型的属性总是被实例化(使用默认构造函数),而不管它们的值如何;因此,总是由ServiceStack JsonSerializer序列化(及其属性).

I am using Entity Framework as my ORM. It has ComplexTypeAttribute (which is used to annotate POCO's). Properties that are complex types, are always instantiated (using default constructor), regardless of their value; And as a consequence, are always serialized by ServiceStack JsonSerializer (along with their properties).

JSON.NET有一个名为DefaultValueHandling的枚举,可以在这些情况下使用.

JSON.NET has an enum called DefaultValueHandling, which can be used in these situations.

ServiceStack有类似的东西吗?

Does ServiceStack have something similar?

例如:

class Person
{
   string Name { get; set; }
   Address Address { get; set; }
}

[ComplexType]
class Address
{
   string Street { get; set; }
   int Number { get; set; }
   int PostalCode { get; set; }
}

当我序列化一个没有地址的人时,我得到了:

When I serialize a person that doesn't have address I get this:

"{ Name: Jim, Address : { Number: 0, PostalCode: 0 } }" 

在Json.Net中,如果我将DefaultValueHandling设置为Ignore,我只会得到

In Json.Net if I set DefaultValueHandling to Ignore, I only get

 "{ Name: Jim }"

推荐答案

是的,您可以通过以下不同方式

Yes, here are the different ways you can ignore properties with ServiceStack's JSON and Text serializers.

序列化程序还支持多个挂钩,以自定义序列化和反序列化.

JsConfig 类显示所有可能的自定义.

The JsConfig class shows all the customizations that are possible.

这篇关于有没有一种方法可以在使用ServiceStack json进行序列化时忽略默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆