3

I have a model that i retrieve as json and as xml.

I want to seralize different properties for Json that for xml.

[Serlializable]
class Model
{
     public bool? IncludeAlways {get;set;}

     [XmlIgnore]
     public bool? IncludeForJson {get;set;}

     [JsonIgnore]
     public bool? IncludeForXml {get;set;}
}

Sample use:

new Model()
{
    IncludeAlways = true,
    IncludeForJson = true,
    IncludeForXml = true
};

It results the expected Xml:

<Model>
    <IncludeAlways>true</IncludeAlways>
    <IncludeForXml>true</IncludeForXml>
</Model>

but not the expected json

I expected some json is like this:

{ "IncludeAlways":true, "IncludeForJson":true }

but i have the "IgnoreForXml" property is

{ "IncludeAlways":true }

Using .Net Framework 2.0 Newtonsoft JSON.Net for json and .Net Framework 2.0 standard XmlSerializer for xml. The xml is generated as a result of a WebMethod and i asume that is serialized with the standard XmlSerializer that serialize to xml.

4
  • You're expecting XML/JSON to output strings despite of having bool? declared? Commented Mar 27, 2015 at 12:55
  • Since XML and JSON are interchangable in JSON.NET, I think JsonConvert checks for both XmlIgnore and JsonIgnore when serializing. Why do you distinguish them anyway? Commented Mar 27, 2015 at 12:58
  • 2
    The Xml and Json are for different pourposes and want to reuse the model is used for both pourposes. I look for a settings feature that configure the attributes to use to ignore or something alike. Commented Mar 28, 2015 at 11:19
  • Possible duplicate of Serialize .Net object to json, controlled using xml attributes Commented Aug 17, 2017 at 23:43

0

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.