Search in sources :

Example 91 with JsonParseException

use of com.google.gson.JsonParseException in project maple-ir by LLVM-but-worse.

the class FieldInsnNodeSerializer method deserialize.

@Override
public FieldInsnNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    int opcode;
    String owner = null, name = null, desc = null;
    JsonObject object = (JsonObject) json;
    opcode = object.get("opcode").getAsInt();
    owner = object.get("owner").getAsString();
    name = object.get("name").getAsString();
    desc = object.get("desc").getAsString();
    if (owner == null || name == null || desc == null)
        throw new JsonParseException("Could not parse FieldInsnNode");
    return new FieldInsnNode(opcode, owner, name, desc);
}
Also used : JsonObject(com.google.gson.JsonObject) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) JsonParseException(com.google.gson.JsonParseException)

Example 92 with JsonParseException

use of com.google.gson.JsonParseException in project cosmic by MissionCriticalCloud.

the class RoutingConfigAdapter method deserialize.

@Override
public RoutingConfig deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
    final JsonObject jsonObject = jsonElement.getAsJsonObject();
    if (!jsonObject.has("type")) {
        throw new JsonParseException("Deserializing as a RoutingConfig, but no type present in the json object");
    }
    final String routingConfigType = jsonObject.get("type").getAsString();
    if (SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG.equals(routingConfigType)) {
        return context.deserialize(jsonElement, SingleDefaultRouteImplicitRoutingConfig.class);
    } else if (ROUTING_TABLE_ROUTING_CONFIG.equals(routingConfigType)) {
        return context.deserialize(jsonElement, RoutingTableRoutingConfig.class);
    }
    throw new JsonParseException("Failed to deserialize type \"" + routingConfigType + "\"");
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 93 with JsonParseException

use of com.google.gson.JsonParseException in project cosmic by MissionCriticalCloud.

the class NatRuleAdapter method deserialize.

@Override
public NatRule deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
    final JsonObject jsonObject = jsonElement.getAsJsonObject();
    if (!jsonObject.has("type")) {
        throw new JsonParseException("Deserializing as a NatRule, but no type present in the json object");
    }
    final String natRuleType = jsonObject.get("type").getAsString();
    if ("SourceNatRule".equals(natRuleType)) {
        return context.deserialize(jsonElement, SourceNatRule.class);
    } else if ("DestinationNatRule".equals(natRuleType)) {
        return context.deserialize(jsonElement, DestinationNatRule.class);
    }
    throw new JsonParseException("Failed to deserialize type \"" + natRuleType + "\"");
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 94 with JsonParseException

use of com.google.gson.JsonParseException in project hub-alert by blackducksoftware.

the class DistributionJobDetailsModelJsonAdapterTest method deserializeThrowsJsonParseExceptionTest.

@Test
public void deserializeThrowsJsonParseExceptionTest() {
    String testFieldValue = "a value";
    DistributionJobDetailsModel baseModel = new Test_DistributionJobDetailsModel(testFieldValue);
    JsonElement jsonElement = gson.toJsonTree(baseModel);
    DistributionJobDetailsModelJsonAdapter deserializer = new DistributionJobDetailsModelJsonAdapter();
    try {
        deserializer.deserialize(jsonElement, DistributionJobDetailsModel.class, jsonDeserializationContext);
        fail("Expected exception: " + JsonParseException.class);
    } catch (JsonParseException e) {
    // Success
    }
}
Also used : JsonElement(com.google.gson.JsonElement) JsonParseException(com.google.gson.JsonParseException) Test(org.junit.jupiter.api.Test)

Example 95 with JsonParseException

use of com.google.gson.JsonParseException in project java-crud-api by kolchagov.

the class TestApi method getMockHttpServletRequest.

private MockHttpServletRequest getMockHttpServletRequest() throws UnsupportedEncodingException {
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setServerName("localhost");
    req.setMethod(method);
    final UriComponents build = UriComponentsBuilder.fromUriString(this.baseUrl).build();
    req.setPathInfo(build.getPath());
    req.setQueryString(build.getQuery());
    setParamsFromBuild(req, build);
    if (data != null) {
        try {
            if (data.endsWith("__is_null"))
                throw new JsonParseException("");
            // invalid json test expects json content
            if (!"{\"}".equals(data)) {
                parser.parse(data);
            }
            req.setContentType("application/json");
        } catch (JsonParseException ignored) {
            req.setContentType("application/x-www-form-urlencoded");
            final String url = "/?" + URLDecoder.decode(data, "utf8");
            setParamsFromBuild(req, UriComponentsBuilder.fromUriString(url).build());
        }
        req.setContent(data.getBytes("utf8"));
    }
    return req;
}
Also used : UriComponents(org.springframework.web.util.UriComponents) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JsonParseException(com.google.gson.JsonParseException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)267 JsonObject (com.google.gson.JsonObject)105 IOException (java.io.IOException)78 JsonElement (com.google.gson.JsonElement)71 Gson (com.google.gson.Gson)39 JsonArray (com.google.gson.JsonArray)30 JsonReader (com.google.gson.stream.JsonReader)28 InputStreamReader (java.io.InputStreamReader)28 JsonParser (com.google.gson.JsonParser)25 JsonPrimitive (com.google.gson.JsonPrimitive)25 Map (java.util.Map)25 InputStream (java.io.InputStream)23 GsonBuilder (com.google.gson.GsonBuilder)20 ArrayList (java.util.ArrayList)20 Type (java.lang.reflect.Type)17 JsonWriter (com.google.gson.stream.JsonWriter)15 StringReader (java.io.StringReader)13 HashMap (java.util.HashMap)13 LinkedHashMap (java.util.LinkedHashMap)13 HttpUrl (okhttp3.HttpUrl)13