Skip to content

About

JsonApiX is an Android, annotation processor library that was made to transform regular Kotlin classes into their JSON API representations, with the ability to serialize or deserialize them to or from strings.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

46 stars

Watchers

9 watching

Forks

Latest commit

 

History

611 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Maven Central Version

JsonApiX

Description

  • JSON API X is an Android, annotation processor library with the intention of extending it to a KMP library in due time
  • Implements a parser between Kotlin classes and JSON API specification strings in both directions
  • Includes Retrofit module for easy API implementations

Table of contents

Requirements

The library uses KAPT for annotation processing. The setup is different on a Kotlin or Java project.

Getting started

Firstly, make sure to include mavenCentral() in your buildscript and add the serialization plugin:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion")
    }
}

Then add the following dependencies and enable kapt and kotlinx-serialization plugins:

plugins {
    id "kotlinx-serialization"
    id "kotlin-kapt" or id "com.android.legacy-kapt" -> depends on your AGP version
}
// Serialization API. Check the docs link above for newer versions
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0")

// Json API X
implementation("com.infinum.jsonapix:core:<latest-version>")

// Kotlin project
kapt("com.infinum.jsonapix:processor:<latest-version>")

// Java project
annotationProcessor "com.infinum.jsonapix:processor:<latest-version>"

// Optional: For Retrofit support
implementation("com.infinum.jsonapix:retrofit:<latest-version>")

// Optional: Custom lint tool checker
implementation("com.infinum.jsonapix:lint:<latest-version>")

Usage

Let's take the example of a class named Person.

@Serializable
@JsonApiX(type = "person")
data class Person(
    val firstName: String,
    val lastName: String,
    val age: Int,
    @HasMany(type = "friend")
    val friends: List<Friend>,
    @HasOne(type = "dog")
    val dog: Dog?
)

@JsonApiX annotation takes a String type parameter and it must be combined with @Serializable from KotlinX Serialization. Type is later used as a value for the type key in the JSON API specification.

@HasOne and @HasMany indicate the class relationships. They also take the type parameter, that must be the same as the one indicated in the class definition.

In this case, Dog must also be annotated with @Serializable and @JsonApiX(type = "dog"), where "dog" is the type defined for the Dog class.

All non-primitive fields of a class should be some kind of relationship.

To-one relationships

A @HasOne property can be declared in one of two shapes, and the declared type is what decides which one the property uses.

@HasOne(type = "dog")
val dog: Dog?                 // legacy: null means "leave this relationship alone"

@HasOne(type = "dog")
val dog: Relationship<Dog>    // all four states

The nullable shape cannot say "clear this relationship". JSON API reads an absent relationships member as "leave it alone" and {"data": null} as "remove it", and forbids a server from treating the first as the second, but a single nullable property collapses both into null. Relationship<Dog> separates them:

State Written as Means
Relationship.Unset member left out leave this relationship alone
Relationship.Unlink "dog": {"data": null} remove the relationship
Relationship.Linked(ResourceIdentifier("dog", "1")) "dog": {"data": {"type": "dog", "id": "1"}} point at a resource, without sending it in included
Relationship.Included(dog) "dog": {"data": {...}} plus included point at a resource and send it
@Serializable
@JsonApiX(type = "owner")
data class Owner(
    val name: String,
    @HasOne(type = "dog")
    val myFavoriteDog: Relationship<Dog> = Relationship.Unset,
) : JsonApiModel()

api.update(OwnerModel(data = owner.copyModel(myFavoriteDog = Relationship.Unlink)))
api.update(OwnerModel(data = owner.linkMyFavoriteDog(id = "1")))

Use copyModel, not copy

A model's JSON API type and id live in private fields on JsonApiModel and are set after construction, so they are not constructor parameters. copy rebuilds the model through its constructor and leaves both behind, and the copy then serializes through the id() ?: "0" fallback. The request goes out claiming to be resource 0 instead of failing, so nothing tells you it happened:

owner.copy(myFavoriteDog = Relationship.Unlink)      // -> "id": "0"
owner.copyModel(myFavoriteDog = Relationship.Unlink) // -> "id": "5"

copyModel is generated for every annotated model. It takes the same named arguments as copy, defaults each to the current value, and carries the type and id across. This is not specific to relationships: copy loses the identity when you change an attribute too. Models that are not data classes have no copy at all and have always needed this.

The lint module ships a JsonApiXCopyLosesIdentity check that flags copy on a JsonApiModel and offers to replace it with copyModel.

linkMyFavoriteDog is generated for every Relationship property, named link plus the property name. It exists because the JSON API type comes off the @HasOne annotation, so the generated code can fill it in and the call site does not have to repeat it. It delegates to copyModel, so it carries the identity too.

Read the related model back with value, which gives the model for an Included and null for the other three states:

