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(String msg, Throwable cause) 

Source Link

Document

Creates exception with the specified message and cause.

Usage

From source file:co.cask.cdap.etl.spark.batch.DatasetInfoTypeAdapter.java

License:Apache License

@Override
public DatasetInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    String datasetName = obj.get("datasetName").getAsString();
    Map<String, String> datasetArgs = context.deserialize(obj.get("datasetArgs"), mapType);
    if (obj.get("datasetSplitClass") == null) {
        return new DatasetInfo(datasetName, datasetArgs, null);
    }//from   w w  w . j  a va  2  s  .c  om
    String datasetSplitClass = obj.get("datasetSplitClass").getAsString();
    ClassLoader classLoader = Objects.firstNonNull(Thread.currentThread().getContextClassLoader(),
            SparkBatchSourceFactory.class.getClassLoader());
    try {
        Class<?> splitClass = classLoader.loadClass(datasetSplitClass);
        List<Split> splits = context.deserialize(obj.get("datasetSplits"), getListType(splitClass));
        return new DatasetInfo(datasetName, datasetArgs, splits);
    } catch (ClassNotFoundException e) {
        throw new JsonParseException("Unable to deserialize splits", e);
    }
}

From source file:com.canoo.dolphin.impl.codec.CreatePresentationModelEncoder.java

License:Apache License

@Override
public CreatePresentationModelCommand decode(JsonObject jsonObject) {
    Assert.requireNonNull(jsonObject, "jsonObject");

    try {//from w  w  w . j  av  a  2  s  . co  m
        final CreatePresentationModelCommand command = new CreatePresentationModelCommand();

        command.setPmId(jsonObject.getAsJsonPrimitive(PM_ID).getAsString());
        command.setPmType(jsonObject.getAsJsonPrimitive(PM_TYPE).getAsString());
        command.setClientSideOnly(false);

        final JsonArray jsonArray = jsonObject.getAsJsonArray(PM_ATTRIBUTES);
        final List<Map<String, Object>> attributes = new ArrayList<>();
        for (final JsonElement jsonElement : jsonArray) {
            final JsonObject attribute = jsonElement.getAsJsonObject();
            final HashMap<String, Object> map = new HashMap<>();
            map.put("propertyName", attribute.getAsJsonPrimitive(ATTRIBUTE_NAME).getAsString());
            map.put("id", attribute.getAsJsonPrimitive(ATTRIBUTE_ID).getAsString());
            final Object value = attribute.has(ATTRIBUTE_VALUE) ? decodeValue(attribute.get(ATTRIBUTE_VALUE))
                    : null;
            map.put("value", value);
            map.put("baseValue", value);
            map.put("qualifier", null);
            attributes.add(map);
        }
        command.setAttributes(attributes);

        return command;
    } catch (IllegalStateException | ClassCastException | NullPointerException ex) {
        throw new JsonParseException("Illegal JSON detected", ex);
    }
}

From source file:com.canoo.dolphin.impl.codec.OptimizedJsonCodec.java

License:Apache License

@Override
public List<Command> decode(String transmitted) {
    Assert.requireNonNull(transmitted, "transmitted");
    LOG.trace("Decoding message: {}", transmitted);
    try {/*from  w  ww. ja  v  a 2 s. c o  m*/
        final List<Command> commands = new ArrayList<>();
        final JsonArray array = (JsonArray) new JsonParser().parse(transmitted);

        for (final JsonElement jsonElement : array) {
            final JsonObject command = (JsonObject) jsonElement;
            JsonPrimitive idPrimitive = command.getAsJsonPrimitive("id");

            String id = null;
            if (idPrimitive != null) {
                id = idPrimitive.getAsString();
            }
            LOG.trace("Decoding command: {}", id);
            CommandEncoder<?> encoder = null;
            if (id != null) {
                encoder = DECODERS.get(id);
            }
            if (encoder != null) {
                commands.add(encoder.decode(command));
            } else {
                commands.addAll(fallBack.decode("[" + command.toString() + "]"));
            }
        }
        LOG.trace("Decoded command list with {} commands", commands.size());
        return commands;
    } catch (ClassCastException | NullPointerException ex) {
        throw new JsonParseException("Illegal JSON detected", ex);
    }
}

From source file:com.canoo.dolphin.impl.codec.ValueChangedCommandEncoder.java

License:Apache License

@Override
public ValueChangedCommand decode(JsonObject jsonObject) {
    Assert.requireNonNull(jsonObject, "jsonObject");

    try {/*from www .  j av  a  2  s  .com*/
        final ValueChangedCommand command = new ValueChangedCommand();

        command.setNewValue(decodeValue(jsonObject.get("n")));
        command.setOldValue(decodeValue(jsonObject.get("o")));
        command.setAttributeId(jsonObject.get("a").getAsString());

        return command;
    } catch (IllegalStateException | ClassCastException | NullPointerException ex) {
        throw new JsonParseException("Illegal JSON detected", ex);
    }
}

From source file:com.epam.dlab.automation.model.JsonMapperDto.java

License:Apache License

public static <T> List<T> readListOf(String pathToJson, Class<T> clasz) {
    try (FileInputStream in = new FileInputStream(pathToJson)) {
        CollectionType typeReference = TypeFactory.defaultInstance().constructCollectionType(List.class, clasz);
        return OBJECT_MAPPER.readValue(in, typeReference);
    } catch (IOException e) {
        throw new JsonParseException("Cannot read json file", e);
    }/*from www  .j  a va 2  s .co  m*/
}

