All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.co.autotrader.traverson.conversion.FastJsonResourceConverter Maven / Gradle / Ivy

Go to download

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 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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy