- Newtonsoft Json Serialize Name Examples
- Newtonsoft Json Serialize Namevaluecollection
- Newtonsoft Json Serialize To File
- Newtonsoft Json Serialize Name Generator
Active6 months ago
Json.NET will call that method during serialization to determine whether or not to serialize the corresponding object member. If your method returns true, the member will be serialized; otherwise, it won't. It's totally up to you how you make the decision whether to return true or false. Serializing and Deserializing JSON. The JsonSerializer is able to read and write JSON text directly to a stream via JsonTextReader and JsonTextWriter. Other kinds of JsonWriters can also be used, such as JTokenReader / JTokenWriter, to convert your object to and from LINQ to JSON objects, or BsonReader / BsonWriter, to convert to and from BSON. Json.NET comes with the StringEnumConverter to convert between an enum and the JSON string representation. The property of the ErrorCode enum simply needs to be attributed as a JsonConverter of type StringEnumConverter in order to be serialized and deserialized.
I have some data in a C# DataSet object. I can serialize it right now using a Json.net converter like this
However, this uses the property names from
data
when printing to the .json file. I would like to change the property names to be something different (say, change 'foo' to 'bar').In the Json.net documentation, under 'Serializing and Deserializing JSON' → 'Serialization Attributes' it says 'JsonPropertyAttribute... allows the name to be customized'. But there is no example. Does anyone know how to use a JsonPropertyAttribute to change the property name to something else?
(Direct link to documentation)
Json.net's documentation seems to be sparse. If you have a great example I'll try to get it added to the official documentation.Thanks!
Uwe Keim28.4k3333 gold badges142142 silver badges228228 bronze badges
culixculix
5,38255 gold badges2626 silver badges4747 bronze badges
3 Answers
You could decorate the property you wish controlling its name with the
[JsonProperty]
attribute which allows you to specify a different name:Documentation: Serialization Attributes
Martin Brown17.9k1212 gold badges6565 silver badges100100 bronze badges
Darin DimitrovDarin Dimitrov878k233233 gold badges30763076 silver badges27922792 bronze badges
If you don't have access to the classes to change the properties, or don't want to always use the same rename property, renaming can also be done by creating a custom resolver.
For example, if you have a class called
MyCustomObject
, that has a property called LongPropertyName
, you can use a custom resolver like this…Then call for serialization and supply the resolver:
And the result will be shortened to {'Short':'prop value'} instead of {'LongPropertyName':'prop value'}
More info on custom resolvers here
Luke Girvin11.1k88 gold badges5353 silver badges7575 bronze badges
Newtonsoft Json Serialize Name Examples
StingyJackStingyJackNewtonsoft Json Serialize Namevaluecollection
16.1k77 gold badges5252 silver badges105105 bronze badges
There is still another way to do it, which is using a particular NamingStrategy, which can be applied to a class or a property by decorating them with
[JSonObject]
or [JsonProperty]
.There are predefined naming strategies like
CamelCaseNamingStrategy
, but you can implement your own ones.The implementation of different naming strategies can be found here: https://github.com/JamesNK/Newtonsoft.Json/tree/master/Src/Newtonsoft.Json/Serialization
JotaBeNewtonsoft Json Serialize To File
JotaBe31.2k66 gold badges6969 silver badges9696 bronze badges