Skip to content

Serializers

Péter Szrnka edited this page Jun 9, 2021 · 2 revisions

Firebase-hosting-api-java client communicates with Firebase Hosting servers by sending data via HTTP requests. To achieve this, data transfer objects must be serialized to strings. Earlier this library provided 3 built in serializer, but from version 0.6 you must create your own serializer by implementing io.github.szrnkapeter.firebase.hosting.serializer.Serializer interface. To help your work I provide you 3 ready-to-go implementations, which use the most popular JSON serializers.

GSON

Import (Maven)

<properties>
	...
	<gson.version>2.8.6</gson.version>
	...
</properties>
...
<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>${gson.version}</version>
	<scope>test</scope>
</dependency>

Code

import com.google.gson.Gson;

/**
 * Gson based serializer/deserializer example.
 * 
 * @author Peter Szrnka
 * @since 0.6
 */
public class GsonSerializer implements Serializer {

	/*
	 * (non-Javadoc)
	 * @see hu.szrnkapeter.firebase.hosting.serializer.Serializer#getObject(java.lang.Class, java.lang.String)
	 */
	@Override
	public <T> T getObject(Class<T> clazz, String obj) throws Exception {
		return new Gson().fromJson(obj, clazz);
	}

	/*
	 * (non-Javadoc)
	 * @see io.github.szrnkapeter.firebase.hosting.serializer.Serializer#toJson(java.lang.Class, java.lang.Object)
	 */
	@Override
	public <T> String toJson(Class<T> clazz, T obj) throws Exception {
		return new Gson().toJson(obj);
	}
}

Moshi

Import (Maven)

<properties>
	...
	<moshi.version>1.11.0</moshi.version>
	...
</properties>
...
<dependency>
	<groupId>com.squareup.moshi</groupId>
	<artifactId>moshi</artifactId>
	<version>${moshi.version}</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>com.squareup.moshi</groupId>
	<artifactId>moshi-adapters</artifactId>
	<version>${moshi.version}</version>
	<scope>test</scope>
</dependency>

Code

import java.util.Date;

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter;

/**
 * Moshi based serializer/deserializer.
 * 
 * @author Peter Szrnka
 * @since 0.6
 * 
 */
public class MoshiSerializer implements Serializer {

	/*
	 * (non-Javadoc)
	 * @see hu.szrnkapeter.firebase.hosting.serializer.Serializer#getObject(java.lang.Class, java.lang.String)
	 */
	@Override
	public <T> T getObject(Class<T> clazz, String obj) throws Exception {
		return getAdapter(clazz).fromJson(obj);
	}

	/*
	 * (non-Javadoc)
	 * @see io.github.szrnkapeter.firebase.hosting.serializer.Serializer#toJson(java.lang.Class, java.lang.Object)
	 */
	@Override
	public <T> String toJson(Class<T> clazz, T obj) throws Exception {
		return getAdapter(clazz).toJson(obj);
	}
	
	private <T> JsonAdapter<T> getAdapter(Class<T> clazz) {
		Moshi moshi = new Moshi.Builder().add(Date.class, new Rfc3339DateJsonAdapter()).build();
		return moshi.adapter(clazz);
	}
}

Jackson

Import (Maven)

<properties>
	...
	<jackson-databind.version>2.11.1</jackson-databind.version>
	...
</properties>
...
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>${jackson-databind.version}</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.datatype</groupId>
	<artifactId>jackson-datatype-jsr310</artifactId>
	<version>${jackson-databind.version}</version>
</dependency>

Code

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Jackson based serializer/deserializer.
 * 
 * @author Peter Szrnka
 * @since 0.6
 * 
 */
public class JacksonSerializer implements Serializer {

	/*
	 * (non-Javadoc)
	 * @see hu.szrnkapeter.firebase.hosting.serializer.Serializer#getObject(java.lang.Class, java.lang.String)
	 */
	@Override
	public <T> T getObject(Class<T> clazz, String obj) throws Exception {
		return getObjectMapper().readValue(obj, clazz);
	}

	/*
	 * (non-Javadoc)
	 * @see io.github.szrnkapeter.firebase.hosting.serializer.Serializer#toJson(java.lang.Class, java.lang.Object)
	 */
	@Override
	public <T> String toJson(Class<T> clazz, T obj) throws Exception {
		return getObjectMapper().writeValueAsString(obj);
	}
	
	private ObjectMapper getObjectMapper() {
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
		return objectMapper;
	}
}
Clone this wiki locally