val dog: Dog? = owner.myFavoriteDog.value

Deserialization produces Included when the linkage resolves against included, and Linked when it does not, so a relationship whose resource was not included survives instead of being read as null. Everything else reads as Unset, including a member whose data is null: a response cannot ask a client to clear anything, and a relationship object carrying only links or meta says nothing about the linkage, so reading either as Unlink would turn an ordinary response into a request that deletes the relationship on the next write.

The nullable shape keeps working exactly as before and produces byte-identical output. It is legacy, and will be dropped in the next major release.

To-many relationships are unchanged. An empty list already means "clear" for those.

Once you hit build, PersonModel, PersonItem, and PersonList are automatically generated. These wrappers facilitate the serialization and deserialization of your classes into and from JSON API strings.

For example, for a single Person instance, PersonModel is generated as follows:

public data class PersonModel(
  public val `data`: Person, // The Person object
  // ... Root values
  // ... Resource object values
)
public data class PersonItem(
  public val `data`: Person, // Individual Person object within the list
  // ... Resource object values
)
public data class PersonList(
  public val `data`: List<PersonItem>, // List of PersonItem objects
  // ... Root values
)

To access the serialization and deserialization features, you need to use the TypeAdapter interface. To get the type adapter for your specific class, use the generated TypeAdapterFactory:

// Gets adapter for a single instance of Person
val adapter = TypeAdapterFactory().getAdapter(PersonModel::class)

// Gets adapter for a list of Person instances
val listAdapter = TypeAdapterFactory().getAdapter(PersonList::class)

adapter.convertToString(personModel) // Produces JSON API String from a PersonModel instance
adapter.convertFromString(inputJsonString) // Produces PersonModel instance from JSON API String

listAdapter.convertToString(personList) // Produces JSON API String from a PersonList
listAdapter.convertFromString(inputJsonString) // Produces PersonList from JSON API String

Nullability

JsonApiX relies on the included JSON array from the JSON API specification when deserializing relationships. If the input JSON API string is missing the data of a non-nullable relationship in the included array, deserialization attempt will throw a JsonApiXMissingArgumentException. On the other hand, nullable relationships will be evaluated as null.

Errors

According to the JSON API specification, each object has an optional error array. The following interface is used as a root-level wrapper for JSON API responses. It contains a nullable errors list, that is used to parse the errors.

interface JsonApiX<out Model> {
    val data: ResourceObject<Model>?
    val included: List<ResourceObject<*>>?
    val errors: List<Error>?
    val links: Links?
    val meta: Meta?
}

A single error is modeled to wrap the most common arguments of an error. Developer needs to make sure to model the error responses to match this model.

@Serializable
@SerialName("error")
class DefaultError(
    val code: String,
    val title: String,
    val detail: String,
    val status: String
)

When using Retrofit, in the event of a network error a HttpException will be thrown. To extract the DefaultError model from a response, you can use the HttpException.asJsonXHttpException() extension. The extension will return a JsonXHttpException, containing the original response as well as errors list.

// Using DefaultError
try {
    val person = io { sampleApiService.fetchPerson() }
} catch (exception: HttpException) {
    val jsonXHttpException = exception.asJsonXHttpException()
    val errors = jsonXHttpException.errors // List<DefaultError>
    // handle errors
    ...
}

Custom error

Developers can define their own custom error models to adapt to the specific requirements.

Let's take this custom person error model as an example. Every custom error model should extend the Error interface and have a JsonApiXError annotation set.

@Serializable
@JsonApiXError(type = "person")
data class PersonalError(
    val desc: String
) : Error

In this example, the annotation processor will automatically make the error type of a Person class to be a PersonError and use it as a type when deserializing errors array. Developer needs to make sure that the type parameter value in JsonApiXError matches the one in the JsonApiX above the original model.

To extract a custom error model (ex. PersonalError), clients should use a generic asJsonXHttpException in a similar way as explained in the previous chapter for DefaultError.

// Using custom error
try {
    val person = io { sampleApiService.fetchPerson() }
} catch (exception: HttpException) {
    val jsonXHttpException = exception.asJsonXHttpException<PersonError>()
    val errors = jsonXHttpException.errors // List<PersonError>
    // handle errors
    ...
}

JsonApiModel - Handling links and meta JSON API fields

JSON API specification includes resources that are not necessarily a part of the original models. JsonApiX provides a way to retrieve the links and meta values from the JSON API input, without including those fields in your model.

To achieve this, your model need to extend JsonApiModel abstract class.

@Serializable
@JsonApiX("person")
data class Person(
    val name: String?,
    val surname: String,
    val age: Int,
    @HasOne("dog")
    val myFavoriteDog: Dog? = null
) : JsonApiModel()

JsonApiModel is an abstract class that provides getters and setters for id and type.

