3

I have a class that I serialize to JSON in C# and post to a RESTful web service. I have a requirment that if one field is filled out another field not be present. The service errors if both fields are serialized into the JSON object. My class looks like this:

    [DataContract(Name = "test-object")]
public class TestObject
{
    [DataMember(Name = "name")]
    public string Name { get; set; }

    // If string-value is not null or whitespace do not serialize bool-value
    [DataMember(Name = "bool-value")]
    public bool BoolValue { get; set; }

    // If string-value is null or whitespace do not serialize it
    [DataMember(Name = "string-value")]
    public string StringValue { get; set; }
}

As noted in the comments, if StringValue has a value don't put BoolValue in the JSON object. If StringValue is blank, don't put in StringValue but instead put in BoolValue.

I found how to do this with XML serialization, but cannot find a way this works with JSON serialization. Is there conditional JSON serialization on C#?

3
  • this question is not quite "similar", but the answer might apply to your case. stackoverflow.com/a/29173426/526704 Commented Mar 20, 2015 at 18:36
  • What are you using to serialize Newtonsoft or what's included in .NET ? Maybe there are events you can hook into each time a property is serialized and skip by observing what's been serialized at that point. Commented Mar 20, 2015 at 20:49
  • I'm using what is built into .Net. Commented Mar 20, 2015 at 21:09

1 Answer 1

3

It appears you are using the DataContractJsonSerializer. In that case, you can:

  1. Disable direct serialization of your properties with the attribute [IgnoreDataMember].
  2. Create proxy string and bool? properties for serialization that return null when they should not be serialized. These can be private.
  3. Set [DataMember(EmitDefaultValue=false)] on these proxy properties to suppress output of nulls.

Thus:

[DataContract(Name = "test-object")]
public class TestObject
{
    [DataMember(Name = "name")]
    public string Name { get; set; }

    [IgnoreDataMember]
    public bool BoolValue { get; set; }

    [IgnoreDataMember]
    public string StringValue { get; set; }

    bool ShouldSerializeStringValue()
    {
        return !String.IsNullOrWhiteSpace(StringValue);
    }

    // If string-value is not null or whitespace do not serialize bool-value
    [DataMember(Name = "bool-value", EmitDefaultValue=false)]
    bool? SerializedBoolValue {
        get
        {
            if (!ShouldSerializeStringValue())
                return BoolValue;
            return null;
        }
        set
        {
            BoolValue = (value ?? false); // Or don't set it at all if value is null - your choice.
        }
    }

    // If string-value is null or whitespace do not serialize it
    [DataMember(Name = "string-value", EmitDefaultValue=false)]
    string SerializedStringValue {
        get
        {
            if (ShouldSerializeStringValue())
                return StringValue;
            return null;
        }
        set
        {
            StringValue = value;
        }
    }
}

Incidentally, this will also work with Json.NET, which respects data contract attributes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.