Ignoring null fields in Json.net

60,095

Solution 1

Yes you need to use JsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore.

But because structs are value types you need to mark Field2, Field3 nullable to get the expected result:

public struct structA
{
    public string Field1;
    public structB? Field2;
    public structB? Field3;
}

Or just use classes instead of structs.

Documentation: NullValueHandling Enumeration

Solution 2

You can also apply the JsonProperty attribute to the relevant properties and set the null value handling that way. Refer to the Reference property in the example below:

Note: The JsonSerializerSettings will override the attributes.

public class Person
{
    public int Id { get; set; }
    
    [JsonProperty( NullValueHandling = NullValueHandling.Ignore )]
    public int? Reference { get; set; }

    public string Name { get; set; }
}
Share:
60,095
Thaven
Author by

Thaven

Just standard coder. Faktycznie nie masz twardych dowodów na poparcie swojej tezy ;). Z kontrolkami jest malutki problem - w wielu systemach chcesz mieć "klasyczny grid" w którym 80% funkcjonalności jest wspólnych dla wszystkich ekranów, a rózni się te 20%. Z Twojego opisu wynika że Szefo-Recyklinator postanowił zrobić kontrolkę która da zarówno te 80%, jak i WSZYSTKIE z tych 20%, więc sumarczynie da jakies z 200% albo ile tam. Z kolei z artykułu wynika że Ty optujesz za zrobieniem oddzielnej kontrolki dla każdego z tych ekranów, co z kolei dorpowadzi do tego że 80% kodu do jej osbsługi w każdym będzie redundantne - co też będzie piekłem w urzymaniu Nie pamiętam jak to wyglądało z dziedziczeniem kontrolek w WPF-ie, ale słusznym podejściem wydaje się zidentyfikować te 80%, z tego zrobić kontrolkę bazową, a funkcje charakterystyczne dla poszczególnych ekranów implementować w kontrolkach pochodnych od niej. Ewentualnie znaleść inne mechanizmy pozwalające "wtłoczyć" wymagane w danym ekranie funkcjonalności do kontrolki - jest ich trochę do wyboru. w DRY nie chodzi o to żeby zrobić jeden megakomponent z milionem przełączników - to jakaś potworność :P. W DRY chodzi o to żeby coś co jest wspólne - takie faktycznie było, żeby w przypadku modyfikacji modyfikowac jedno miejsce. takie milon-przełącznikowa konrolka łamie z kolei zasadę KISS ;)

Updated on July 05, 2022

Comments

  • Thaven
    Thaven almost 2 years

    I have some data that I have to serialize to JSON. I'm using JSON.NET. My code structure is similar to this:

    public struct structA
    {
        public string Field1;
        public structB Field2;
        public structB Field3;
    }
    
    public struct structB
    {
        public string Subfield1;
        public string Subfield2;
    }
    

    Problem is, my JSON output needs to have ONLY Field1 OR Field2 OR Field3 - it depends on which field is used (i.e. not null). By default, my JSON looks like this:

    {
        "Field1": null,
        "Field2": {"Subfield1": "test1", "Subfield2": "test2"},
        "Field3": {"Subfield1": null, "Subfield2": null},
    }
    

    I know I can use NullValueHandling.Ignore, but that gives me JSON that looks like this:

    {
        "Field2": {"Subfield1": "test1", "Subfield2": "test2"},
        "Field3": {}
    }
    

    And what I need is this:

    {
        "Field2": {"Subfield1": "test1", "Subfield2": "test2"},
    }
    

    Is there simple way to achieve this?

  • Mickey Perlstein
    Mickey Perlstein almost 12 years