io.github.jpmorganchase.fusion.parsing.GsonAPIResponseParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fusion-sdk Show documentation
Show all versions of fusion-sdk Show documentation
A Java SDK for the Fusion platform API
package io.github.jpmorganchase.fusion.parsing;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import io.github.jpmorganchase.fusion.api.response.UploadedPart;
import io.github.jpmorganchase.fusion.model.*;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GsonAPIResponseParser implements APIResponseParser {
private static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final Gson gson;
public GsonAPIResponseParser() {
GsonBuilder gsonBuilder = new GsonBuilder();
// TODO: need to add a serializer as well once we get to update operations
gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateDeserializer());
gson = gsonBuilder.create();
}
public GsonAPIResponseParser(Gson gson) {
this.gson = gson;
}
@Override
public Map parseCatalogResponse(String json) {
return parseResourcesFromResponse(json, Catalog.class);
}
@Override
public Map parseDatasetResponse(String json) {
return parseResourcesFromResponse(json, Dataset.class);
}
@Override
public Map parseAttributeResponse(String json) {
return parseResourcesFromResponse(json, Attribute.class);
}
@Override
public Map parseDataProductResponse(String json) {
return parseResourcesFromResponse(json, DataProduct.class);
}
@Override
public Map parseDatasetSeriesResponse(String json) {
return parseResourcesFromResponse(json, DatasetSeries.class);
}
@Override
public Map parseDistributionResponse(String json) {
return parseResourcesFromResponse(json, Distribution.class);
}
@Override
public Operation parseOperationResponse(String json) {
return gson.fromJson(json, Operation.class);
}
@Override
public UploadedPart parseUploadPartResponse(String json) {
return new GsonBuilder().create().fromJson(json, UploadedPart.class);
}
@Override
public Map parseResourcesFromResponse(String json, Class resourceClass) {
// TODO: handle varArgs
JsonArray resources = getResources(json);
Type listType = TypeToken.getParameterized(List.class, resourceClass).getType();
List resourceList = gson.fromJson(resources, listType);
return resourceList.stream()
.collect(Collectors.toMap(
T::getIdentifier,
Function.identity(),
// resolve any duplicate keys, for now just skip the duplicates
(r1, r2) -> {
logger.warn("Duplicate key '{}' found, will be ignored", r2.getIdentifier());
return r1;
}));
}
@Override
public Map> parseResourcesUntyped(String json) {
Type mapType = new TypeToken
© 2015 - 2025 Weber Informatics LLC | Privacy Policy