Example usage for com.google.gson JsonParseException JsonParseException

List of usage examples for com.google.gson JsonParseException JsonParseException

Introduction

In this page you can find the example usage for com.google.gson JsonParseException JsonParseException.

Prototype

public JsonParseException(Throwable cause) 

Source Link

Document

Creates exception with the specified cause.

Usage

From source file:org.apache.gobblin.runtime.spec_serde.FlowSpecDeserializer.java

License:Apache License

@Override
public FlowSpec deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    JsonObject jsonObject = json.getAsJsonObject();

    String uri = jsonObject.get(FlowSpecSerializer.FLOW_SPEC_URI_KEY).getAsString();
    String version = jsonObject.get(FlowSpecSerializer.FLOW_SPEC_VERSION_KEY).getAsString();
    String description = jsonObject.get(FlowSpecSerializer.FLOW_SPEC_DESCRIPTION_KEY).getAsString();
    Config config = ConfigFactory/*from ww  w.  j av  a 2 s. c  om*/
            .parseString(jsonObject.get(FlowSpecSerializer.FLOW_SPEC_CONFIG_KEY).getAsString());

    Properties properties = new Properties();
    try {
        properties.load(new StringReader(
                jsonObject.get(FlowSpecSerializer.FLOW_SPEC_CONFIG_AS_PROPERTIES_KEY).getAsString()));
    } catch (IOException e) {
        throw new JsonParseException(e);
    }

    Set<URI> templateURIs = new HashSet<>();
    try {
        for (JsonElement template : jsonObject.get(FlowSpecSerializer.FLOW_SPEC_TEMPLATE_URIS_KEY)
                .getAsJsonArray()) {
            templateURIs.add(new URI(template.getAsString()));
        }
    } catch (URISyntaxException e) {
        throw new JsonParseException(e);
    }

    List<Spec> childSpecs = new ArrayList<>();
    for (JsonElement spec : jsonObject.get(FlowSpecSerializer.FLOW_SPEC_CHILD_SPECS_KEY).getAsJsonArray()) {
        childSpecs.add(context.deserialize(spec, FlowSpec.class));
    }

    FlowSpec.Builder builder = FlowSpec.builder(uri).withVersion(version).withDescription(description)
            .withConfig(config).withConfigAsProperties(properties);
    if (!templateURIs.isEmpty()) {
        builder = builder.withTemplates(templateURIs);
    }
    if (!childSpecs.isEmpty()) {
        builder = builder.withChildSpecs(childSpecs);
    }

    return builder.build();
}

From source file:org.apache.gobblin.runtime.spec_serde.FlowSpecSerializer.java

License:Apache License

@Override
public JsonElement serialize(FlowSpec src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject flowSpecJson = new JsonObject();

    flowSpecJson.add(FLOW_SPEC_URI_KEY, context.serialize(src.getUri()));
    flowSpecJson.add(FLOW_SPEC_VERSION_KEY, context.serialize(src.getVersion()));
    flowSpecJson.add(FLOW_SPEC_DESCRIPTION_KEY, context.serialize(src.getDescription()));
    flowSpecJson.add(FLOW_SPEC_CONFIG_KEY,
            context.serialize(src.getConfig().root().render(ConfigRenderOptions.concise())));

    StringWriter writer = new StringWriter();
    try {//from  ww w.j  av  a2  s  .  c  o  m
        src.getConfigAsProperties().store(writer, "");
    } catch (IOException e) {
        throw new JsonParseException(e);
    }
    flowSpecJson.add(FLOW_SPEC_CONFIG_AS_PROPERTIES_KEY, context.serialize(writer.getBuffer().toString()));

    JsonArray templateURIs = new JsonArray();
    if (src.getTemplateURIs().isPresent()) {
        for (URI templateURI : src.getTemplateURIs().get()) {
            templateURIs.add(context.serialize(templateURI));
        }
    }
    flowSpecJson.add(FLOW_SPEC_TEMPLATE_URIS_KEY, templateURIs);

    JsonArray childSpecs = new JsonArray();
    if (src.getChildSpecs().isPresent()) {
        for (Spec spec : src.getChildSpecs().get()) {
            childSpecs.add(context.serialize(spec));
        }
    }
    flowSpecJson.add(FLOW_SPEC_CHILD_SPECS_KEY, childSpecs);

    return flowSpecJson;
}

From source file:org.apache.mahout.math.JsonMatrixAdapter.java

License:Apache License

public Matrix deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Vector.class, new JsonVectorAdapter());
    builder.registerTypeAdapter(Matrix.class, new JsonMatrixAdapter());
    Gson gson = builder.create();/*  ww w.jav a  2  s  .  co  m*/
    JsonObject obj = json.getAsJsonObject();
    String klass = obj.get(CLASS).getAsString();
    String matrix = obj.get(MATRIX).getAsString();
    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    Class<?> cl;
    try {
        cl = ccl.loadClass(klass);
    } catch (ClassNotFoundException e) {
        throw new JsonParseException(e);
    }
    return (Matrix) gson.fromJson(matrix, cl);
}

From source file:org.apache.mahout.math.JsonVectorAdapter.java

