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:com.heartbuffer.pipette.json.InputConfigJsonAdapter.java

License:Open Source License

@Override
public InputConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonPrimitive primitive = (JsonPrimitive) jsonObject.get(TYPE_FIELD);
    String inputType = primitive.getAsString();

    Class<?> clazz = null;//  www  .  jav a  2s .  c o m

    switch (inputType) {
    case FileInput.TYPE:
        clazz = FileInputConfig.class;
        break;
    default:
        throw new JsonParseException("No such input of type '" + inputType + "'");
    }

    return context.deserialize(jsonObject, clazz);
}

From source file:com.heartbuffer.pipette.json.OutputConfigJsonAdapter.java

License:Open Source License

@Override
public OutputConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonPrimitive primitive = (JsonPrimitive) jsonObject.get(TYPE_FIELD);
    String outputType = primitive.getAsString();

    Class<?> clazz = null;/*from w w w.ja v  a  2 s . c o  m*/

    switch (outputType) {
    case FileOutput.TYPE:
        //TODO: Add FileOutputConfig
        break;
    case ElasticsearchTransportOutput.TYPE:
        clazz = ElasticsearchTransportOutputConfig.class;
        break;
    case ElasticsearchHttpOutput.TYPE:
        clazz = ElasticsearchHttpOutputConfig.class;
        break;
    default:
        throw new JsonParseException("No such output of type '" + outputType + "'");
    }

    return context.deserialize(jsonObject, clazz);
}

From source file:com.hisign.sso.common.gson.CustDateTypeAdapter.java

License:Apache License

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("The date should be a string value");
    }/*  w  ww  . j ava2 s . co m*/
    Date date = deserializeToDate(json);
    if (typeOfT == Date.class) {
        return date;
    } else if (typeOfT == Timestamp.class) {
        return new Timestamp(date.getTime());
    } else if (typeOfT == java.sql.Date.class) {
        return new java.sql.Date(date.getTime());
    } else {
        throw new IllegalArgumentException(getClass() + " cannot deserialize to " + typeOfT);
    }
}

From source file:com.hp.ov.sdk.adaptors.PortTelemetrySerializationAdapter.java

License:Apache License

@Override
public PortTelemetry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (json.isJsonPrimitive()) {
        return PortTelemetry.valueOf(json.getAsString());
    } else if (json.isJsonArray()) {
        for (JsonElement element : json.getAsJsonArray()) {
            String elementAsString = element.getAsString();

            if (PortTelemetry.contains(elementAsString)) {
                return PortTelemetry.valueOf(elementAsString);
            }/*from  www .  j av a  2s  .c  o m*/
        }
    }
    throw new JsonParseException("Unknown port telemetry value type. "
            + "Expected value types are String or String[] (String array)");
}

From source file:com.hp.ov.sdk.adaptors.StorageCapabilitiesDeserializer.java

License:Apache License

@Override
public StorageCapabilities deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (json.isJsonArray()) {
        StorageCapabilities storageCapabilities = new StorageCapabilities();

        storageCapabilities.setControllerModes(Lists.newArrayList(ControllerMode.RAID));
        storageCapabilities.setDriveTechnologies(new ArrayList<DriveTechnology>());
        storageCapabilities.setRaidLevels(new ArrayList<RaidLevel>());

        Iterator<JsonElement> iterator = json.getAsJsonArray().iterator();

        while (iterator.hasNext()) {
            JsonElement element = iterator.next();

            if (!element.isJsonPrimitive()) {
                throw new JsonParseException(
                        "Expected a primitive (String) value but found " + element.toString());
            }/*w ww .  j a  va2 s.  c  om*/

            try {
                storageCapabilities.getRaidLevels().add(RaidLevel.valueOf(element.getAsString()));
            } catch (IllegalArgumentException e) {
                throw new JsonParseException("Unknown RAID Level", e);
            }
        }
        return storageCapabilities;
    }
    return new Gson().fromJson(json, StorageCapabilities.class);
}

From source file:com.ibm.watson.app.common.services.impl.BluemixServicesConfigurationParser.java

License:Open Source License

