Example usage for com.google.gson JsonDeserializationContext deserialize

List of usage examples for com.google.gson JsonDeserializationContext deserialize

Introduction

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

Prototype

public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;

Source Link

Document

Invokes default deserialization on the specified object.

Usage

From source file:com.continuuity.weave.internal.json.RuntimeSpecificationCodec.java

License:Open Source License

@Override
public RuntimeSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();

    String name = jsonObj.get("name").getAsString();
    WeaveRunnableSpecification runnable = context.deserialize(jsonObj.get("runnable"),
            WeaveRunnableSpecification.class);
    ResourceSpecification resources = context.deserialize(jsonObj.get("resources"),
            ResourceSpecification.class);
    Collection<LocalFile> files = context.deserialize(jsonObj.get("files"),
            new TypeToken<Collection<LocalFile>>() {
            }.getType());/*from w  w w .j a  v  a  2 s.co m*/

    return new DefaultRuntimeSpecification(name, runnable, resources, files);
}

From source file:com.continuuity.weave.internal.json.WeaveRunnableSpecificationCodec.java

License:Open Source License

@Override
public WeaveRunnableSpecification deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();

    String className = jsonObj.get("classname").getAsString();
    String name = jsonObj.get("name").getAsString();
    Map<String, String> arguments = context.deserialize(jsonObj.get("arguments"),
            new TypeToken<Map<String, String>>() {
            }.getType());/*from   ww  w  . j  a  v a 2 s .  co  m*/

    return new DefaultWeaveRunnableSpecification(className, name, arguments);
}

From source file:com.continuuity.weave.internal.json.WeaveSpecificationCodec.java

License:Open Source License

@Override
public WeaveSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();

    String name = jsonObj.get("name").getAsString();
    Map<String, RuntimeSpecification> runnables = context.deserialize(jsonObj.get("runnables"),
            new TypeToken<Map<String, RuntimeSpecification>>() {
            }.getType());/*from   w w w .ja  v a2s .  c o m*/
    List<WeaveSpecification.Order> orders = context.deserialize(jsonObj.get("orders"),
            new TypeToken<List<WeaveSpecification.Order>>() {
            }.getType());

    return new DefaultWeaveSpecification(name, runnables, orders);
}

From source file:com.continuuity.weave.internal.logging.LogEntryDecoder.java

License:Open Source License

@Override
public LogEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        return null;
    }//  w w w  . j a  va2s  . c om
    JsonObject jsonObj = json.getAsJsonObject();

    final String name = jsonObj.get("name").getAsString();
    final String host = jsonObj.get("host").getAsString();
    final long timestamp = Long.parseLong(jsonObj.get("timestamp").getAsString());
    final LogEntry.Level level = LogEntry.Level.valueOf(jsonObj.get("level").getAsString());
    final String className = jsonObj.get("className").getAsString();
    final String method = jsonObj.get("method").getAsString();
    final String file = jsonObj.get("file").getAsString();
    final String line = jsonObj.get("line").getAsString();
    final String thread = jsonObj.get("thread").getAsString();
    final String message = jsonObj.get("message").getAsString();

    final StackTraceElement[] stackTraces = context.deserialize(jsonObj.get("stackTraces").getAsJsonArray(),
            StackTraceElement[].class);

    return new LogEntry() {
        @Override
        public String getLoggerName() {
            return name;
        }

        @Override
        public String getHost() {
            return host;
        }

        @Override
        public long getTimestamp() {
            return timestamp;
        }

        @Override
        public Level getLogLevel() {
            return level;
        }

        @Override
        public String getSourceClassName() {
            return className;
        }

        @Override
        public String getSourceMethodName() {
            return method;
        }

        @Override
        public String getFileName() {
            return file;
        }

        @Override
        public int getLineNumber() {
            if (line.equals("?")) {
                return -1;
            } else {
                return Integer.parseInt(line);
            }
        }

        @Override
        public String getThreadName() {
            return thread;
        }

        @Override
        public String getMessage() {
            return message;
        }

        @Override
        public StackTraceElement[] getStackTraces() {
            return stackTraces;
        }
    };
}

From source file:com.crowdmap.java.sdk.json.UsersDeserializer.java

License:Open Source License

@Override
public List<User> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    List<User> users = new ArrayList<User>();
    if (json.isJsonArray()) {
        for (JsonElement e : json.getAsJsonArray()) {
            users.add((User) context.deserialize(e, User.class));
        }//from  w w w.  ja va  2 s. c om
    } else if (json.isJsonObject()) {
        users.add((User) context.deserialize(json, User.class));
    } else {
        throw new CrowdmapException("Unexpected JSON type" + json.getClass());
    }

    return users;
}

From source file:com.epishie.tabs.gson.ThingDeserializer.java

License:Apache License

