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:org.openqa.selenium.json.Json.java

License:Apache License

private static Object readValue(JsonReader in, Gson gson) throws IOException {
    switch (in.peek()) {
    case BEGIN_ARRAY:
    case BEGIN_OBJECT:
    case BOOLEAN:
    case NULL://w w w .ja  va  2 s.  c  om
    case STRING:
        return gson.fromJson(in, Object.class);

    case NUMBER:
        String number = in.nextString();
        if (number.indexOf('.') != -1) {
            return Double.parseDouble(number);
        }
        return Long.parseLong(number);

    default:
        throw new JsonParseException("Unexpected type: " + in.peek());
    }
}

From source file:org.ppojo.data.ArtifactSerializer.java

License:Apache License

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

    if (!json.isJsonObject())
        throw new JsonParseException("Invalid element type for artifact, expected JsonObject got "
                + JsonElementTypes.getType(json) + ", in " + getDeserializeFilePath());
    String artifactName = readStringProperty(json, "", "name");
    String type = readStringProperty(json, artifactName, "type");
    ArtifactTypes artifactTypes = ArtifactTypes.Parse(type);
    if (artifactTypes == ArtifactTypes.Unknown)
        throw new InvalidArtifactType("Invalid artifact type " + type + " at " + artifactName + " in template "
                + getDeserializeFilePath());
    ArtifactMetaData artifactMetaData = ArtifactMetaData.getArtifactMetaData(artifactTypes);
    Map<String, Object> localOptions = null;
    for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
        JsonElementTypes elementType = JsonElementTypes.getType(entry.getValue());
        String propertyName = entry.getKey();
        ArtifactOptions.Fields optionsProperty = ArtifactOptions.Fields.Parse(propertyName);
        if (optionsProperty != ArtifactOptions.Fields.Unknown) {
            ArtifactMetaData.validateOptionsProperty(artifactMetaData, optionsProperty, elementType,
                    artifactName, getDeserializeFilePath());
            Object optionValue = context.deserialize(entry.getValue(), optionsProperty.getOptionType());
            if (optionsProperty.getOptionType().isEnum() && optionValue == null) {
                throw new EnumParseException(entry.getValue().getAsString(), propertyName, artifactName,
                        getDeserializeFilePath());
            }/*from   w w w .j a v a  2 s . c o  m*/
            if (localOptions == null)
                localOptions = new HashMap<>();
            localOptions.put(optionsProperty.toString(), optionValue);
        } else
            ArtifactMetaData.validateArtifactProperty(artifactMetaData, propertyName, elementType, artifactName,
                    getDeserializeFilePath());

        if (elementType == JsonElementTypes.JsonArray) {
            for (JsonElement arrayElement : entry.getValue().getAsJsonArray()) {
                JsonElementTypes arrayElementType = JsonElementTypes.getType(arrayElement);
                if (arrayElementType != JsonElementTypes.String
                        && arrayElementType != JsonElementTypes.JsonObject)
                    throw new JsonArrayMixedItemTypes(
                            "Invalid json array property " + entry.getKey() + " of artifact " + artifactName
                                    + ". Item types must all be String o JsonObject items. in "
                                    + getDeserializeFilePath());
            }
        }
    }
    ArtifactData result;
    result = _gson.fromJson(json, artifactMetaData.getArtifactClass());
    result.options = localOptions;
    return result;
}

From source file:org.ppojo.data.ArtifactSerializer.java

License:Apache License

private String readStringProperty(JsonElement json, String artifact_name, String propertyName,
        boolean isRequired, String defaultValue) {
    JsonElement typeElement = json.getAsJsonObject().get(propertyName);
    if (typeElement == null)
        if (isRequired)
            throw new RequiredPropertyMissing("Required property " + propertyName + " is missing in artifact "
                    + artifact_name + " declaration in " + getDeserializeFilePath());
        else// ww  w.  j ava 2 s .co m
            return defaultValue;
    if (!typeElement.isJsonPrimitive() || !typeElement.getAsJsonPrimitive().isString())
        throw new JsonParseException("Invalid element type for property: " + propertyName + " in artifact "
                + artifact_name + ", expected String got " + JsonElementTypes.getType(typeElement) + ", in "
                + getDeserializeFilePath());
    return typeElement.getAsString();
}

From source file:org.ppojo.data.CopyStyleDataSerializer.java

License:Apache License

