Example usage for com.google.gson JsonSyntaxException JsonSyntaxException

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

Introduction

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

Prototype

public JsonSyntaxException(String msg, Throwable cause) 

Source Link

Usage

From source file:br.com.caelum.vraptor.serialization.gson.DateGsonConverter.java

License:Open Source License

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {//w  w w . jav  a  2 s. c  om
        return iso8601Format.parse(json.getAsString());
    } catch (ParseException e) {
        throw new JsonSyntaxException(json.getAsString(), e);
    }
}

From source file:co.forsaken.projectindigo.utils.mojangtokens.DateTypeAdapter.java

License:Open Source License

@Override
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("The date " + json + " is not a string!");
    }// w  ww  .j a  v  a2  s  . c om
    if (type != Date.class) {
        throw new IllegalArgumentException(getClass() + " cannot deserialize to " + type);
    }
    String value = json.getAsString();
    synchronized (enUsFormat) {
        try {
            return enUsFormat.parse(value);
        } catch (ParseException e) {
            try {
                return iso8601Format.parse(value);
            } catch (ParseException e2) {
                try {
                    String tmp = value.replace("Z", "+00:00");
                    return iso8601Format.parse(tmp.substring(0, 22) + tmp.substring(23));
                } catch (ParseException e3) {
                    throw new JsonSyntaxException("Invalid date " + value, e3);
                }
            }
        }
    }
}

From source file:com.ca.dvs.utilities.raml.JsonUtil.java

License:Open Source License

/**
 * Get a JSON representation of the Raml example, whether it is inline or included.
 * <p>/*from  w ww  . j  a va 2 s  . co m*/
 * @param example the Raml example text
 * @param resourceDir a folder to search for imported Raml elements
 * @return Raml example data in JSON format
 */
public static String getJsonFromExample(String example, File resourceDir) {

    String json = example;

    // Example is not JSON.  Is it a string containing the path to a file?
    File includeFile = null;

    includeFile = resourceDir == null ? new File(example.trim()) : new File(resourceDir, example.trim());

    logger.debug(String.format("Potential include file: %s", includeFile.getAbsolutePath()));

    String includedContent = null;

    if (includeFile.canRead()) { // First check to see if example is actually an existing file path (RAML parser include bug workaround)

        Scanner scanner = null;

        try {

            scanner = new Scanner(includeFile);

            includedContent = scanner.useDelimiter("\\Z").next();

            JsonParser parser = new JsonParser();

            try {

                parser.parse(includedContent);

                json = includedContent;

            } catch (JsonSyntaxException jse) {

                String msg = String.format("Processing included example %s - %s\nContent follows...\n%s",
                        includeFile.getPath(), jse.getMessage(), includedContent);
                throw new JsonSyntaxException(msg, jse.getCause());

            }

        } catch (FileNotFoundException e1) {

            // If resolving the example content as a file lands us here, it's apparently not a file
            // validate the content by parsing as JSON

            try {

                JsonParser parser = new JsonParser();
                parser.parse(example);

                // If we get this far without an exception, the example is truly JSON
                json = example;

            } catch (JsonSyntaxException e) {

                String msg = String.format("Processing example - %s\nContent follows...\n%s", e.getMessage(),
                        example);
                throw new JsonSyntaxException(msg, e.getCause());

            }

        } finally {

            if (null != scanner) {
                scanner.close();
            }

        }

    } else {

        try {
            JsonParser parser = new JsonParser();
            parser.parse(example);

            // If we get this far without an exception, the example is truly JSON
            json = example;

        } catch (JsonSyntaxException e) {

            String msg = String.format("Processing example - %s\nContent follows...\n%s", e.getMessage(),
                    example);
            throw new JsonSyntaxException(msg, e.getCause());

        }
    }

    return json;
}

From source file:com.ca.dvs.utilities.raml.RamlUtil.java

License:Open Source License

/**
 * @param resource the Raml Resource object from which to extract example data
 * @param resourceDir the directory anchor from which any possible included resources must be loaded from
 * @return the object containing the example data
 *//*w  w w  .j  a va  2 s.  c  o m*/
