Rename field to JsonSerializer

I have a class library that I need to output through JsonResult in an ASP.NET MVC environment. (JsonResult uses JsonSerializer to create its output.)

I found, after reading the documentation, that if you put [ScriptIgnore] in a public property / field, it will not be serialized, like [XmlIgnore] for an XML serializer.

I need the equivalent functionality [XmlElement("elementname")] , which fully defines the name of the field / property in the output serialization. I have a field called Elements that needs to be serialized into a field called Elements .

How to do this using default JsonSerializer?

Thanks David

+4
source share
2 answers

Are you using the DataContractJsonSerializer class?

If so...

Add this attribute to the Elements field.

 [DataMember(Name = "elements")] 

This SO> question suggests how to override the use of the JsonScriptSerializer in the JsonDataContractSerializer.

Kindness,

Dan

+2
source

Unhappy answer: you cannot do this. Having said that, I am currently developing a module that extends any object, creating at runtime an anonymous object that will follow the rules from attributes such as JsonIgnore or JsonProperty. I will write more when I have something.

+1
source

All Articles