@Override
public CopyStyleData[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonArray())
        throw new JsonParseException("Invalid element type for option pojoCopyStyles, expected JsonArray got "
                + JsonElementTypes.getType(json) + ", in " + getDeserializeFilePath());
    JsonArray array = json.getAsJsonArray();
    if (array.size() == 0)
        return EmptyArray.get(CopyStyleData.class);
    CopyStyleData[] result = new CopyStyleData[array.size()];
    int memberIndex = 0;
    for (JsonElement element : array) {
        result[memberIndex] = ParseJsonArrayElement(element, memberIndex);
        memberIndex++;/*w w w. j a  va 2s. co  m*/
    }
    Set<CopyStyleTypes> styleTypes = new HashSet<>();
    for (CopyStyleData copyStyleData : result) {
        if (!styleTypes.contains(copyStyleData.style))
            styleTypes.add(copyStyleData.style);
        else
            throw new JsonParseException(
                    "Invalid definition for option pojoCopyStyles, duplicate entries for style type "
                            + copyStyleData.style + " exist. This is not allowed in "
                            + getDeserializeFilePath());
    }
    return result;
}

From source file:org.ppojo.data.CopyStyleDataSerializer.java

License:Apache License

private CopyStyleData ParseJsonArrayElement(JsonElement element, int memberIndex) {
    CopyStyleData copyStyleData;//  w w  w .  j  a  v  a2  s  .  c  o m
    JsonElementTypes elementType = JsonElementTypes.getType(element);
    switch (elementType) {
    case String:
        copyStyleData = ParseStyleStringElement(element.getAsString(), memberIndex);
        break;
    case JsonObject:
        copyStyleData = ParseStyleJsonObject(element.getAsJsonObject(), memberIndex);
        break;
    default:
        throw new JsonParseException("Invalid element type for option pojoCopyStyles[" + memberIndex
                + "], expected String or JsonObject got " + elementType + ", in " + getDeserializeFilePath());
    }
    if (copyStyleData.methodName == null)
        copyStyleData.methodName = copyStyleData.style.getDefaultMethodName();
    else if (copyStyleData.methodName.equals("") && copyStyleData.style != CopyStyleTypes.copyConstructor)
        throw new JsonParseException("Invalid field value option pojoCopyStyles[" + memberIndex
                + "].methodName, value can not be empty, in " + getDeserializeFilePath());
    return copyStyleData;
}

From source file:org.ppojo.data.CopyStyleDataSerializer.java

License:Apache License

private CopyStyleData ParseStyleJsonObject(JsonObject asObject, int memberIndex) {
    CopyStyleData copyStyleData = new CopyStyleData();
    JsonElement styleMember = null;/*from  w  w w. j ava 2 s . co  m*/
    JsonElement methodNameMember = null;
    for (Map.Entry<String, JsonElement> elementEntry : asObject.entrySet()) {
        switch (elementEntry.getKey()) {
        case "style":
            styleMember = elementEntry.getValue();
            break;
        case "methodName":
            methodNameMember = elementEntry.getValue();
            break;
        default:
            throw new JsonParseException("Invalid member for array element pojoCopyStyles[" + memberIndex
                    + "], unsupported field " + elementEntry.getKey()
                    + ". Only supports fields style and methodName, in " + getDeserializeFilePath());
        }
    }
    if (styleMember == null)
        throw new JsonParseException("Invalid element in option pojoCopyStyles[" + memberIndex
                + "], required field: style is missing, in " + getDeserializeFilePath());
    if (!(styleMember.isJsonPrimitive() && styleMember.getAsJsonPrimitive().isString()))
        throw new JsonParseException("Invalid element type for option pojoCopyStyles[" + memberIndex
                + "] style field, expected String got " + JsonElementTypes.getType(styleMember) + ", in "
                + getDeserializeFilePath());
    if (methodNameMember != null
            && !(methodNameMember.isJsonPrimitive() && methodNameMember.getAsJsonPrimitive().isString()))
        throw new JsonParseException("Invalid element type for option pojoCopyStyles[" + memberIndex
                + "]  methodName field, expected String got " + JsonElementTypes.getType(methodNameMember)
                + ", in " + getDeserializeFilePath());
    copyStyleData.style = parseStyleType(styleMember.getAsString(), memberIndex);
    if (methodNameMember != null)
        copyStyleData.methodName = EmptyIfNull(methodNameMember.getAsString());
    return copyStyleData;
}

From source file:org.ppojo.data.TemplateSerializer.java

License:Apache License

@Override
public TemplateFileData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject())
        throw new JsonParseException("Invalid element type for template, expected JsonObject got "
                + JsonElementTypes.getType(json) + ", in " + getDeserializeFilePath());
    ArtifactData[] artifacts = null;// ww w. j  av  a 2  s .c  o  m
    Map<String, Object> options = null;
    for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
        JsonElementTypes elementType = JsonElementTypes.getType(entry.getValue());
        String propertyName = entry.getKey();
        validateTemplateProperty(propertyName, elementType, getDeserializeFilePath());
        switch (propertyName) {
        case TemplateFileData.Fields.ARTIFACTS:
            artifacts = context.deserialize(entry.getValue(), ArtifactData[].class);
            break;
        case TemplateFileData.Fields.OPTIONS:
            options = deserializeOptions(entry.getValue(), context);
            break;
        }
    }
    TemplateFileData result = _gson.fromJson(json, TemplateFileData.class);
    result.artifacts = artifacts;
    result.options = options;
    return result;
}

