com.mocean.modules.ResponseFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moceanapisdk Show documentation
Show all versions of moceanapisdk Show documentation
This is an Mocean SDK written in java. To use it you will need a mocean account. Signup for free at
https://moceanapi.com
package com.mocean.modules;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mocean.exception.MoceanErrorException;
import javax.xml.bind.JAXB;
import java.io.IOException;
import java.io.StringReader;
public class ResponseFactory {
private ResponseFactory() {
}
public static T createObjectFromRawResponse(String rawResponse, Class type) throws MoceanErrorException {
//first check whether is json
try {
return new ObjectMapper().readValue(rawResponse, type);
} catch (IOException e) {
//format is not json, try xml now
try {
return JAXB.unmarshal(new StringReader(rawResponse), type);
} catch (Exception ex) {
throw new MoceanErrorException("unable to parse response, " + rawResponse);
}
}
}
}