Copyright
이 모든 내용은 Pluralsight에 Xavier Morera가 올린 'Getting Started with JSON in C# Using Json.NET'라는 강의의 세번째 챕터를 듣고 정리한 것입니다(https://app.pluralsight.com/library/courses/json-csharp-jsondotnet-getting-started/table-of-contents).
Content
Outline
Settings and Attributes
Settings
Attributes
Settings and Attributes
A setting is a user preference that is supplied during the conversion process. It can be specified as a property on the JsonSerializer class or using the JsonSerializer settings on JsonConvert.
An attribute is a declarative tag that is supplied on classes, properties, and more, that is taking into account during the serialization and deserialization process.
Settings
DateFormatHandling
With DateFormatHandling you tell Json.NET how to handle dates. e.g., the ISO date format or the Microsoft date format.
MissingMemberHandling
With MissingMemberHandling you tell Json.NET what to do when the JSON contains a member that is not defined. You can ignore or you can raise an error.
ReferenceLoopHandling
With ReferenceLoopHandling you tell Json.NET what to do when there is an object that references itself. You can ignore, raise an error, or serialize.
NullValueHandling
With NullValueHandling you tell Json.NET what to do when it runs into null values, both on serialization and deserialization.
DefaultValueHandling
With DefaultValueHandling you specify how to use the default values that are set using the DefaultValue attribute. [DefaultValue(3)] int defaultAgeIsThree;
Can ignore property which was set to default value when Serializing
Can populate default value to a property when Deserializing
ObjectCreationHandling
With ObjectCreationHandling you tell Json.NET how to handle objects that are created during the deserialization. By default, Json.NET sets values and appends values to existing collections. This might be the desired behavior in some cases, but in others it might not. You can specify if you want to reuse or replace the objects or collections that are set. This is particularly useful when you have constructors that populate values before the deserialization process.
TypeNameHandling
TypeNameHandling is very important because it tells Json.NET to preserve type information that's very useful when you're serializing and deserializing.
TypeNameAssemblyFormat
With TypeNameAssembly you tell Json.NET how you want type names written during the serialization process.
Binder
With Binder you tell Json.NET how to resolve type names to .NET types.
MetadataPropertyHandling
ConstructorHandling
ConstructorHandling is a way of telling Json.NET to specify which constructor to use, even if it's not a public constructor.
Converters
Converters is a way of telling Json.NET which converters you want to use during the deserialization and serialization process.
ContractResolver
With ContractResolver you specify to Json.NET how you want to control the serialization and deserialization without having attributes in your classes.
TraceWriter
TraceWriter is used for logging and debugging, and with Error you specify to Json.NET how is it that you want to handle errors.
Error
With Error you specify to Json.NET how is it that you want to handle errors.
Attributes
Attributes are declarative tags that are placed on classes, properties, and more and they provide additional information to Json.NET on how to do the serialization and deserialization process.
in Json.NET
JsonObjectAttribute
The JsonObjectAttribute is placed on classes to tell Json.NET how to serialize as a JsonObject.
JsonArrayAttribute
The JsonArrayAttribute is placed in collections and it tells Json.NET to serialize as a JsonArray.
JsonDictionaryAttribute
The JsonDictionaryAttribute is placed in dictionaries and it tells Json.NET how they should be serialized as JSON objects.
JsonPropertyAttribute
The JsonPropertyAttribute is used in fields and properties to control how they're serialized as properties in JsonObjects.
+ [JsonProperty(PropertyName = "AuthorName", Required = Required.Always, Order = 2)]
public string name { get; set; }
If Json.NET try to serialize the class without value of above property(name) throws an error - Useful when a specific member to be set before serialize.
And Serialized PropertyName set to "AuthorName". Setting PropertyName can be implicit like below "WhereInTheWorld"
+ [JsonProperty("WhereInTheWorld", DefaultValueHandling = DefaultValueHandling.Ignore)]
[DefaultValue("Costa Rica")]
public string location { get; set; }
if location property is set to "Costa Rica" just as same as DefaultValue above, then DefaultValueHandling.Ignore Attribute makes Json.NET not to serialize location property at all. It's useful to save some bytes.
JsonIgnoreAttribute
JsonIgnore tells Json.NET to ignore and do not include a property during serialization.
MemberSerialization OptIn, OptOut, Fields
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] //Only JsonProperty attached member is serialized
[JsonObject(MemberSerialization = MemberSerialization.OptOut)] //Just looking for which ones to ignore
[JsonObject(MemberSerialization = MemberSerialization.Field)] //Add some string on autogenerated field
public class AuthorJsonObjectOptIn
{
private string privateField;
[JsonProperty] private string privateFieldWithAttribute; //private field also serialized
[JsonProperty] public string name { get; set; } //Only this two member get serialize
public string[] courses { get; set; }
public DateTime since;
[NonSerialized] public bool happy; //Ignore this two member and serialize else including private member
[JsonIgnoreAttribute] public object issues { get; set; }
}
JsonConverterAttribute
The JsonConverter can be placed on classes, fields, or properties and it tells Json.NET to use a specific JsonConverter during the serialization process.
e.g. [JsonConverter(typeof(StringEnumConverter))] public Relationship relationship { get; set; } //Relationship is of type enum
if no Converter here, Json.NET serialize relationship(of type enum) as string like 1 or 2...
But with this converter it uses text in enum
JsonExtensionDataAttribute
The JsonExtensionDataAttribute is placed on a collection field or property and it tells Json.NET to use this as a catchall bucket to include any properties that do not have any matching class members.
JsonConstructorAttribute
The JsonConstructor attribute is placed on a constructor and it tells Json.NET to use this constructor during deserialization.
in Standard .NET Attributes
SerializableAttribute
The SerializableAttribute is used to indicate that a class can be serialized.
DataContractAttribute
The DataContractAttribute is used to specify that the type or class implements a data contract and is serializable.
DataMemberAttribute
The DataMemberAttribute which when applied to a member of a type specifies that the member is part of a data contract.
NonSerializedAttribute
NonSerializedAttribute which tells Json.NET that a particular field should not be serialized.
출처
이 모든 내용은 Pluralsight에 Xavier Morera가 올린 'Getting Started with JSON in C# Using Json.NET'라는 강의의 세번째 챕터를 듣고 정리한 것입니다(https://app.pluralsight.com/library/courses/json-csharp-jsondotnet-getting-started/table-of-contents). 제가 정리한 것보다 더 많은 내용과 Demo를 포함하고 있으며 최종 Summary는 생략하겠습니다. Microsoft 지원을 통해 한달간 무료로 Pluralsight의 강의를 들으실 수도 있습니다.
'Programming > etc' 카테고리의 다른 글
(Json.NET) Performance Tips (0) | 2018.03.30 |
---|---|
(Json.NET) Custom Serialization (0) | 2018.03.30 |
(Json.NET) Serialization Fundamentals (0) | 2018.03.30 |