2

Is there any way to ignore null fields while serializing POCO to JSON string using Utf8Json library?

I have a ToString method in my below class which I am using externally so I wanted to see if there is any way to exclude null fields while doing serialization? Basically I don't want null fields in my json string after serialization. I am using Uft8Json library here.

public class Process
{
    public Process() { }

    [DataMember(Name = "lang_code")]
    public string LCode { get; set; }

    [DataMember(Name = "data_currency")]
    public string Currency { get; set; }

    [DataMember(Name = "country_code")]
    public string CCode { get; set; }

    public override string ToString()
    {
        return Utf8Json.JsonSerializer.ToJsonString(this);
    }
}

I tried reading the docs but couldn't find it. Is there any way to do this using Utf8Json library?

1 Answer 1

2

Use resolver:

return Utf8Json.JsonSerializer.ToJsonString(this, 
    Utf8Json.Resolvers.StandardResolver.ExcludeNull);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.