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.berniesanders.fieldthebern.parsing.CanvassDataSerializer.java

License:Apache License

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

    CanvassData typedContent = null;// w w  w .j  a v a 2 s  .  co m
    switch (json.getAsJsonObject().get("type").getAsString()) {
    case ApiAddress.TYPE:
        typedContent = context.deserialize(json, ApiAddress.class);
        break;
    case Person.TYPE:
        typedContent = context.deserialize(json, Person.class);
        break;
    case UserData.TYPE:
        typedContent = context.deserialize(json, UserData.class);
        break;
    default:
        throw new JsonParseException("unknown type:" + json.getAsJsonObject().get("type").getAsString());
    }

    return typedContent;
}

From source file:com.berniesanders.fieldthebern.parsing.CollectionDeserializer.java

License:Apache License

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

    ApiItem typedContent = null;//from w ww.  j a  v a  2 s  . co  m
    switch (json.getAsJsonObject().get("type").getAsString()) {
    case "collection":
        typedContent = context.deserialize(json, Collection.class);
        break;
    case "page":
        typedContent = context.deserialize(json, Page.class);
        break;
    default:
        throw new JsonParseException("unknown type:" + json.getAsJsonObject().get("type").getAsString());
    }

    return typedContent;
}

From source file:com.berniesanders.fieldthebern.parsing.PageContentDeserializer.java

License:Apache License

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

    //Gson gson = new Gson(); //use a temp gson so we don't trigger a stack overflow.
    //Content untypedContent = gson.fromJson(json, Content.class);

    Content typedContent = null;//from   w w  w .j ava2  s  . c  o  m
    switch (json.getAsJsonObject().get("type").getAsString()) {
    case "h1":
        typedContent = context.deserialize(json, H1.class);
        break;
    case "h2":
        typedContent = context.deserialize(json, H2.class);
        break;
    case "h3":
        typedContent = context.deserialize(json, H3.class);
        break;
    case "p":
        typedContent = context.deserialize(json, P.class);
        break;
    case "img":
        typedContent = context.deserialize(json, Img.class);
        break;
    case "nav":
        try {
            typedContent = context.deserialize(json, Nav.class);
        } catch (JsonSyntaxException jsonEx) {
            Timber.e(jsonEx, "Error deserializing a JSON object");
            //use an empty nav item, there is an error in the ftb json
            typedContent = new Nav();
        }
        break;
    case "video":
        typedContent = context.deserialize(json, Video.class);
        break;
    case "quote":
        typedContent = context.deserialize(json, Quote.class);
        break;
    case "list":
        typedContent = context.deserialize(json, List.class);
        break;
    case "iframe":
        typedContent = context.deserialize(json, Iframe.class);
        break;
    default:
        throw new JsonParseException("unknown type:" + json.getAsJsonObject().get("type").getAsString());
    }

    return typedContent;
}

From source file:com.birbit.jsonapi.JsonApiDeserializer.java

License:Apache License

public JsonApiResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("JSON API response root should be a json object");
    }/*from w  w w  .j a va 2  s  .co m*/
    if (!(typeOfT instanceof ParameterizedType)) {
        throw new JsonParseException("JSON API response should be requested with a parameterized type where the"
                + " type parameter represents the `data` field's type");
    }
    ParameterizedType parameterizedType = (ParameterizedType) typeOfT;
    JsonObject jsonObject = json.getAsJsonObject();

    JsonApiLinks links = parseLinks(context, jsonObject);

    T[] data = parseData(context, parameterizedType, jsonObject);
    List<JsonApiError> errors = parserErrors(context, jsonObject);
    if ((data == null) == (errors == null)) {
        throw new JsonParseException("The JSON API response should have data or errors");
    }
    if (errors != null) {
        return new JsonApiResponse(errors, typeMapping, links);
    }
    Map<String, Map<String, Object>> included = parseIncluded(context, jsonObject);
    //noinspection unchecked
    return new JsonApiResponse(data, included, typeMapping, links);
}

From source file:com.birbit.jsonapi.JsonApiLinksDeserializer.java

License:Apache License

@Override
public JsonApiLinks deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("JsonApiLinks json element must be an object");
    }//from w ww .ja v  a  2s . c om
    JsonObject asJsonObject = json.getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entries = asJsonObject.entrySet();
    if (entries.isEmpty()) {
        return JsonApiLinks.EMPTY;
    }
    Map<String, JsonApiLinkItem> result = new HashMap<String, JsonApiLinkItem>();
    for (Map.Entry<String, JsonElement> entry : asJsonObject.entrySet()) {
        JsonElement value = entry.getValue();
        if (value.isJsonPrimitive()) {
            result.put(entry.getKey(), new JsonApiLinkItem(entry.getValue().getAsString()));
        } else {
            result.put(entry.getKey(),
                    context.<JsonApiLinkItem>deserialize(entry.getValue(), JsonApiLinkItem.class));
        }
    }
    return new JsonApiLinks(result);
}

From source file:com.birbit.jsonapi.JsonApiResourceDeserializer.java

License:Apache License

