Conditional JSON serialization using C# -
i have class serialize json in c# , post restful web service. have requirment if 1 field filled out field not present. service errors if both fields serialized json object. class looks this: [datacontract(name = "test-object")] public class testobject { [datamember(name = "name")] public string name { get; set; } // if string-value not null or whitespace not serialize bool-value [datamember(name = "bool-value")] public bool boolvalue { get; set; } // if string-value null or whitespace not serialize [datamember(name = "string-value")] public string stringvalue { get; set; } } as noted in comments, if stringvalue has value don't put boolvalue in json object. if stringvalue blank, don't put in stringvalue instead put in boolvalue. i found how xml serialization, cannot find way works json serialization. there conditional json serialization on c#? it appears using datacontractjsonse