Skip to content

Commit 450a3fa

Browse files
committed
Improved serializer docs
1 parent df47c88 commit 450a3fa

2 files changed

Lines changed: 109 additions & 8 deletions

File tree

docs/serializers/custom.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Implement the platform's `Serializer` interface (or appropriate base type):
88

99
=== "C#"
1010

11-
On .NET, TypedRest uses `MediaTypeFormatter` from the `System.Net.Http.Formatting` family. Derive from `MediaTypeFormatter` (or one of its specializations) and override the read/write methods.
11+
TypedRest uses `MediaTypeFormatter` from the `System.Net.Http.Formatting` family. Derive from `MediaTypeFormatter` (or one of its specializations) and override the read/write methods.
1212

1313
```csharp
1414
using System.Net.Http.Formatting;
@@ -106,10 +106,10 @@ Implement the platform's `Serializer` interface (or appropriate base type):
106106

107107
=== "TypeScript"
108108

109-
Implement the `Serializer` interface from the `typedrest` package:
109+
Implement the `Serializer` interface:
110110

111111
```typescript
112-
import { Serializer } from "typedrest";
112+
import { Serializer } from "typedrest/serializers";
113113

114114
class MySerializer implements Serializer {
115115
readonly supportedMediaTypes = ["application/my-format"];

docs/serializers/json.md

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ JSON is the default serialization format in TypedRest.
1414
- Automatic type name handling
1515

1616
```csharp
17-
var endpoint = new EntryEndpoint(new Uri("http://example.com/"));
18-
// Uses NewtonsoftJsonSerializer by default
17+
var endpoint = new EntryEndpoint(new Uri("http://example.com/")); // Uses NewtonsoftJsonSerializer by default
1918
```
2019

2120
To customize the serializer settings:
@@ -63,13 +62,115 @@ JSON is the default serialization format in TypedRest.
6362
});
6463
```
6564

65+
=== "Java"
66+
67+
**kotlinx.serialization (Default)**
68+
69+
The default serializer uses [kotlinx.serialization](https://kotlinlang.org/docs/serialization.html). Entity classes are typically defined in Kotlin with the `@Serializable` annotation.
70+
71+
```java
72+
EntryEndpoint endpoint = new EntryEndpoint(URI.create("http://example.com/")); // Uses KotlinxJsonSerializer by default
73+
```
74+
75+
**Jackson**
76+
77+
For Java POJOs or more control over serialization, add the [typedrest-serializers-jackson](https://central.sonatype.com/artifact/net.typedrest/typedrest-serializers-jackson) dependency and pass a `JacksonJsonSerializer`:
78+
79+
```java
80+
EntryEndpoint endpoint = new EntryEndpoint(URI.create("http://example.com/"), null, new JacksonJsonSerializer());
81+
```
82+
83+
To customize the `JsonMapper`:
84+
85+
```java
86+
JsonMapper mapper = JsonMapper.builder()
87+
.addModule(new KotlinModule.Builder().build())
88+
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
89+
.build();
90+
EntryEndpoint endpoint = new EntryEndpoint(URI.create("http://example.com/"), null, new JacksonJsonSerializer(mapper));
91+
```
92+
93+
**Moshi**
94+
95+
Add the [typedrest-serializers-moshi](https://central.sonatype.com/artifact/net.typedrest/typedrest-serializers-moshi) dependency and pass a `MoshiJsonSerializer`:
96+
97+
```java
98+
EntryEndpoint endpoint = new EntryEndpoint(URI.create("http://example.com/"), null, new MoshiJsonSerializer());
99+
```
100+
101+
=== "Kotlin"
102+
103+
**kotlinx.serialization (Default)**
104+
105+
The default serializer uses [kotlinx.serialization](https://kotlinlang.org/docs/serialization.html). Annotate entity classes with `@Serializable`:
106+
107+
```kotlin
108+
import kotlinx.serialization.Serializable
109+
110+
@Serializable
111+
data class Contact(val name: String)
112+
```
113+
114+
```kotlin
115+
val endpoint = EntryEndpoint(URI.create("http://example.com/")) // Uses KotlinxJsonSerializer by default
116+
```
117+
118+
**Jackson**
119+
120+
Add the [typedrest-serializers-jackson](https://central.sonatype.com/artifact/net.typedrest/typedrest-serializers-jackson) dependency and pass a `JacksonJsonSerializer`:
121+
122+
```kotlin
123+
val endpoint = EntryEndpoint(
124+
URI.create("http://example.com/"),
125+
serializer = JacksonJsonSerializer()
126+
)
127+
```
128+
129+
To customize the `JsonMapper`:
130+
131+
```kotlin
132+
val mapper = JsonMapper.builder()
133+
.addModule(kotlinModule())
134+
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
135+
.build()
136+
val endpoint = EntryEndpoint(
137+
URI.create("http://example.com/"),
138+
serializer = JacksonJsonSerializer(mapper)
139+
)
140+
```
141+
142+
**Moshi**
143+
144+
Add the [typedrest-serializers-moshi](https://central.sonatype.com/artifact/net.typedrest/typedrest-serializers-moshi) dependency and pass a `MoshiJsonSerializer`:
145+
146+
```kotlin
147+
val endpoint = EntryEndpoint(
148+
URI.create("http://example.com/"),
149+
serializer = MoshiJsonSerializer()
150+
)
151+
```
152+
153+
To customize the `Moshi` instance:
154+
155+
```kotlin
156+
val moshi = Moshi.Builder()
157+
.add(KotlinJsonAdapterFactory())
158+
.add(Date::class.java, Rfc3339DateJsonAdapter())
159+
.build()
160+
val endpoint = EntryEndpoint(
161+
URI.create("http://example.com/"),
162+
serializer = MoshiJsonSerializer(moshi)
163+
)
164+
```
165+
66166
=== "TypeScript"
67167

68-
TypeScript uses the native `JSON.stringify()` and `JSON.parse()` methods:
168+
**Native JSON (Default)**
169+
170+
The default serializer uses the native `JSON.stringify()` and `JSON.parse()` methods.
69171

70172
```typescript
71-
const endpoint = new EntryEndpoint(new URL("http://example.com/"));
72-
// Uses JsonSerializer by default
173+
const endpoint = new EntryEndpoint(new URL("http://example.com/")); // Uses JsonSerializer by default
73174
```
74175

75176
## Content type

0 commit comments

Comments
 (0)