|
| 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. |
0 commit comments