Skip to content

Commit 8bca806

Browse files
committed
Added documentation for "Serializers"
1 parent d7f3e31 commit 8bca806

6 files changed

Lines changed: 169 additions & 1 deletion

File tree

docs/.pages

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ nav:
44
- setup
55
- endpoints
66
- serializers
7-
- error-handling
87
- link-handling
8+
- error-handling
99
- code-generation
1010
- imprint.md

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# BSON
2+
3+
=== "C#"
4+
5+
The BSON serializer provides efficient binary serialization using [Newtonsoft.Json](https://www.newtonsoft.com/json)'s BSON support:
6+
7+
```csharp
8+
var endpoint = new EntryEndpoint(
9+
new Uri("http://example.com/"),
10+
serializer: new BsonSerializer());
11+
```
12+
13+
The BSON serializer uses the same Newtonsoft.Json settings as the default JSON serializer:
14+
15+
- Camel-case property naming
16+
- String enums with camel-case naming
17+
- Null values are not serialized
18+
- Automatic type name handling

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: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 endpoint = new EntryEndpoint(
25+
new Uri("http://example.com/"),
26+
serializer: new NewtonsoftJsonSerializer
27+
{
28+
SerializerSettings =
29+
{
30+
DateFormatString = "yyyy-MM-dd"
31+
}
32+
});
33+
```
34+
35+
**System.Text.Json**
36+
37+
You can also use the [System.Text.Json](https://learn.microsoft.com/en-us/dotnet/api/system.text.json) serializer with the [TypedRest.SystemTextJson](https://www.nuget.org/packages/TypedRest.SystemTextJson) NuGet package:
38+
39+
!!! note
40+
The `TypedRest.SystemTextJson` package version should match your main `TypedRest` package version. Both packages follow the same versioning scheme.
41+
42+
Default settings:
43+
44+
- Web defaults (camel-case property naming)
45+
- Null values are not serialized when writing
46+
47+
Basic usage:
48+
49+
```csharp
50+
var endpoint = new EntryEndpoint(
51+
new Uri("http://example.com/"),
52+
serializer: new SystemTextJsonSerializer());
53+
```
54+
55+
To customize the serializer options:
56+
57+
```csharp
58+
var endpoint = new EntryEndpoint(
59+
new Uri("http://example.com/"),
60+
serializer: new SystemTextJsonSerializer
61+
{
62+
Options =
63+
{
64+
WriteIndented = true,
65+
Converters = {new JsonStringEnumConverter() }
66+
}
67+
});
68+
```
69+
70+
=== "TypeScript"
71+
72+
TypeScript uses the native `JSON.stringify()` and `JSON.parse()` methods:
73+
74+
```typescript
75+
const endpoint = new EntryEndpoint(new URL("http://example.com/"));
76+
// Uses JsonSerializer by default
77+
```
78+
79+
**Custom serializers**
80+
81+
You can implement the `Serializer` interface for custom serialization:
82+
83+
```typescript
84+
import { Serializer } from "typedrest";
85+
86+
class MySerializer implements Serializer {
87+
readonly supportedMediaTypes = ["application/json"];
88+
89+
serialize<T>(entity: T): string {
90+
// Custom serialization logic
91+
return JSON.stringify(entity);
92+
}
93+
94+
deserialize<T>(text: string): T {
95+
// Custom deserialization logic
96+
return JSON.parse(text) as T;
97+
}
98+
}
99+
100+
const endpoint = new EntryEndpoint(
101+
new URL("http://example.com/"),
102+
new MySerializer());
103+
```

docs/serializers/xml.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# XML
2+
3+
=== "C#"
4+
```csharp
5+
var endpoint = new EntryEndpoint(
6+
new Uri("http://example.com/"),
7+
serializer: new XmlSerializer());
8+
```

0 commit comments

Comments
 (0)