From source file:com.epam.dlab.automation.model.JsonMapperDto.java

License:Apache License

public static <T> T readObject(String pathToJson, Class<T> clasz) {
    try (FileInputStream in = new FileInputStream(pathToJson)) {
        return OBJECT_MAPPER.readValue(in, clasz);
    } catch (IOException e) {
        throw new JsonParseException("Cannot read json file", e);
    }/*  w w w. j a  v  a  2 s .  c om*/
}

From source file:com.evilco.license.common.data.holder.LicenseHolderJsonAdapter.java

License:Apache License

/**
 * {@inheritDoc}/*w w  w.  j  av  a  2 s . c o  m*/
 */
@Override
public ILicenseHolder deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    // get implementation name
    try {
        // get class instance
        Class<? extends ILicenseHolder> licenseClass = Class
                .forName(json.getAsJsonObject().get("implementationClassName").getAsString())
                .asSubclass(ILicenseHolder.class);

        // de-serialize
        return context.deserialize(json, licenseClass);
    } catch (ClassCastException ex) {
        throw new JsonParseException(ex.getMessage(), ex);
    } catch (ClassNotFoundException ex) {
        throw new JsonParseException(ex.getMessage(), ex);
    }
}

From source file:com.google.gwtjsonrpc.server.JsonServlet.java

License:Apache License

private void parseGetRequest(final CallType call) {
    final HttpServletRequest req = call.httpRequest;

    if ("2.0".equals(req.getParameter("jsonrpc"))) {
        final JsonObject d = new JsonObject();
        d.addProperty("jsonrpc", "2.0");
        d.addProperty("method", req.getParameter("method"));
        d.addProperty("id", req.getParameter("id"));
        try {/*from  ww  w .j a  va 2  s .  com*/
            final byte[] params = req.getParameter("params").getBytes("ISO-8859-1");
            final String p = new String(Base64.decodeBase64(params), "UTF-8");
            d.add("params", new JsonParser().parse(p));
        } catch (UnsupportedEncodingException e) {
            throw new JsonParseException("Cannot parse params", e);
        }

        try {
            final GsonBuilder gb = createGsonBuilder();
            gb.registerTypeAdapter(ActiveCall.class, //
                    new CallDeserializer<CallType>(call, this));
            gb.create().fromJson(d, ActiveCall.class);
        } catch (JsonParseException err) {
            call.method = null;
            call.params = null;
            throw err;
        }

    } else { /* JSON-RPC 1.1 */
        final Gson gs = createGsonBuilder().create();

        call.method = lookupMethod(req.getParameter("method"));
        if (call.method == null) {
            throw new NoSuchRemoteMethodException();
        }
        final Type[] paramTypes = call.method.getParamTypes();
        final Object[] r = new Object[paramTypes.length];

        for (int i = 0; i < r.length; i++) {
            final String v = req.getParameter("param" + i);
            if (v == null) {
                r[i] = null;
            } else if (paramTypes[i] == String.class) {
                r[i] = v;
            } else if (paramTypes[i] instanceof Class<?> && ((Class<?>) paramTypes[i]).isPrimitive()) {
                // Primitive type, use the JSON representation of that type.
                //
                r[i] = gs.fromJson(v, paramTypes[i]);
            } else {
                // Assume it is like a java.sql.Timestamp or something and treat
                // the value as JSON string.
                //
                r[i] = gs.fromJson(gs.toJson(v), paramTypes[i]);
            }
        }
        call.params = r;
        call.callback = req.getParameter("callback");
    }
}

From source file:com.google.wave.api.impl.ElementGsonAdaptor.java

License:Apache License

@Override
public Element deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Element result = null;/*  w  ww . java 2  s. c o  m*/
    ElementType type = ElementType.valueOfIgnoreCase(json.getAsJsonObject().get(TYPE_TAG).getAsString());

    Map<String, String> properties = context.deserialize(json.getAsJsonObject().get(PROPERTIES_TAG),
            GsonFactory.STRING_MAP_TYPE);

    if (FormElement.getFormElementTypes().contains(type)) {
        result = new FormElement(type, properties);
    } else if (type == ElementType.GADGET) {
        result = new Gadget(properties);
    } else if (type == ElementType.IMAGE) {
        result = new Image(properties);
    } else if (type == ElementType.ATTACHMENT) {
        byte[] data = null;
        String encodedData = properties.get(Attachment.DATA);
        if (encodedData != null) {
            try {
                data = Base64.decodeBase64(encodedData.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new JsonParseException("Couldn't convert to utf-8", e);
            }
        }
        result = new Attachment(properties, data);
    } else if (type == ElementType.LINE) {
        result = new Line(properties);
    } else {
        result = new Element(type, properties);
    }
    return result;
}

From source file:com.googlesource.gerrit.plugins.hooks.rtc.workitems.RtcRelatedLinkDeserializer.java

License:Apache License

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

    JsonObject jsonObj = json.getAsJsonObject();
    String resourceUrlString = jsonObj.get("rdf:resource").getAsString();
    try {//from   w  ww  .j  a va  2  s. c o m
        URL resourceUrl = new URL(resourceUrlString);
        String label = jsonObj.get("oslc_cm:label").getAsString();

        return new RtcRelatedLink(resourceUrl, label);

    } catch (MalformedURLException e) {
        throw new JsonParseException("Invalid rdf:resource URL '" + resourceUrlString + "'", e);
    }
}