public static Map<String, Object> getSampleData(Resource resource, File resourceDir) {

    Map<String, Object> sampleData = new LinkedHashMap<String, Object>();

    Action getAction = resource.getAction(ActionType.GET);
    if (null != getAction) {

        Map<String, Response> getResponses = getAction.getResponses();

        if (null != getResponses) {

            Response response200 = getResponses.get("200");

            if (null != response200) {

                // We're only interested in data associated with a successful GET operation
                Map<String, MimeType> bodyMap = response200.getBody();

                if (null != bodyMap) {

                    // We can only deal with JSON style data for the moment
                    MimeType mimeBody = bodyMap.get("application/json");

                    if (null != mimeBody) {

                        String schema = mimeBody.getSchema();

                        if (null != schema) {

                            try {

                                sampleData.put("schema", getJsonElementValue(schema));

                            } catch (JsonSyntaxException e) {
                                String msg = String.format("Parsing schema for resource %s - %s",
                                        resource.getUri(), e.getMessage());
                                logger.error(msg);
                                throw new JsonSyntaxException(msg, e.getCause());
                            }

                        } else {
                            sampleData.put("schema", null);
                        }

                        String example = mimeBody.getExample();

                        if (null != example) {

                            try {

                                String jsonString = JsonUtil.getJsonFromExample(example, resourceDir);

                                sampleData.put("example", getJsonElementValue(jsonString));

                            } catch (JsonSyntaxException e) {
                                String msg = String.format("Parsing example for resource %s - %s",
                                        resource.getUri(), e.getMessage());
                                logger.error(msg);
                                throw new JsonSyntaxException(msg, e.getCause());
                            }
                        } else {
                            sampleData.put("example", null);
                        }
                    }
                }
            }
        }
    }

    return sampleData;
}

From source file:com.cognifide.qa.bb.aem.content.CrxRequestSender.java

License:Apache License

/**
 * This method sends request to author instance
 *
 * @param request request to send/* w w w . j  a v  a  2 s. co  m*/
 * @return JsonObject representation of response
 * @throws IOException if response doesn't contain desired message
 */
public JsonObject sendCrxRequest(HttpUriRequest request) throws IOException {
    String resultJson;
    try (CloseableHttpResponse response = httpClient.execute(request)) {
        resultJson = EntityUtils.toString(response.getEntity());
    }
    JsonObject result;
    try {
        result = new JsonParser().parse(resultJson).getAsJsonObject();
    } catch (JsonSyntaxException e) {
        throw new JsonSyntaxException("Unable to parse as Json: " + resultJson, e);
    }
    if (result.get("success").getAsBoolean()) {
        return result;
    }
    throw new IOException(result.get("msg").getAsString());
}

From source file:com.fixmyfolks.app.DateTypeAdapter.java

License:Apache License

private synchronized Date deserializeToDate(String json) {
    try {//from w  w  w. j  a  v a 2 s .co m
        return localFormat.parse(json);
    } catch (ParseException ignored) {
    }
    try {
        return enUsFormat.parse(json);
    } catch (ParseException ignored) {
    }
    try {
        return iso8601Format.parse(json);
    } catch (ParseException e) {
        throw new JsonSyntaxException(json, e);
    }
}

From source file:com.gilecode.yagson.types.TypeUtils.java

License:Apache License

private static Type toType(String name) {
    if (name.equals("?")) {
        return unknownType;
    }/*  w ww  .  j ava 2s .c om*/
    try {
        return Class.forName(name);
    } catch (ClassNotFoundException e) {
        throw new JsonSyntaxException("Missing class specified in @type info", e);
    }
}

From source file:com.google.dart.server.internal.remote.ByteResponseStream.java

License:Open Source License

@Override
public JsonObject take() throws Exception {
    String line = lineQueue.take();
    if (line == EOF_LINE) {
        lineQueue.add(line);/* w  w  w  . j  av  a 2 s .c  o  m*/
        return null;
    }
    try {
        return (JsonObject) new JsonParser().parse(line);
    } catch (JsonSyntaxException e) {
        // Include the line in the message so that we can better diagnose the problem
        throw new JsonSyntaxException("Parse server message failed: " + line, e);
    }
}

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

License:Apache License

/**
 * @param json//w w w.  j  av a2  s  .  c o  m
 * @return
 */
private Date deserializeToDate(JsonElement json) {
    try {
        String dateStr = json.getAsString();
        if (dateStr.trim().equals("")) {
            return null;
        } else {
            return custFormat.parse(dateStr);
        }
    } catch (ParseException e) {
        throw new JsonSyntaxException(json.getAsString(), e);
    }
}

From source file:com.hpzc.common.json.MyDateTypeAdapter.java

License:Apache License

private synchronized Date deserializeToDate(String json) {
    try {/*w  w  w. j a v  a2s  .  co  m*/

        //        if(isNumeric(json))
        //{
        //long time = Long.parseLong(json);
        //return new Date(time);
        //}
        if (json.indexOf("T") > 0) {
            return df3.parse(json);
        } else if (json.indexOf(":") > 0) {
            return df2.parse(json);
        } else {
            return df1.parse(json);
        }
    } catch (ParseException e) {
        throw new JsonSyntaxException(json, e);
    }

    //    try {
    //      return df1.parse(json);
    //    } catch (ParseException ignored) {
    //    }
    //    try {
    //      return df2.parse(json);
    //    } catch (ParseException ignored) {
    //    }
    //    try {
    //      return df3.parse(json);
    //    } catch (ParseException e) {
    //      throw new JsonSyntaxException(json, e);
    //    }
}