4

I have a class library that I need to output via a JsonResult in the ASP.NET MVC framework. (JsonResult uses the JsonSerializer to produce its output.)

I discovered through reading the documentation that if you put [ScriptIgnore] on a public property/field, it will not get serialized, much like [XmlIgnore] for the XML serializer.

I need the equivalent functionality of [XmlElement("elementname")], which specifies absolutely the name of the field/property in the output serialization. I have a field called Elements that needs to get serialized to a field named elements.

How can I accomplish this using the default JsonSerializer?

Thanks, David

Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
  • I'm confused why this would get downvoted. Its pretty relevant to anyone who wants to have a strongly-typed class library decoupled from what gets spit out to JavaScript. – David Pfeffer Dec 11 '09 at 15:53
  • From the MSDN documentation on this serializer, it doesn't appear any more attributes apply to it. This is -very- disappointing if it genuinely doesn't support it. My use case in this instance is there is a JSON.net compatible implementation of the OpenFlashChart2 object model, and I wanted to just annotate the fields with [ScriptIgnore] and whatever attribute this question yielded and then submit back to the OpenFlashChart2 team as a working ASP.NET MVC compatible library. Now it looks like I'm going to have to wrap the classes in an ASP.NET MVC wrapper with poorly named public fields. – David Pfeffer Dec 11 '09 at 16:00
  • David did you ever end up creating an ASP.NET MVC library for use with OpenFlashChart2? – runxc1 Bret Ferrier Mar 31 '11 at 18:41

3 Answers3

2

Are you using the DataContractJsonSerializer class?

If so ...

Add this attribute to you Elements field

[DataMember(Name = "elements")] 

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

Kindness,

Dan

Community
  • 1
  • 1
Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
  • No, I am using the JsonSerializer. As I mentioned, it is required because it is what JsonResult uses in the MVC framework. – David Pfeffer Dec 11 '09 at 15:35
  • 2
    Added link to SO question explaining a route to use differenet (more configurable) Json serializer. – Daniel Elliott Dec 11 '09 at 15:44
  • Still not what I'm looking for, but this answers the question now and is really helpful. I might end up being forced down this road if no one can answer the question. Thanks! – David Pfeffer Dec 11 '09 at 15:52
1

The unfortunate answer is, you cannot do it. Having said that, I am currently developing a module that will extend any object by producing at runtime an anonymous object that will follow rules from attributes, such as JsonIgnore or JsonProperty. I'll post more when I have something.

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
-1

The correct attribute for this is JsonPropertyName.

Sora2455
  • 744
  • 7
  • 25
  • You're answering this question 10 years after it was originally asked. This question is not about .NET Core 3.0's built in `JsonSerializer`, which of course did not exist in 2009. It is about the built-in `JsonSerializer` in now-obsolete ASP.NET MVC 2.0, prior to them switching to JSON.NET as the default serializer. – David Pfeffer Nov 11 '19 at 03:03