Skip to content

Commit

Permalink
Updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Koelman committed Sep 14, 2021
1 parent e2c94d3 commit 1ed87ae
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
21 changes: 10 additions & 11 deletions docs/usage/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/resource-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
15 changes: 8 additions & 7 deletions docs/usage/resources/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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#
Expand Down Expand Up @@ -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
Expand All @@ -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; }
}
```
Expand All @@ -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<Bar>(value);
: JsonSerializer.Deserialize<Bar>(value);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/resources/relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -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#
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TResource>`, 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<TResource>`, the [configured naming convention](~/usage/options.md#customize-serializer-options) is applied to the name of the controller.

```c#
public class OrderLineController : ControllerBase
Expand Down

0 comments on commit 1ed87ae

Please sign in to comment.