@SuppressWarnings("WeakerAccess")
public T deserialize(String id, JsonElement json, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("expected a json object to parse into " + klass + " but received " + json);
    }/*from   w  w  w  .ja v a 2s  . c  om*/
    T t = null;
    try {
        JsonObject jsonObject = json.getAsJsonObject();
        t = parseObject(context, id, jsonObject);
        parseRelationships(context, t, jsonObject);
        parseLinks(context, t, jsonObject);
    } catch (IllegalAccessException e) {
        throw new JsonParseException("Cannot set ID/link on " + t, e);
    } catch (InvocationTargetException e) {
        throw new JsonParseException("Cannot set ID/link on " + t, e);
    } catch (InstantiationException e) {
        throw new JsonParseException(
                "Cannot create an instance of " + klass + ". Make sure it has a no-arg" + " constructor", e);
    }
    return t;
}

From source file:com.blackducksoftware.integration.hub.teamcity.server.global.ServerHubConfigPersistenceManager.java

License:Apache License

public void loadSettings() {
    if (configFile.exists()) {
        try (BufferedReader reader = new BufferedReader(new FileReader(configFile))) {
            final JsonObject globalConfigJson = jsonParser.parse(reader).getAsJsonObject();
            try {
                if (globalConfigJson.has("hubServerConfig")) {
                    setHubServerConfig(//from   w ww .j  a v a2 s .  co  m
                            gson.fromJson(globalConfigJson.get("hubServerConfig"), HubServerConfig.class));
                    setHubWorkspaceCheck(globalConfigJson.get("hubWorkspaceCheck").getAsBoolean());
                } else {
                    throw new JsonParseException(
                            "The Hub Teamcity configuration must be from a previous version.");
                }
            } catch (final JsonParseException e) {
                // try to load the old config
                setHubServerConfig(gson.fromJson(globalConfigJson, HubServerConfig.class));
                setHubWorkspaceCheck(true);
            }
        } catch (final IOException e) {
            Loggers.SERVER.error("Failed to load Hub config file: " + configFile, e);
        }
    }
}

From source file:com.bzcentre.dapiPush.ReceipientTypeAdapter.java

License:Open Source License

private MeetingPayload extractPayload(JsonReader in)
        throws IOException, NumberFormatException, IllegalStateException, JsonParseException {
    NginxClojureRT.log.debug(TAG + "TypeAdapter extracting Payload...");

    MeetingPayload meetingPayload = new MeetingPayload();
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*w  w w  . j a  v a 2 s .  c  o m*/
        throw new JsonParseException("null Payload");
    }

    in.beginObject();
    while (in.hasNext()) {
        switch (in.nextName()) {
        case "aps":
            in.beginObject();
            while (in.hasNext()) {
                switch (in.nextName()) {
                case "badge":
                    meetingPayload.getAps().setBadge(in.nextLong());
                    break;
                case "sound":
                    meetingPayload.getAps().setSound(in.nextString());
                    break;
                case "alert":
                    in.beginObject();
                    while (in.hasNext()) {
                        switch (in.nextName()) {
                        case "title":
                            meetingPayload.getAps().getAlert().setTitle(in.nextString());
                            break;
                        case "body":
                            meetingPayload.getAps().getAlert().setBody(in.nextString());
                            break;
                        case "action-loc-key":
                            meetingPayload.getAps().getAlert().setActionLocKey(in.nextString());
                            break;
                        }
                    }
                    in.endObject();
                    break;
                }
            }
            in.endObject();
            break;
        case "dapi":
            meetingPayload.setDapi(in.nextString());
            break;
        case "acme1":
            meetingPayload.setAcme1(in.nextString());
            break;
        case "acme2":
            meetingPayload.setAcme2(in.nextLong());
            break;
        case "acme3":
            meetingPayload.setAcme3(in.nextLong());
            break;
        case "acme4":
            NginxClojureRT.log.info(TAG + "TypeAdapter Reader is reading acme4...");
            meetingPayload.setAcme4(in.nextLong());
            break;
        case "acme5":
            meetingPayload.setAcme5(in.nextLong());
            break;
        case "acme6":
            meetingPayload.setAcme6(in.nextLong());
            break;
        case "acme7":
            ArrayList<String> attendees = new ArrayList<>();
            in.beginArray();
            while (in.hasNext()) {
                attendees.add(in.nextString());
            }
            in.endArray();
            meetingPayload.setAcme7(attendees);
            break;
        case "acme8":
            meetingPayload.setAcme8(in.nextString());
            break;
        }
    }
    in.endObject();

    return meetingPayload;
}

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

License:Apache License

static Object decodeValue(JsonElement jsonElement) {
    if (jsonElement == null || jsonElement.isJsonNull()) {
        return null;
    }/*  w ww.j  a  v a  2 s  . c  om*/
    if (!jsonElement.isJsonPrimitive()) {
        throw new JsonParseException("Illegal JSON detected");
    }
    final JsonPrimitive value = (JsonPrimitive) jsonElement;

    if (value.isString()) {
        return value.getAsString();
    } else if (value.isBoolean()) {
        return value.getAsBoolean();
    } else if (value.isNumber()) {
        return value.getAsNumber();
    }
    throw new JsonParseException("Currently only String, Boolean, or Number are allowed as primitives");
}

From source file:com.cardinity.json.UtcDateTypeAdapter.java

License:Apache License

@Override
public Date read(JsonReader in) throws IOException {
    try {/*from  w  w w .  jav a2 s  .  co  m*/
        switch (in.peek()) {
        case NULL:
            in.nextNull();
            return null;
        default:
            String date = in.nextString();
            // Instead of using iso8601Format.parse(value), we use Jackson's date parsing
            // This is because Android doesn't support XXX because it is JDK 1.6
            return parse(date, new ParsePosition(0));
        }
    } catch (ParseException e) {
        throw new JsonParseException(e);
    }
}