License:Apache License

public Vector deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Vector.class, new JsonVectorAdapter());
    Gson gson = builder.create();/*from   w w w.  j av a 2s.c o m*/
    JsonObject obj = json.getAsJsonObject();
    String klass = obj.get(JsonMatrixAdapter.CLASS).getAsString();
    String vector = obj.get(VECTOR).getAsString();
    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    Class<?> cl;
    try {
        cl = ccl.loadClass(klass);
    } catch (ClassNotFoundException e) {
        throw new JsonParseException(e);
    }
    return (Vector) gson.fromJson(vector, cl);
}

From source file:org.apache.pirk.querier.wideskies.QuerierDeserializer.java

License:Apache License

@Override
public Querier deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    // Check the version number.
    long querierVersion = jsonObject.get("querierVersion").getAsLong();
    if (querierVersion != Querier.querierSerialVersionUID) {
        throw new JsonParseException("Attempt to deserialize unsupported query version. Supported: "
                + Querier.querierSerialVersionUID + "; Received: " + querierVersion);
    }//  w ww  . j  a v  a 2  s  .  c  o m
    // Then deserialize the Query Info
    Query query = gson.fromJson(jsonObject.get("query").toString(), Query.class);

    // Now Paillier
    Paillier paillier = deserializePaillier(jsonObject.get("paillier").getAsJsonObject());

    List<String> selectors = gson.fromJson(jsonObject.get("selectors").toString(),
            new TypeToken<List<String>>() {
            }.getType());
    Map<Integer, String> embedSelectorMap = gson.fromJson(jsonObject.get("embedSelectorMap").toString(),
            new TypeToken<Map<Integer, String>>() {
            }.getType());

    return new Querier(selectors, paillier, query, embedSelectorMap);
}

From source file:org.apache.pirk.query.wideskies.QueryDeserializer.java

License:Apache License

@Override
public Query deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    logger.info("Got query json:" + jsonObject.toString());
    // Check the version number.
    long queryVersion = jsonObject.get("queryVersion").getAsLong();
    if (queryVersion != Query.querySerialVersionUID) {
        throw new JsonParseException("Attempt to deserialize unsupported query version. Supported: "
                + Query.querySerialVersionUID + "; Received: " + queryVersion);
    }// w  ww. ja  v a  2 s . c  o m
    // Then deserialize the Query Info
    QueryInfo queryInfo = deserializeInfo(jsonObject.get("queryInfo").getAsJsonObject());
    SortedMap<Integer, BigInteger> queryElements = gson.fromJson(jsonObject.get("queryElements"),
            new TypeToken<SortedMap<Integer, BigInteger>>() {
            }.getType());
    BigInteger N = new BigInteger(jsonObject.get("n").getAsString());
    BigInteger NSquared = new BigInteger(jsonObject.get("nsquared").getAsString());
    Map<Integer, String> expFileBasedLookup = gson.fromJson(jsonObject.get("expFileBasedLookup"),
            new TypeToken<Map<Integer, String>>() {
            }.getType());

    Query query = new Query(queryInfo, N, NSquared, queryElements);
    query.setExpFileBasedLookup(expFileBasedLookup);
    return query;
}

From source file:org.apache.pirk.query.wideskies.QueryDeserializer.java

License:Apache License

/**
 * Deserializes a QueryInfo JsonObject// ww  w .j a  v a2  s . c  o  m
 *
 * @param queryInfoJson
 *          A JsonObject at the root of a serialized QueryInfo object.
 * @return A QueryInfo object of the deserialized Json.
 * @throws JsonParseException
 */
public static QueryInfo deserializeInfo(JsonObject queryInfoJson) throws JsonParseException {
    // First check the version.
    long infoVersion = queryInfoJson.get("queryInfoVersion").getAsLong();
    if (infoVersion != QueryInfo.queryInfoSerialVersionUID) {
        throw new JsonParseException("Attempt to deserialize unsupported query info version. Supported: "
                + QueryInfo.queryInfoSerialVersionUID + "; Received: " + infoVersion);
    }
    // Deserialize the QuerySchema next, accounting for the possibility that it is null.
    QuerySchema querySchema;
    if (queryInfoJson.get("qSchema").isJsonNull()) {
        querySchema = null;
    } else {
        querySchema = deserializeSchema(queryInfoJson.get("qSchema").getAsJsonObject());
    }
    // Now start making the QueryInfo object.
    QueryInfo info = new QueryInfo(UUID.fromString(queryInfoJson.get("identifier").getAsString()),
            queryInfoJson.get("numSelectors").getAsInt(), queryInfoJson.get("hashBitSize").getAsInt(),
            queryInfoJson.get("hashKey").getAsString(), queryInfoJson.get("dataPartitionBitSize").getAsInt(),
            queryInfoJson.get("queryType").getAsString(), queryInfoJson.get("useExpLookupTable").getAsBoolean(),
            queryInfoJson.get("embedSelector").getAsBoolean(),
            queryInfoJson.get("useHDFSExpLookupTable").getAsBoolean(),
            queryInfoJson.get("numBitsPerDataElement").getAsInt(), querySchema);
    return info;
}