@Override
public Thing deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Expected json object");
    }/* w w  w.  j a  va2s. co m*/
    JsonObject jsonObject = json.getAsJsonObject();
    if (!jsonObject.has(KIND)) {
        throw new JsonParseException("Expected to have \"kind\" field");
    }
    JsonElement kindElement = jsonObject.get(KIND);
    if (!kindElement.isJsonPrimitive()) {
        throw new JsonParseException("Expected to have a primitive \"kind\" field");
    }
    if (!jsonObject.has(DATA)) {
        throw new JsonParseException("Expected to have a\"data\" field");
    }
    String kind = kindElement.getAsString();
    String id = null;
    String name = null;
    if (jsonObject.has(ID)) {
        JsonElement idElement = jsonObject.get(ID);
        id = idElement.getAsString();
    }
    if (jsonObject.has(NAME)) {
        JsonElement nameElement = jsonObject.get(NAME);
        name = nameElement.getAsString();
    }
    Thing thing = null;
    switch (kind) {
    case Link.KIND:
        Link link = context.deserialize(jsonObject.get(DATA), Link.class);
        thing = new Thing<>(id, name, link);
        break;
    case Listing.KIND:
        Listing listing = context.deserialize(jsonObject.get(DATA), Listing.class);
        // noinspection unchecked
        thing = new Thing(id, name, listing);
        break;
    case Subreddit.KIND:
        Subreddit subreddit = context.deserialize(jsonObject.get(DATA), Subreddit.class);
        // noinspection unchecked
        thing = new Thing(id, name, subreddit);
        break;
    case Comment.KIND:
        Comment comment = context.deserialize(jsonObject.get(DATA), Comment.class);
        // noinspection unchecked
        thing = new Thing(id, name, comment);
        break;
    }
    return thing;
}

From source file:com.evilco.license.common.data.holder.LicenseHolderJsonAdapter.java

License:Apache License

/**
 * {@inheritDoc}/*w  w w.j a  v  a  2s.c o  m*/
 */
@Override
public ILicenseHolder deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    // get implementation name
    try {
        // get class instance
        Class<? extends ILicenseHolder> licenseClass = Class
                .forName(json.getAsJsonObject().get("implementationClassName").getAsString())
                .asSubclass(ILicenseHolder.class);

        // de-serialize
        return context.deserialize(json, licenseClass);
    } catch (ClassCastException ex) {
        throw new JsonParseException(ex.getMessage(), ex);
    } catch (ClassNotFoundException ex) {
        throw new JsonParseException(ex.getMessage(), ex);
    }
}

From source file:com.facebook.buck.intellij.plugin.ws.buckevents.BuckEventAdapter.java

License:Apache License

@Override
public BuckEventInterface deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonPrimitive prim = (JsonPrimitive) jsonObject.get("type");
    String typeFromJSON = prim.getAsString();

    if (MAPPINGS.containsKey(typeFromJSON)) {
        return jsonDeserializationContext.deserialize(jsonElement, MAPPINGS.get(typeFromJSON));
    }//from   w  w  w. jav  a2s  . c o m

    return jsonDeserializationContext.deserialize(jsonElement, BuckEventUnknown.class);
}

From source file:com.github.abel533.echarts.json.AxisDeserializer.java

License:Open Source License

@Override
/**/*  w  w  w. j a  va  2s.  c  om*/
 * json,typeOfT,context
 *
 * @param json
 * @param typeOfT
 * @param context
 */
public Axis deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    String _type = jsonObject.get("type").getAsString();
    AxisType type = AxisType.valueOf(_type);
    Axis axis = null;
    switch (type) {
    case category:
        axis = context.deserialize(jsonObject, CategoryAxis.class);
        break;
    case value:
        axis = context.deserialize(jsonObject, ValueAxis.class);
        break;
    case time:
        axis = context.deserialize(jsonObject, TimeAxis.class);
        break;
    }
    return axis;
}

From source file:com.github.abel533.echarts.json.SeriesDeserializer.java

License:Open Source License

@Override
/**//from w  w w.j  a  va 2  s  .  c  o  m
 * json,typeOfT,context
 *
 * @param json
 * @param typeOfT
 * @param context
 */
public Series deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    String _type = jsonObject.get("type").getAsString();
    SeriesType type = SeriesType.valueOf(_type);
    Series series = null;
    switch (type) {
    case line:
        series = context.deserialize(jsonObject, Line.class);
        break;
    case bar:
        series = context.deserialize(jsonObject, Bar.class);
        break;
    case scatter:
        series = context.deserialize(jsonObject, Scatter.class);
        break;
    case funnel:
        series = context.deserialize(jsonObject, Funnel.class);
        break;
    case pie:
        series = context.deserialize(jsonObject, Pie.class);
        break;
    case force:
        series = context.deserialize(jsonObject, Force.class);
        break;
    case gauge:
        series = context.deserialize(jsonObject, Gauge.class);
        break;
    case map:
        series = context.deserialize(jsonObject, Map.class);
        break;
    case island:
        series = context.deserialize(jsonObject, Island.class);
        break;
    case k:
        series = context.deserialize(jsonObject, K.class);
        break;
    case radar:
        series = context.deserialize(jsonObject, Radar.class);
        break;
    case chord:
        series = context.deserialize(jsonObject, Chord.class);
        break;
    }
    return series;
}