Skip to content

Commit 2e5d6aa

Browse files
committed
Added documentation for "Serializers"
1 parent 36849f9 commit 2e5d6aa

5 files changed

Lines changed: 192 additions & 0 deletions

File tree

docs/serializers/.pages

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
nav:
2+
- index.md
3+
- json.md
4+
- bson.md
5+
- xml.md

docs/serializers/bson.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# BSON
2+
3+
BSON (Binary JSON) serialization is available in the .NET implementation of TypedRest.
4+
5+
=== "C#"
6+
7+
The BSON serializer provides efficient binary serialization using Newtonsoft.Json's BSON support:
8+
9+
```csharp
10+
using TypedRest.Serializers;
11+
12+
var serializer = new BsonSerializer();
13+
var endpoint = new EntryEndpoint(
14+
new Uri("http://example.com/"),
15+
serializer: serializer);
16+
```
17+
18+
The BSON serializer uses the same Newtonsoft.Json settings as the default JSON serializer:
19+
20+
- Camel-case property naming
21+
- String enums with camel-case naming
22+
- Null values are not serialized
23+
- Automatic type name handling
24+
25+
## Content type
26+
27+
TODO: The serializer handles the `application/bson` content type.

docs/serializers/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
11
# Serializers
22

33
Serializers control how entities are serialized when sending requests and deserialized when receiving responses.
4+
5+
TypedRest provides built-in serializers for various transport formats:
6+
7+
- [JSON](json.md)
8+
- [BSON](bson.md)
9+
- [XML](xml.md)
10+
11+
You can also create and use custom serializers.
12+
13+
## Inheritance
14+
15+
When creating [endpoints](../endpoints/index.md), the serializer is automatically inherited from the parent (referrer) endpoint:
16+
17+
=== "C#"
18+
19+
```csharp
20+
var client = new EntryEndpoint(new Uri("http://example.com/"));
21+
client.Serializer = new SystemTextJsonSerializer();
22+
23+
var contacts = new CollectionEndpoint<Contact>(client, relativeUri: "./contacts");
24+
// contacts.Serializer is automatically set to the same SystemTextJsonSerializer
25+
```
26+
27+
=== "TypeScript"
28+
29+
```typescript
30+
const client = new EntryEndpoint(new URL("http://example.com/"));
31+
client.serializer = new CustomSerializer();
32+
33+
const contacts = new CollectionEndpoint<Contact>(client, "./contacts");
34+
// contacts.serializer is automatically set to the same CustomSerializer
35+
```
36+
37+
This inheritance ensures consistent serialization behavior across your entire endpoint hierarchy.

docs/serializers/json.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# JSON
2+
3+
JSON is the default serialization format in TypedRest.
4+
5+
=== "C#"
6+
7+
### Newtonsoft.Json (Default)
8+
9+
The default serializer uses [Newtonsoft.Json](https://www.newtonsoft.com/json) with the following settings:
10+
11+
- Camel-case property naming
12+
- String enums with camel-case naming
13+
- Null values are not serialized
14+
- Automatic type name handling
15+
16+
```csharp
17+
var endpoint = new EntryEndpoint(new Uri("http://example.com/"));
18+
// Uses NewtonsoftJsonSerializer by default
19+
```
20+
21+
To customize the serializer settings:
22+
23+
```csharp
24+
var serializer = new NewtonsoftJsonSerializer();
25+
serializer.SerializerSettings.DateFormatString = "yyyy-MM-dd";
26+
27+
var endpoint = new EntryEndpoint(
28+
new Uri("http://example.com/"),
29+
serializer: serializer);
30+
```
31+
32+
### System.Text.Json
33+
34+
You can also use the `System.Text.Json` serializer with the `TypedRest.SystemTextJson` NuGet package:
35+
36+
!!! note
37+
The `TypedRest.SystemTextJson` package version should match your main `TypedRest` package version. Both packages follow the same versioning scheme.
38+
39+
Default settings:
40+
41+
- Web defaults (camel-case property naming)
42+
- Null values are not serialized when writing
43+
44+
Basic usage:
45+
46+
```csharp
47+
using TypedRest.Serializers;
48+
49+
var serializer = new SystemTextJsonSerializer();
50+
var endpoint = new EntryEndpoint(
51+
new Uri("http://example.com/"),
52+
serializer: serializer);
53+
```
54+
55+
To customize the serializer options:
56+
57+
```csharp
58+
var serializer = new SystemTextJsonSerializer();
59+
serializer.Options.WriteIndented = true;
60+
serializer.Options.Converters.Add(new JsonStringEnumConverter());
61+
62+
var endpoint = new EntryEndpoint(
63+
new Uri("http://example.com/"),
64+
serializer: serializer);
65+
```
66+
67+
=== "TypeScript"
68+
69+
TypeScript uses the native `JSON.stringify()` and `JSON.parse()` methods:
70+
71+
```typescript
72+
const endpoint = new EntryEndpoint(new URL("http://example.com/"));
73+
// Uses JsonSerializer by default
74+
```
75+
76+
### Custom Serializers
77+
78+
You can implement the `Serializer` interface for custom serialization:
79+
80+
```typescript
81+
import { Serializer } from "typedrest";
82+
83+
class MySerializer implements Serializer {
84+
readonly supportedMediaTypes = ["application/json"];
85+
86+
serialize<T>(entity: T): string {
87+
// Custom serialization logic
88+
return JSON.stringify(entity);
89+
}
90+
91+
deserialize<T>(text: string): T {
92+
// Custom deserialization logic
93+
return JSON.parse(text) as T;
94+
}
95+
}
96+
97+
const endpoint = new EntryEndpoint(
98+
new URL("http://example.com/"),
99+
new MySerializer());
100+
```
101+
102+
## Content type
103+
104+
TODO: TypedRest automatically handle custom media types that end with `+json` (e.g., `application/vnd.api+json`, `application/hal+json`). These are treated as JSON and deserialized using the configured JSON serializer.

docs/serializers/xml.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# XML
2+
3+
XML serialization is available in the .NET implementation of TypedRest.
4+
5+
=== "C#"
6+
TODO: The XML serializer uses .NET's built-in `System.Xml.Serialization.XmlSerializer`:
7+
8+
```csharp
9+
using TypedRest.Serializers;
10+
11+
var serializer = new XmlSerializer();
12+
var endpoint = new EntryEndpoint(
13+
new Uri("http://example.com/"),
14+
serializer: serializer);
15+
```
16+
17+
## Content type
18+
19+
TODO: The serializer handles the following content types:
20+
21+
- `application/xml`
22+
- `text/xml`

0 commit comments

Comments
 (0)