|
| 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 | + ``` |
0 commit comments