Is there a way to ignore default values during ser

2019-02-24 12:12发布

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 has an enum called DefaultValueHandling, which can be used in these situations.

Does ServiceStack have something similar?

For example:

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 } }" 

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

 "{ Name: Jim }"

2条回答
放我归山
2楼-- · 2019-02-24 12:47

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

The serializers also support multiple hooks to customize serialization and deserialization.

The JsConfig class shows all the customizations that are possible.

查看更多
Emotional °昔
3楼-- · 2019-02-24 13:03

Please consider changing your value types to nullable data types and set null as default value for any reference type.

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; }
}

This should help you get rid of attributes with default values as ServiceStack Text will omit properties with null value. Also observe that 'Age' which is of type int? is omitted from serialized output when it is null. The example also demonstrates serialization using anonymous objects.

Example below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack;

namespace JsonTest
{
    class Person
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public int? Age { get; set; }
        public List<Person> Children { get; set; }

        public Person()
        {
            Children = new List<Person>();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var c1 = new Person { Name = "John", Address = "USA", Age = null };
            var c2 = new Person { Name = "John", Address = "USA", Age = 12 };
            List<Person> children = new List<Person>();
            children.Add(c1);
            string name = "Jim";
            // Uncomment lines below and check  - Children attribute is omitted from JSON result
            // children = null;
            // name = null;
            var p1 = new { Name = name, Address = "USA", Age=40, Children = children};
            var p2 = new Person { Name = "Jim", Address = "USA" , Age = null};
            p2.Children.Add(c2);
            Console.WriteLine(p1.ToJson());
            Console.WriteLine(p2.ToJson());
            Console.ReadLine();
        }
    }
}

Output:

{"Name":"Jim","Address":"USA","Age":40,"Children":[{"Name":"John","Address":"USA","Children":[]}]}
{"Name":"Jim","Address":"USA","Children":[{"Name":"John","Address":"USA","Age":12,"Children":[]}]}

{"Address":"USA","Age":40}
{"Name":"Jim","Address":"USA","Children":[{"Name":"John","Address":"USA","Age":12,"Children":[]}]}
查看更多
登录 后发表回答