@@ -12,134 +12,67 @@ Serializers control how entities are serialized when sending requests and deseri
1212| XML | ✅ | ❌ |
1313| BSON | ✅ | ❌ |
1414
15- ## .NET Serializers
15+ ## Serializer Types
1616
17- ### JSON with Newtonsoft.Json (Default )
17+ ### [ JSON] ( json.md )
1818
19- The default serializer uses Newtonsoft.Json with the following settings :
19+ JSON is the default serialization format in both TypedRest implementations :
2020
21- - Camel-case property naming
22- - String enums with camel-case naming
23- - Null values are not serialized
24- - Automatic type name handling
21+ - ** .NET** : Uses [ Newtonsoft.Json] ( https://www.newtonsoft.com/json ) by default, with optional support for System.Text.Json via a separate NuGet package
22+ - ** TypeScript** : Uses native ` JSON.stringify() ` and ` JSON.parse() `
2523
26- ``` csharp
27- var endpoint = new EntryEndpoint (new Uri (" http://example.com/" ));
28- // Uses NewtonsoftJsonSerializer by default
29- ```
24+ Both implementations automatically handle custom JSON media types with the ` +json ` suffix (e.g., ` application/vnd.api+json ` , ` application/hal+json ` ).
3025
31- To customize:
26+ ### [ XML ] ( xml.md )
3227
33- ``` csharp
34- var serializer = new NewtonsoftJsonSerializer ();
35- serializer .SerializerSettings .DateFormatString = " yyyy-MM-dd" ;
28+ XML serialization is available in .NET using the built-in ` System.Xml.Serialization.XmlSerializer ` . TypeScript does not support XML serialization.
3629
37- var endpoint = new EntryEndpoint (new Uri (" http://example.com/" ), serializer : serializer );
38- ```
30+ ### [ BSON] ( bson.md )
3931
40- ### JSON with System.Text.Json
32+ BSON (Binary JSON) serialization is available in .NET for efficient binary serialization. TypeScript does not support BSON serialization.
4133
42- For better performance, you can use the System.Text.Json serializer from the ` TypedRest.SystemTextJson ` NuGet package:
34+ ## Multiple Serializers
4335
44- ``` bash
45- dotnet add package TypedRest. SystemTextJson
46- ```
36+ In .NET, you can provide multiple serializers for content negotiation:
4737
48- ``` csharp
49- using TypedRest . Serializers ;
38+ === "C#"
5039
51- var serializer = new SystemTextJsonSerializer ();
52- var endpoint = new EntryEndpoint (new Uri (" http://example.com/" ), serializer : serializer );
53- ```
40+ ```csharp
41+ var serializers = new MediaTypeFormatter[]
42+ {
43+ new SystemTextJsonSerializer(),
44+ new XmlSerializer()
45+ };
5446
55- Default settings:
47+ var endpoint = new EntryEndpoint(httpClient, serializers);
48+ ```
5649
57- - Web defaults (camel-case property naming)
58- - Null values are not serialized when writing
50+ The first serializer is used for outgoing requests. For incoming responses, the appropriate serializer is selected based on the ` Content-Type ` header.
5951
60- To customize:
52+ ## Serializer Inheritance
6153
62- ``` csharp
63- var serializer = new SystemTextJsonSerializer ();
64- serializer .Options .WriteIndented = true ;
65- serializer .Options .Converters .Add (new JsonStringEnumConverter ());
66- ```
54+ When creating child endpoints, the serializer configuration is automatically inherited from the parent (referrer) endpoint:
6755
68- ### XML
56+ === "C#"
6957
70- For XML serialization:
58+ ```csharp
59+ var client = new EntryEndpoint(new Uri("http://example.com/"));
60+ client.Serializer = new SystemTextJsonSerializer();
7161
72- ``` csharp
73- using TypedRest . Serializers ;
62+ // Child endpoints inherit the serializer
63+ var contacts = new CollectionEndpoint<Contact>(client, relativeUri: "./contacts");
64+ // contacts.Serializer is automatically set to the same SystemTextJsonSerializer
65+ ```
7466
75- var serializer = new XmlSerializer ();
76- var endpoint = new EntryEndpoint (new Uri (" http://example.com/" ), serializer : serializer );
77- ```
67+ === "TypeScript"
7868
79- ### BSON
69+ ```typescript
70+ const client = new EntryEndpoint(new URL("http://example.com/"));
71+ client.serializer = new CustomSerializer();
8072
81- For BSON (Binary JSON) serialization:
73+ // Child endpoints inherit the serializer
74+ const contacts = new CollectionEndpoint<Contact>(client, "./contacts");
75+ // contacts.serializer is automatically set to the same CustomSerializer
76+ ```
8277
83- ``` csharp
84- using TypedRest .Serializers ;
85-
86- var serializer = new BsonSerializer ();
87- var endpoint = new EntryEndpoint (new Uri (" http://example.com/" ), serializer : serializer );
88- ```
89-
90- BSON uses the same Newtonsoft.Json settings as the default JSON serializer.
91-
92- ### Multiple Serializers
93-
94- You can provide multiple serializers for content negotiation:
95-
96- ``` csharp
97- var serializers = new MediaTypeFormatter []
98- {
99- new SystemTextJsonSerializer (),
100- new XmlSerializer ()
101- };
102-
103- var endpoint = new EntryEndpoint (httpClient , serializers );
104- ```
105-
106- The first serializer is used for outgoing requests. For incoming responses, the appropriate serializer is selected based on the ` Content-Type ` header.
107-
108- ## TypeScript Serializers
109-
110- ### JSON (Default)
111-
112- TypeScript uses the native ` JSON. stringify() ` and ` JSON.parse() ` methods:
113-
114- ``` typescript
115- const endpoint = new EntryEndpoint (new URL (" http://example.com/" ));
116- // Uses JsonSerializer by default
117- ```
118-
119- ### Custom Serializers
120-
121- You can implement the ` Serializer ` interface for custom serialization:
122-
123- ``` typescript
124- import { Serializer } from " typedrest" ;
125-
126- class MySerializer implements Serializer {
127- readonly supportedMediaTypes = [" application/json" ];
128-
129- serialize<T >(entity : T ): string {
130- // Custom serialization logic
131- return JSON .stringify (entity );
132- }
133-
134- deserialize<T >(text : string ): T {
135- // Custom deserialization logic
136- return JSON . parse (text ) as T ;
137- }
138- }
139-
140- const endpoint = new EntryEndpoint (new URL (" http://example.com/" ), new MySerializer ());
141- ```
142-
143- ## Custom JSON Media Types
144-
145- Both .NET and TypeScript automatically handle custom media types that end with ` +json ` (e.g., ` application/vnd.api+json ` ). These are treated as JSON and deserialized using the configured JSON serializer.
78+ This inheritance ensures consistent serialization behavior across your entire endpoint hierarchy.
0 commit comments