diff --git a/docs/usage/options.md b/docs/usage/options.md index 287c1c52f1..e2e099e31e 100644 --- a/docs/usage/options.md +++ b/docs/usage/options.md @@ -78,27 +78,26 @@ To limit the maximum depth of nested includes, use `MaximumIncludeDepth`. This i options.MaximumIncludeDepth = 1; ``` -## Custom Serializer Settings +## Customize Serializer options -We use [Newtonsoft.Json](https://www.newtonsoft.com/json) for all serialization needs. -If you want to change the default serializer settings, you can: +We use [System.Text.Json](https://www.nuget.org/packages/System.Text.Json) for all serialization needs. +If you want to change the default serializer options, you can: ```c# -options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; -options.SerializerSettings.Converters.Add(new StringEnumConverter()); -options.SerializerSettings.Formatting = Formatting.Indented; +options.SerializerOptions.WriteIndented = true; +options.SerializerOptions.ReferenceHandler = ReferenceHandler.Preserve; +options.SerializerOptions.Converters.Add(new JsonStringEnumConverter()); ``` The default naming convention (as used in the routes and resource/attribute/relationship names) is also determined here, and can be changed (default is camel-case): ```c# -options.SerializerSettings.ContractResolver = new DefaultContractResolver -{ - NamingStrategy = new KebabCaseNamingStrategy() -}; +// Use Pascal case +options.SerializerOptions.PropertyNamingPolicy = null; +options.SerializerOptions.DictionaryKeyPolicy = null; ``` -Because we copy resource properties into an intermediate object before serialization, Newtonsoft.Json annotations on properties are ignored. +Because we copy resource properties into an intermediate object before serialization, JSON annotations such as `[JsonPropertyName]` and `[JsonIgnore]` on `[Attr]` properties are ignored. ## Enable ModelState Validation diff --git a/docs/usage/resource-graph.md b/docs/usage/resource-graph.md index c77c842429..36e424d6e0 100644 --- a/docs/usage/resource-graph.md +++ b/docs/usage/resource-graph.md @@ -98,4 +98,4 @@ public class MyModel : Identifiable } ``` -The default naming convention can be changed in [options](~/usage/options.md#custom-serializer-settings). +The default naming convention can be changed in [options](~/usage/options.md#customize-serializer-options). diff --git a/docs/usage/resources/attributes.md b/docs/usage/resources/attributes.md index 6e24ab964f..6a42bae7e0 100644 --- a/docs/usage/resources/attributes.md +++ b/docs/usage/resources/attributes.md @@ -14,7 +14,7 @@ public class Person : Identifiable There are two ways the exposed attribute name is determined: -1. Using the configured [naming convention](~/usage/options.md#custom-serializer-settings). +1. Using the configured [naming convention](~/usage/options.md#customize-serializer-options). 2. Individually using the attribute's constructor. ```c# @@ -88,9 +88,9 @@ public class Person : Identifiable ## Complex Attributes Models may contain complex attributes. -Serialization of these types is done by [Newtonsoft.Json](https://www.newtonsoft.com/json), -so you should use their APIs to specify serialization formats. -You can also use global options to specify `JsonSerializer` configuration. +Serialization of these types is done by [System.Text.Json](https://www.nuget.org/packages/System.Text.Json), +so you should use their APIs to specify serialization format. +You can also use [global options](~/usage/options.md#customize-serializer-options) to control the `JsonSerializer` behavior. ```c# public class Foo : Identifiable @@ -101,7 +101,8 @@ public class Foo : Identifiable public class Bar { - [JsonProperty("compound-member")] + [JsonPropertyName("compound-member")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string CompoundMember { get; set; } } ``` @@ -121,13 +122,13 @@ public class Foo : Identifiable { get { - return Bar == null ? "{}" : JsonConvert.SerializeObject(Bar); + return Bar == null ? "{}" : JsonSerializer.Serialize(Bar); } set { Bar = string.IsNullOrWhiteSpace(value) ? null - : JsonConvert.DeserializeObject(value); + : JsonSerializer.Deserialize(value); } } } diff --git a/docs/usage/resources/relationships.md b/docs/usage/resources/relationships.md index 869b38f97c..aada8841bb 100644 --- a/docs/usage/resources/relationships.md +++ b/docs/usage/resources/relationships.md @@ -38,7 +38,7 @@ The left side of this relationship is of type `Person` (public name: "persons") There are two ways the exposed relationship name is determined: -1. Using the configured [naming convention](~/usage/options.md#custom-serializer-settings). +1. Using the configured [naming convention](~/usage/options.md#customize-serializer-options). 2. Individually using the attribute's constructor. ```c# diff --git a/docs/usage/routing.md b/docs/usage/routing.md index 314e2bdfb1..0a10831d9b 100644 --- a/docs/usage/routing.md +++ b/docs/usage/routing.md @@ -45,7 +45,7 @@ The exposed name of the resource ([which can be customized](~/usage/resource-gra ### Non-JSON:API controllers -If a controller does not inherit from `JsonApiController`, the [configured naming convention](~/usage/options.md#custom-serializer-settings) is applied to the name of the controller. +If a controller does not inherit from `JsonApiController`, the [configured naming convention](~/usage/options.md#customize-serializer-options) is applied to the name of the controller. ```c# public class OrderLineController : ControllerBase