From source file:org.projectbuendia.client.json.LocalDateSerializer.java

License:Apache License

@Override
public LocalDate deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    String text = json.getAsString();
    try {/*from   ww  w.ja  va2  s .co m*/
        return LocalDate.parse(text);
    } catch (IllegalArgumentException e) {
        throw new JsonParseException(e);
    }
}

From source file:org.rapla.rest.gwtjsonrpc.server.CallDeserializer.java

License:Apache License

@Override
public ActiveCall deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException, NoSuchRemoteMethodException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Expected object");
    }/*from  w w w . j  a  v  a  2  s . com*/

    final JsonObject in = json.getAsJsonObject();

    req.id = in.get("id");

    final JsonElement jsonrpc = in.get("jsonrpc");
    final JsonElement version = in.get("version");
    if (isString(jsonrpc) && version == null) {
        final String v = jsonrpc.getAsString();
        if ("2.0".equals(v)) {
            req.versionName = "jsonrpc";
            req.versionValue = jsonrpc;
        } else {
            throw new JsonParseException("Expected jsonrpc=2.0");
        }

    } else if (isString(version) && jsonrpc == null) {
        final String v = version.getAsString();
        if ("1.1".equals(v)) {
            req.versionName = "version";
            req.versionValue = version;
        } else {
            throw new JsonParseException("Expected version=1.1");
        }
    } else {
        throw new JsonParseException("Expected version=1.1 or jsonrpc=2.0");
    }

    final JsonElement method = in.get("method");
    if (!isString(method)) {
        throw new JsonParseException("Expected method name as string");
    }

    String asString = method.getAsString();
    req.method = server.lookupMethod(asString);
    if (req.method == null) {
        throw new NoSuchRemoteMethodException(server.getInterfaceClass() + "." + asString);
    }

    final Type[] paramTypes = req.method.getParamTypes();
    final JsonElement params = in.get("params");
    if (params != null) {
        if (!params.isJsonArray()) {
            throw new JsonParseException("Expected params array");
        }

        final JsonArray paramsArray = params.getAsJsonArray();
        if (paramsArray.size() != paramTypes.length) {
            throw new JsonParseException("Expected " + paramTypes.length + " parameter values in params array");
        }

        final Object[] r = new Object[paramTypes.length];
        for (int i = 0; i < r.length; i++) {
            final JsonElement v = paramsArray.get(i);
            if (v != null) {
                r[i] = context.deserialize(v, paramTypes[i]);
            }
        }
        req.params = r;
    } else {
        if (paramTypes.length != 0) {
            throw new JsonParseException("Expected params array");
        }
        req.params = JsonServlet.NO_PARAMS;
    }

    return req;
}

From source file:org.rapla.rest.gwtjsonrpc.server.JsonServlet.java

License:Apache License

private String readBody(final ActiveCall call) throws IOException {
    if (!isBodyJson(call)) {
        throw new JsonParseException("Invalid Request Content-Type");
    }/*from  www .j  a v  a2  s. c  o  m*/
    if (!isBodyUTF8(call)) {
        throw new JsonParseException("Invalid Request Character-Encoding");
    }

    final int len = call.httpRequest.getContentLength();
    if (len < 0) {
        throw new JsonParseException("Invalid Request Content-Length");
    }
    if (len == 0) {
        throw new JsonParseException("Invalid Request POST Body Required");
    }
    if (len > maxRequestSize()) {
        throw new JsonParseException("Invalid Request POST Body Too Large");
    }

    final InputStream in = call.httpRequest.getInputStream();
    if (in == null) {
        throw new JsonParseException("Invalid Request POST Body Required");
    }

    try {
        final byte[] body = new byte[len];
        int off = 0;
        while (off < len) {
            final int n = in.read(body, off, len - off);
            if (n <= 0) {
                throw new JsonParseException("Invalid Request Incomplete Body");
            }
            off += n;
        }

        final CharsetDecoder d = Charset.forName(JsonConstants.JSON_ENC).newDecoder();
        d.onMalformedInput(CodingErrorAction.REPORT);
        d.onUnmappableCharacter(CodingErrorAction.REPORT);
        try {
            ByteBuffer wrap = ByteBuffer.wrap(body);
            CharBuffer decode = d.decode(wrap);
            return decode.toString();
        } catch (CharacterCodingException e) {
            throw new JsonParseException("Invalid Request Not UTF-8", e);
        }
    } finally {
        in.close();
    }
}