0

I am using the JSON.Net library. I have some JSON which looks like this:

"Templates": [
    {
        "Name": "Default Fields 1",
        "Fields": [16, 10, 0, 4, 5, 11, 12, 7, 15, 17, 9, 25, 3],
        "Formats": [
            "string", "datetime", "LeftZeroPaddedString13", "2dp", "2dp", "2dp", "2dp", "int", "int",
            "string", "int", "int", "int", "int"
        ]
    },
    {
        "Name": "Default Fields 2",
        "Fields": [3, 25, 9, 17, 15, 7, 12, 11, 5, 4, 0, 10, 16],
        "Formats": [
            "int", "int", "int", "int", "string", "int", "int", "2dp", "2dp", "2dp", "2dp", "LeftZeroPaddedString13",
            "datetime", "string"
        ]
    }]

And I am deserializing it to the Templates property within the following class:

public class Options
{
    public List<FieldTemplate> Templates { get; set; }
}
public class FieldTemplate
{
    public string Name { get; set; }
    public List<int> Fields { get; set; }
    public List<string> Formats { get; set; }
}

This works fine, but under some circumstances (not always) I want to prevent the Fields and Formats properties from being included when I serialize the object again (though I do want to keep the Name property in the serialized output). I have thought about using ShouldSerializeFields() and ShouldSerializeFormats() within FieldTemplate and then looping through the object to set a boolean property which each of these methods can read, but it doesn't seem elegant. Is there a better way? For example, I could set boolean properties in Options which FieldTemplate could then use. I don't know how to do that though, or even if its possible.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
mulllhausen
  • 4,225
  • 7
  • 49
  • 71
  • You can use a custom contract resolver to do this. See Andrew Whitaker's answer in [Dynamic json serialization filtering via reflection](http://stackoverflow.com/q/28606326/10263); looks like that should work for your needs. – Brian Rogers Jun 09 '16 at 22:44
  • Thanks @BrianRogers. In the end I took Vitaliy's advice and used a static class as this suits my needs better. I'll post an answer for this. – mulllhausen Jun 10 '16 at 04:27

2 Answers2

0

In the end I used a static class with a switch for each property i wanted to dynamically show/hide:

public class FieldTemplate_JSONSerialization_Switches
{
    public static bool Fields = true;
    public static bool Formats = true;
}

public class Options
{
    public List<FieldTemplate> Templates { get; set; }
}

public class FieldTemplate
{
    public string Name { get; set; }
    public List<int> Fields { get; set; }
    public List<string> Formats { get; set; }

    // instructions to Json.NET
    public bool ShouldSerializeFields()
    {
        return FieldTemplate_JSONSerialization_Switches.Fields;
    }

    public bool ShouldSerializeFormats()
    {
        return FieldTemplate_JSONSerialization_Switches.Formats;
    }
}

Then from elsewhere I can enable/disable these properties:

if (condition)
{
    FieldTemplate_JSONSerialization_Switches.Fields = true;
    FieldTemplate_JSONSerialization_Switches.Formats = false;
}
else
{
    FieldTemplate_JSONSerialization_Switches.Fields = false;
    FieldTemplate_JSONSerialization_Switches.Formats = true;
}
mulllhausen
  • 4,225
  • 7
  • 49
  • 71
-1

Here is similar question and answer.

Json.Net documentation about ConditionalProperties

Edit To not iterate over collection create static object and check it's properties inside ShouldSerializeXYZ().

Community
  • 1
  • 1
  • That's just the `ShouldSerializeXYZ()` that I mentioned in my question. The problem I am having is more about how to conditionally trigger `ShouldSerializeXYZ()` when its within an object in a list. – mulllhausen Jun 09 '16 at 07:03
  • 1
    Ok, my mistake. Then create static object and check it's properties inside ShouldSerializeXYZ() :) – Vitaliy Smolyakov Jun 09 '16 at 07:18