JSON¶
JSON is the default serialization format in TypedRest.
Newtonsoft.Json (Default)
The default serializer uses Newtonsoft.Json with the following settings:
- Camel-case property naming
- String enums with camel-case naming
- Null values are not serialized
- Automatic type name handling
var endpoint = new EntryEndpoint(new Uri("http://example.com/")); // Uses NewtonsoftJsonSerializer by default
To customize the serializer settings:
var endpoint = new EntryEndpoint(
new Uri("http://example.com/"),
serializer: new NewtonsoftJsonSerializer
{
SerializerSettings =
{
DateFormatString = "yyyy-MM-dd"
}
});
System.Text.Json
You can also use the System.Text.Json serializer with the TypedRest.SystemTextJson NuGet package. Default settings:
- Web defaults (camel-case property naming)
- Null values are not serialized when writing
Basic usage:
var endpoint = new EntryEndpoint(
new Uri("http://example.com/"),
serializer: new SystemTextJsonSerializer());
To customize the serializer options:
var endpoint = new EntryEndpoint(
new Uri("http://example.com/"),
serializer: new SystemTextJsonSerializer
{
Options =
{
WriteIndented = true,
Converters = {new JsonStringEnumConverter() }
}
});
kotlinx.serialization (Default)
The default serializer uses kotlinx.serialization.
EntryEndpoint endpoint = new EntryEndpoint(URI.create("http://example.com/")); // Uses KotlinxJsonSerializer by default
Not usable with entities written in Java
kotlinx.serialization generates its serializers with a Kotlin compiler plugin, so it only works for entity classes written in Kotlin and annotated with @Serializable. A Java class has no generated serializer, and deserializing one fails at runtime rather than at compile time.
If your entities are Java classes, pick Jackson or Moshi below. Both work by reflection and handle either language.
Jackson
For Java POJOs or more control over serialization, add the typedrest-serializers-jackson dependency and pass a JacksonJsonSerializer:
EntryEndpoint endpoint = new EntryEndpoint(URI.create("http://example.com/"), (HttpCredentials) null, new JacksonJsonSerializer());
To customize the JsonMapper:
JsonMapper mapper = JsonMapper.builder()
.addModule(new KotlinModule.Builder().build())
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build();
EntryEndpoint endpoint = new EntryEndpoint(URI.create("http://example.com/"), (HttpCredentials) null, new JacksonJsonSerializer(mapper));
Moshi
Add the typedrest-serializers-moshi dependency and pass a MoshiJsonSerializer:
EntryEndpoint endpoint = new EntryEndpoint(URI.create("http://example.com/"), (HttpCredentials) null, new MoshiJsonSerializer());
kotlinx.serialization (Default)
The default serializer uses kotlinx.serialization. Annotate entity classes with @Serializable:
import kotlinx.serialization.Serializable
@Serializable
data class Contact(val name: String)
val endpoint = EntryEndpoint(URI.create("http://example.com/")) // Uses KotlinxJsonSerializer by default
Jackson
Add the typedrest-serializers-jackson dependency and pass a JacksonJsonSerializer:
val endpoint = EntryEndpoint(
URI.create("http://example.com/"),
serializer = JacksonJsonSerializer()
)
To customize the JsonMapper:
val mapper = JsonMapper.builder()
.addModule(kotlinModule())
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build()
val endpoint = EntryEndpoint(
URI.create("http://example.com/"),
serializer = JacksonJsonSerializer(mapper)
)
Moshi
Add the typedrest-serializers-moshi dependency and pass a MoshiJsonSerializer:
val endpoint = EntryEndpoint(
URI.create("http://example.com/"),
serializer = MoshiJsonSerializer()
)
To customize the Moshi instance:
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.add(Date::class.java, Rfc3339DateJsonAdapter())
.build()
val endpoint = EntryEndpoint(
URI.create("http://example.com/"),
serializer = MoshiJsonSerializer(moshi)
)
Native JSON (Default)
The default serializer uses the native JSON.stringify() and JSON.parse() methods.
const endpoint = new EntryEndpoint(new URL("http://example.com/")); // Uses JsonSerializer by default
Content type¶
The serializer handles the application/json content type. TypedRest also automatically treats custom media types ending in +json (e.g., application/vnd.api+json, application/hal+json) as JSON and deserializes them using the configured JSON serializer.