uk.co.autotrader.traverson.conversion.FastJsonResourceConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of traverson4j-core Show documentation
Show all versions of traverson4j-core Show documentation
The kernel of traverson4j. This provides the main API for a client to traverse a Hypermedia REST service
The newest version!
package uk.co.autotrader.traverson.conversion;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import uk.co.autotrader.traverson.exception.ConversionException;
import java.io.InputStream;
class FastJsonResourceConverter implements ResourceConverter {
@Override
public Class getDestinationType() {
return JSONObject.class;
}
@Override
public JSONObject convert(InputStream resource, Class extends JSONObject> returnType) {
String resourceAsString = null;
try {
resourceAsString = new StringResourceConverter().convert(resource, String.class);
if (resourceAsString.isEmpty()) {
return null;
}
return JSON.parseObject(resourceAsString);
} catch (JSONException ex) {
throw new ConversionException("Failed to parse to JSONObject", resourceAsString, ex);
}
}
}