public void parseAndRegisterServices(JsonElement json) {
    Objects.requireNonNull(json, MessageKey.AQWEGA14019E_json_element_null.getMessage().getFormattedMessage());
    if (!json.isJsonObject()) {
        throw new JsonParseException(MessageKey.AQWEGA14001E_expected_json_object_parse_bluemix_service_conf
                .getMessage().getFormattedMessage());
    }/* www  . j  ava 2 s. c  o m*/

    final boolean isTrace = logger.isTraceEnabled();

    for (Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
        final String serviceKey = entry.getKey();
        final JsonElement serviceConfigArray = entry.getValue();
        if (!serviceConfigArray.isJsonArray()) {
            throw new JsonParseException(MessageKey.AQWEGA14002E_expected_json_array_while_parsing_config_1
                    .getMessage(serviceKey).getFormattedMessage());
        }

        BluemixServiceInfo<? extends BluemixConfiguredService> service = BluemixServicesBinder
                .getBluemixServiceByName(serviceKey);
        if (service == null) {
            logger.warn(MessageKey.AQWEGA12001W_unexpected_conf_supplied_for_service_ignore_1
                    .getMessage(serviceKey));
            continue;
        }

        if (isTrace) {
            logger.trace("Loading configuration for service '" + serviceKey + "'");
        }

        for (JsonElement serviceInstanceConfig : serviceConfigArray.getAsJsonArray()) {
            try {
                BluemixConfiguredService serviceImpl = service.serviceImpl.getConstructor().newInstance();
                serviceImpl
                        .setConfig(gson.fromJson(serviceInstanceConfig, serviceImpl.getConfigurationClass()));
                serviceImpl.initialize();
                register(service, serviceImpl);
            } catch (IllegalAccessException | InstantiationException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                logger.warn(MessageKey.AQWEGA12002W_unable_register_service_error_2.getMessage(serviceKey,
                        e.getMessage()));
                logger.catching(Level.DEBUG, e);
            }
        }
    }
}

From source file:com.itametis.jsonconverter.pathstrategy.PathDeserializer.java

License:Open Source License

/**
 * Create a dictionary between the Json Element and its name.
 * Delete the links between the Json Elements.
 * The tree is then an HashMap and every element knows who is their children/parent.
 *
 * @param type    the class type./*from w w  w  .  j  av  a2s  .co  m*/
 * @param element the JsonElement.
 */
private void flattenJsonTree(Type type, JsonElement element) {
    if (this.isAJsonnableObject(type)) {

        //Element name :
        String jsonClassName = JsonMappingPackage.getInstance().getJsonnableContent().get(type).getjSonName();

        this.topElement = new JsonElementProxy(element, jsonClassName, null);

        Flattenizer flattenizer = new Flattenizer();
        LOGGER.debug("Flatten top element of the json tree '{}'.", jsonClassName);

        try {
            //Start a recursive flattening.
            this.jsonTree = flattenizer.flatten(this.topElement);
        } catch (JsonException ex) {
            throw new JsonParseException(ex);
        }
    } else {
        throw new JsonParseException("Element on top '" + type.getTypeName()
                + "' is not flagged as jsonnable with annotation @Jsonnable."
                + "It can not be analysed. Litteral conversion will be done.");
    }
}

From source file:com.itametis.jsonconverter.pathstrategy.PathDeserializer.java

License:Open Source License

/**
 * Remove a field from an indicated parent.
 *
 * @param toMove the combo jsonName - field information.
 */// w w w .j av  a2  s .co m
private void removeField(JsonElementProxy element, Map.Entry<String, ScannedField> toMove) {

    JsonElementList parents = getParentFromPath(toMove.getValue().getPathInJson(), element);
    for (JsonElementProxy parent : parents) {
        if (parent == null) {
            throw new JsonParseException(
                    "received null as a parent for element " + element.getJsonName() + " while deleting.");
        }
        try {
            parent.removeChild(element);
            LOGGER.info("Removed node {} from parent {} (jsonName)", element.getJsonName(),
                    parent.getJsonName());
        } catch (JsonException ex) {
            throw new JsonParseException(ex);
        }
    }
}

From source file:com.itametis.jsonconverter.pathstrategy.PathDeserializer.java

License:Open Source License

/**
 * Insert a field to an indicated Parent.
 *
 * @param toMove        the combo jsonName - field information.
 * @param elementToMove the element to insert.
 *///from  ww w . j  a v  a 2s  .  co  m
private void insertField(JsonElementProxy element, Map.Entry<String, ScannedField> toMove) {
    JsonElementList parents = getParentFromPath(toMove.getValue().getPathInCode(), element);
    for (JsonElementProxy parent : parents) {
        if (parent == null) {
            throw new JsonParseException(
                    "Received null as a parent for element " + element.getJsonName() + " while inserting.");
        }
        parent.addChild(element);
        LOGGER.info("Inserted node {} in parent {} (jsonName)", element.getJsonName(), parent.getJsonName());
    }
}

From source file:com.itametis.jsonconverter.pathstrategy.PathDeserializer.java

License:Open Source License

/**
 * Return an error when looking for a parent.
 *
 * @param path   the path./*from  w ww.  j a  v a2  s  .c  om*/
 * @param parent the parent.
 */
private void returnErrorNoParent(String path, JsonElementProxy parent) {
    String availableList = ".., .";
    for (String name : parent.getChildren().keySet()) {
        availableList += ", " + name;
    }
    LOGGER.error("Searched child path '{}'. But available paths were : [{}]", path, availableList);
    throw new JsonParseException(
            "Searched child path '" + path + "'. But available paths were : [" + availableList + "]");
}