From source file:org.apache.pirk.query.wideskies.QueryDeserializer.java

License:Apache License

/**
 * Deserializes a QuerySchema JsonObject
 *
 * @param querySchemaJson/*from w w  w .  ja  v a2s  . c om*/
 *          A JsonObject at the root of a serialized QuerySchema object.
 * @return A QuerySchema object of the deserialized Json.
 * @throws JsonParseException
 */
private static QuerySchema deserializeSchema(JsonObject querySchemaJson) throws JsonParseException {
    // Deserialize The Query Schema First.
    long schemaVersion = querySchemaJson.get("querySchemaVersion").getAsLong();
    if (schemaVersion != QuerySchema.querySchemaSerialVersionUID) {
        throw new JsonParseException("Attempt to deserialize unsupported query info version. Supported: "
                + QueryInfo.queryInfoSerialVersionUID + "; Received: " + schemaVersion);
    }
    String dataFilterName = querySchemaJson.get("filterTypeName").getAsString();
    Set<String> filteredElementNames;
    try {
        filteredElementNames = gson.fromJson(querySchemaJson.get("filteredElementNames"),
                new TypeToken<Set<String>>() {
                }.getType());
    } catch (Exception e) {
        logger.warn("No filtered element names for Query Schema deserialization.");
        filteredElementNames = null;
    }
    // Set up the data filter
    DataFilter dataFilter;
    try {
        dataFilter = FilterFactory.getFilter(dataFilterName, filteredElementNames);
    } catch (IOException | PIRException e) {
        logger.error("Error trying to create data filter from JSON.", e);
        throw new JsonParseException(e);
    }

    QuerySchema querySchema = new QuerySchema(querySchemaJson.get("schemaName").getAsString(),
            querySchemaJson.get("dataSchemaName").getAsString(),
            querySchemaJson.get("selectorName").getAsString(), dataFilterName, dataFilter,
            querySchemaJson.get("dataElementSize").getAsInt());
    List<String> elementNames = gson.fromJson(querySchemaJson.get("elementNames"),
            new TypeToken<List<String>>() {
            }.getType());
    querySchema.getElementNames().addAll(elementNames);
    HashMap<String, String> additionalFields = gson.fromJson(querySchemaJson.get("additionalFields"),
            new TypeToken<HashMap<String, String>>() {
            }.getType());
    querySchema.getAdditionalFields().putAll(additionalFields);
    return querySchema;
}

From source file:org.apache.pirk.response.wideskies.ResponseDeserializer.java

License:Apache License

@Override
public Response deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    final JsonObject jsonObject = jsonElement.getAsJsonObject();
    long responseVersion = jsonObject.get("responseVersion").getAsLong();
    if (responseVersion != Response.responseSerialVersionUID) {
        throw new JsonParseException("\"Attempt to deserialize unsupported query version. Supported: \"\n"
                + "          + Response.responseSerialVersionUID + \"; Received: \" + responseVersion");
    }/* w ww . j  a v  a 2  s  .  c  o m*/
    QueryInfo queryInfo = QueryDeserializer.deserializeInfo(jsonObject.get("queryInfo").getAsJsonObject());
    Response response = new Response(queryInfo);
    TreeMap<Integer, BigInteger> responseElements = gson.fromJson(jsonObject.get("responseElements"),
            new TypeToken<TreeMap<Integer, BigInteger>>() {
            }.getType());
    response.setResponseElements(responseElements);
    return response;
}

From source file:org.apache.qpid.disttest.json.PropertyValueAdapter.java

License:Apache License

@Override
public PropertyValue deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    } else if (json.isJsonPrimitive()) {
        Object result = null;/*from ww w .  ja v  a 2  s. c om*/
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            result = primitive.getAsString();
        } else if (primitive.isNumber()) {
            String asString = primitive.getAsString();
            if (asString.indexOf('.') != -1 || asString.indexOf('e') != -1) {
                result = primitive.getAsDouble();
            } else {
                result = primitive.getAsLong();
            }
        } else if (primitive.isBoolean()) {
            result = primitive.getAsBoolean();
        } else {
            throw new JsonParseException("Unsupported primitive value " + primitive);
        }
        return new SimplePropertyValue(result);
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        List<Object> result = new ArrayList<Object>(array.size());
        for (JsonElement element : array) {
            result.add(context.deserialize(element, Object.class));
        }
        return new SimplePropertyValue(result);
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        JsonElement defElement = object.getAsJsonPrimitive(DEF_FIELD);
        Class<?> classInstance = null;
        if (defElement != null) {
            try {
                classInstance = _factory.getPropertyValueClass(defElement.getAsString());
            } catch (ClassNotFoundException e) {
                // ignore
            }
        }
        if (classInstance == null) {
            Map<String, Object> result = new HashMap<String, Object>();
            for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
                Object value = context.deserialize(entry.getValue(), Object.class);
                result.put(entry.getKey(), value);
            }
            return new SimplePropertyValue(result);
        } else {
            return context.deserialize(json, classInstance);
        }
    } else {
        throw new JsonParseException("Unsupported JSON type " + json);
    }
}