The generated PersonModel and PersonList classes come with default meta and links, but we've provided options for customization. This allows you to tailor these elements to fit your specific project needs, enhancing flexibility and control.

Links

Links can be included in the root object, relationships objects and resource objects (data key). By default, they have the following implementation:

class DefaultLinks(
    val self: String? = null,
    val related: String? = null,
    val first: String? = null,
    val last: String? = null,
    val next: String? = null,
    val prev: String? = null
) : Links

And the can be retrieved in the following way:

// Get root level links
personModel.rootLinks

// Get relationships links
personModel.relationshipsLinks

// Get resource object links
personModel.resourceObjectLinks
Custom links

Developers can define their own custom link models to adapt to the specific requirements.

Let's take this custom person links model as an example. Every custom links model must extend the Links interface and have a JsonApiXLinks annotation.

@Serializable
@JsonApiXLinks(type = "person", placementStrategy = LinksPlacementStrategy.ROOT)
data class PersonLinks(
    val bioLink: String,
    val socialLink: String
) : Links

LinksPlacementStartegy enum is used to determine which links from the whole JSON API object will be replaced by a custom model. It currently supports ROOT, RELATIONSHIPS and DATA links.

Meta

In JSON API specification, Meta is an optional, free-format key-value pair. It doesn't have a predefined default model. For that reason, in order to use the meta feature, developer must define a custom meta model for each class, where he wants to use it.

Let's take this PersonMeta model as an example. Every custom meta model must extend the Meta interface and have a JsonApiXMeta annotation.

@Serializable
@JsonApiXMeta(type = "person")
data class PersonMeta(val owner: String) : Meta

In this example, the annotation processor will automatically make the meta type of a PersonModel class to be PersonMeta. Developer needs to make sure that the type parameter value in JsonApiXMeta matches the one in the JsonApiX above the original model. To retrieve a meta object, a generic variant of the meta() method is used.

// Gets meta object
person.rootMeta
person.relationshipsMeta
person.resourceObjectMeta

Retrofit Support

To enable Retrofit support, you need to add our custom converter in your retrofit builder. It takes an instance of TypeAdapterFactory as a parameter. No additional steps needed.

Retrofit.Builder()
    .addConverterFactory(JsonXConverterFactory(TypeAdapterFactory()))
    .baseUrl("https://www.example.com")
    .build()

Request bodies

The converter writes a request document, not a full JSON API document. included and errors are top level members that the JSON API specification defines for responses only, so they are left out, and so is every member whose value is null. A model field that was never set is indistinguishable from one set to null, and sending it as null would tell the server to clear the value.

The one null that survives is the data member of a relationship object, which is structural: {"data": null} is how JSON API spells "clear this relationship". Only a Relationship.Unlink produces one.

{
  "data": {
    "type": "person",
    "id": "10",
    "attributes": {
      "name": "Frank"
    },
    "relationships": {
      "myFavoriteDog": {
        "data": {
          "type": "dog",
          "id": "1"
        }
      }
    }
  }
}

TypeAdapter.convertToString is unaffected and still writes the full document. If you build request bodies yourself, call String.toJsonApiRequestDocument() on its output to get the same shape.

Request Content-Type

Request bodies are sent as application/json; charset=UTF-8.

The JSON API specification defines its own media type, application/vnd.api+json, and servers are free to answer 415 Unsupported Media Type to anything else. If your server is one of them, pass it to the converter factory:

Retrofit.Builder()
    .addConverterFactory(JsonXConverterFactory(TypeAdapterFactory(), JsonXMediaTypes.JSON_API))
    .baseUrl("https://www.example.com")
    .build()

Note that JsonXMediaTypes.JSON_API carries no charset parameter, on purpose: the specification has servers answer 415 to application/vnd.api+json with any media type parameter. Any MediaType of your own is sent verbatim, so leave the charset off yours too.

List of libraries used in the project

This chapter lists all the libraries that make a bigger contribution to the project. It is essential for any future contributor to get familiar with them, before starting any work on the project. For that reason, links to the documentation are a part of the list.

Along with the libraries documentation, the developer should also know his way around the JSON API, so here is a link to the specification.

Contributing

We believe that the community can help us improve and build better a product. Please refer to our contributing guide to learn about the types of contributions we accept and the process for submitting them.

To ensure that our community remains respectful and professional, we defined a code of conduct that we expect all contributors to follow.

We appreciate your interest and look forward to your contributions.

License

Copyright 2024 Infinum

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Credits

Maintained and sponsored by Infinum.

About

JsonApiX is an Android, annotation processor library that was made to transform regular Kotlin classes into their JSON API representations, with the ability to serialize or deserialize them to or from strings.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

46 stars

Watchers

9 watching

Forks

Releases

Packages

Used by

Contributors

Languages