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:org.sickbeard.json.deserializer.BooleanDeserializer.java

License:Open Source License

@Override
public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    try {/*from  w w w . ja v a 2s.c o m*/
        String value = arg0.getAsJsonPrimitive().getAsString();
        if (value.toLowerCase().equals("true")) {
            return true;
        } else if (value.toLowerCase().equals("false")) {
            return false;
        } else {
            return Integer.valueOf(value) != 0;
        }
    } catch (ClassCastException e) {
        throw new JsonParseException("Cannot parse Boolean string '" + arg0.toString() + "'", e);
    } catch (Exception e) {
        throw new JsonParseException("Cannot parse Boolean string '" + arg0.toString() + "'", e);
    }
}

From source file:org.terasology.naming.gson.VersionTypeAdapter.java

License:Apache License

@Override
public Version deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/*from w ww  .j a  v  a2s. c  o  m*/
        return new Version(json.getAsString());
    } catch (VersionParseException e) {
        throw new JsonParseException("Invalid version '" + json.getAsString() + "'", e);
    }
}

From source file:org.thingsplode.synapse.serializers.gson.adapters.ClassTypeAdapter.java

License:Apache License

@Override
public Class deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/*from   w  w  w. j  a va  2  s .  co m*/
        return Class.forName(json.getAsString());
    } catch (ClassNotFoundException ex) {
        throw new JsonParseException("Could not deserialize json element [" + json.getAsString()
                + "] because of: " + ex.getMessage(), ex);
    }
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.DegreeWorksPlanScraper.java

License:Apache License

protected Gson getGson() {
    return new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//from   w w w.j  av a 2s .co  m
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    }).setPrettyPrinting().create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.GsonRepresentation.java

License:Apache License

public GsonBuilder getBuilder() {
    if (iBuilder == null) {
        iBuilder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
            @Override/*from w  w w .ja  v  a  2 s.co m*/
            public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
            }
        }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
            @Override
            public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
            }
        }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
            @Override
            public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(src));
            }
        }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
            @Override
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                try {
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                            .parse(json.getAsJsonPrimitive().getAsString());
                } catch (ParseException e) {
                    throw new JsonParseException(e.getMessage(), e);
                }
            }
        });
    }
    return iBuilder;
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.PurdueCourseRequestsValidationProvider.java

License:Apache License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override/*w  ww  .  j a  va2  s .  co m*/
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.server.sectioning.PublishedSectioningSolutionsBackend.java

License:Apache License

protected Gson getGson() {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//  w  ww  .j a  v a  2s. co m
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.wso2.carbon.identity.entitlement.endpoint.util.JSONRequestParser.java

License:Open Source License

/**
 * This is to seperate JSON to attributes
 * @param jsonAttribute - the map of category string and the JSON Element
 * @param jsonCategory - the  main object category
 * @param categories - the set of categories
 * @throws RequestParseException/*from ww w. j a  va 2  s.co m*/
 * @throws UnknownIdentifierException
 */
private static void jsonAttributeSeperator(Map.Entry<String, JsonElement> jsonAttribute,
        JsonObject jsonCategory, Set<Attributes> categories)
        throws RequestParseException, UnknownIdentifierException {

    Node content = null;
    URI category = null;
    Set<Attribute> attributes = null;
    String id = null;

    if (EntitlementEndpointConstants.CATEGORY_DEFAULT.equals(jsonAttribute.getKey())) {
        if (jsonCategory.has(EntitlementEndpointConstants.CATEGORY_ID)) {
            category = stringCateogryToURI(
                    jsonCategory.get(EntitlementEndpointConstants.CATEGORY_ID).getAsString());
        }
    } else {
        if (category == null) {
            category = stringCateogryToURI(jsonAttribute.getKey());
        }
        if (jsonCategory.has(EntitlementEndpointConstants.ID)) {
            id = jsonCategory.get(EntitlementEndpointConstants.ID).getAsString();
        }
        if (jsonCategory.has(EntitlementEndpointConstants.CONTENT)) {
            DocumentBuilderFactory dbf;
            Document doc = null;

            String xmlContent = stringContentToXMLContent(
                    jsonCategory.get(EntitlementEndpointConstants.CONTENT).getAsString());
            dbf = IdentityUtil.getSecuredDocumentBuilderFactory();
            dbf.setNamespaceAware(true);

            try (ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlContent.getBytes())) {
                doc = dbf.newDocumentBuilder().parse(inputStream);
            } catch (Exception e) {
                throw new JsonParseException("DOM of request element can not be created from String.", e);
            }
            if (doc != null) {
                content = doc.getDocumentElement();
            }
        }

        // Add all category attributes
        if (jsonCategory.has(EntitlementEndpointConstants.ATTRIBUTE)) {
            if (jsonCategory.get(EntitlementEndpointConstants.ATTRIBUTE).isJsonArray()) {
                attributes = new HashSet<>();
                for (JsonElement jsonElement : jsonCategory.get(EntitlementEndpointConstants.ATTRIBUTE)
                        .getAsJsonArray()) {
                    attributes.add(jsonObjectToAttribute(jsonElement.getAsJsonObject()));
                }
            }
        }

    }
    //Build the Attributes object using above values
    Attributes attributesObj = new Attributes(category, content, attributes, id);
    categories.add(attributesObj);
}

From source file:se.kth.infosys.login.couchbase.AbstractRegisteredServiceJsonSerializer.java

License:Apache License

/**
 * @see JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
 * @param json JSON encoded representation of CAS Service.
 * @param typeOfT (ignored)./*from  w  w  w. j  ava2 s  .co m*/
 * @param context Gson de-serialization context.
 * @return de-serialized version of a CAS service.
 */
public final AbstractRegisteredService deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    JsonObject jsonObject = json.getAsJsonObject();
    String type = jsonObject.get("type").getAsString();
    JsonElement element = jsonObject.get("properties");
    try {
        return context.deserialize(element, Class.forName(type));
    } catch (final ClassNotFoundException e) {
        throw new JsonParseException("Unknown element type: " + type, e);
    }
}

From source file:uk.ac.ncl.aries.entanglement.revlog.RevisionItemDeserializer.java

License:Apache License

@Override
public RevisionItem deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    String typeName = null;//from w ww.  j  av  a2 s  .  co m
    try {
        //      System.out.println("je: "+je);
        //      System.out.println("Type: "+type);
        //      System.out.println("Context: "+jdc);

        JsonObject rootObj = je.getAsJsonObject();
        typeName = rootObj.get("type").getAsString();
        Class cl = getOperationTypeForName(typeName);
        GraphOperation graphOp = jdc.deserialize(rootObj.get("op"), cl);

        RevisionItem revItem = new RevisionItem();
        revItem.setType(typeName);
        revItem.setOp(graphOp);

        return revItem;
    } catch (Exception e) {
        throw new JsonParseException("Failed to parse subdocument of abstract type: " + type + " to: "
                + typeName + ". Document was:\n" + je, e);
    }
}