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.

Iker
  • 31
  • 4
  • You're expecting XML/JSON to output `string`s despite of having `bool?` declared? – Binkan Salaryman Mar 27 '15 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? – Binkan Salaryman Mar 27 '15 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. – Iker Mar 28 '15 at 11:19
  • Possible duplicate of [Serialize .Net object to json, controlled using xml attributes](https://stackoverflow.com/questions/4686817/serialize-net-object-to-json-controlled-using-xml-attributes) – polina-c Aug 17 '17 at 23:43

0 Answers0