data
when printing to the .json file. I would like to change the property names to be something different (say, change 'foo' to 'bar').[JsonProperty]
attribute which allows you to specify a different name:MyCustomObject
, that has a property called LongPropertyName
, you can use a custom resolver like this…[JSonObject]
or [JsonProperty]
.CamelCaseNamingStrategy
, but you can implement your own ones.enum
property, and upon serializing the object using JavaScriptSerializer
, my json result contains the integer value of the enumeration rather than its string
'name'. Is there a way to get the enum as a string
in my json without having to create a custom JavaScriptConverter
? Perhaps there's an attribute that I could decorate the enum
Hmi software, free download. definition, or object property, with?JavaScriptSerializer
serializes enums
to their numeric values and not their string representation. You would need to use custom serialization to serialize the enum
as its name instead of numeric value.JavaScriptSerializer
than see answer on this question provided by OmerBakhari: JSON.net covers this use case (via the attribute [JsonConverter(typeof(StringEnumConverter))]
) and many others not handled by the built in .net serializers. Here is a link comparing features and functionalities of the serializers.StringEnumConverter
attribute:StringEnumConverter
documentation.ScriptIgnore
attribute to the Gender
property, causing it to not be serialised, and adding a GenderString
property which does get serialised:<select>
element. This works for both simple enums and bitflag enums.<select>
drop-down. Response.AsJson()
helper method - but don't worry, you can use any standard JSON formatter as the enum has already been projected into a simple anonymous type ready for serialization.JsonSerializer
if you don't want to use JsonConverter
attribute:enum
it sees during that serialization.Newtonsoft.Json
library. It fixes the enum issue and also makes the error handling much better, and it works in IIS hosted services. It's quite a lot of code, so you can find it on GitHub here: https://github.com/jongrant/wcfjsonserializer/blob/master/NewtonsoftJsonFormatter.csWeb.config
to get it to work, you can see an example file here:https://github.com/jongrant/wcfjsonserializer/blob/master/Web.configStringEnumConverter
to make sure that our enum values could expand over time without breaking catastrophically on the deserializing side (see background below). Using the SafeEnumConverter
below allows deserialization to finish even if the payload contains a value for the enum that does not have a named definition, closer to how int-to-enum conversion would work.StringEnumConverter
, the problem we had is that we also needed passivity for cases when a new enum value was added, but not every client was immediately aware of the new value. In these cases, the StringEnumConverter
packaged with Newtonsoft JSON throws a JsonSerializationException
similar to 'Error converting value SomeString to type EnumType' and then the whole deserialization process fails. This was a deal breaker for us, because even if the client planned on ignoring/discarding the property value that it didn't understand, it still needed to be capable of deserializing the rest of the payload!