com.ingenico.connect.gateway.sdk.java.defaultimpl.DefaultMarshaller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connect-sdk-java Show documentation
Show all versions of connect-sdk-java Show documentation
SDK to communicate with the Ingenico ePayments platform using the Ingenico Connect Server API
package com.ingenico.connect.gateway.sdk.java.defaultimpl;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ingenico.connect.gateway.sdk.java.Marshaller;
import com.ingenico.connect.gateway.sdk.java.MarshallerSyntaxException;
/**
* {@link Marshaller} implementation based on {@link Gson}.
*/
public class DefaultMarshaller implements Marshaller {
// Gson instances are thread-safe, so reuse one single instance
private static final Gson GSON = new Gson();
private static final Charset CHARSET = Charset.forName("UTF-8");
public static final DefaultMarshaller INSTANCE = new DefaultMarshaller();
protected DefaultMarshaller() {}
@Override
public String marshal(Object requestObject) {
return GSON.toJson(requestObject);
}
@Override
public T unmarshal(String responseJson, Class type) {
try {
return GSON.fromJson(responseJson, type);
} catch (JsonSyntaxException e) {
throw new MarshallerSyntaxException(e);
}
}
@Override
public T unmarshal(InputStream responseJson, Class type) {
try {
return GSON.fromJson(new InputStreamReader(responseJson, CHARSET), type);
} catch (JsonSyntaxException e) {
throw new MarshallerSyntaxException(e);
}
}
}