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:co.aurasphere.botmill.fb.internal.util.json.IncomingMessageDeserializer.java

License:Open Source License

public IncomingMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    boolean isEcho = false;

    if (json.getAsJsonObject().get("is_echo") != null && json.getAsJsonObject().get("is_echo").isJsonNull()) {
        isEcho = json.getAsJsonObject().get("is_echo").getAsBoolean();
    }/* ww  w. j  a va  2s  .c om*/

    Class<? extends IncomingMessage> incomingMessageClass = null;
    if (isEcho == true) {
        incomingMessageClass = EchoMessage.class;
    } else {
        incomingMessageClass = ReceivedMessage.class;
    }
    return context.deserialize(json, incomingMessageClass);
}

From source file:co.aurasphere.botmill.kik.util.json.IncomingMessagesDeserializer.java

License:Open Source License

@SuppressWarnings("incomplete-switch")
public MessageCallback deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    MessageCallback messageCallback = new MessageCallback();
    //   The message callback can have multiple message bodies.
    JsonElement jsonMessages = json.getAsJsonObject().get("messages");

    for (int i = 0; i < jsonMessages.getAsJsonArray().size(); i++) {
        //   get type.
        JsonObject jsonMessage = jsonMessages.getAsJsonArray().get(i).getAsJsonObject();
        String typeString = jsonMessage.get("type").getAsString();
        MessageType messageType = MessageType.valueOf(typeString.replace('-', '_').toUpperCase());
        Class<? extends Message> messageClass = null;
        switch (messageType) {
        case TEXT:
            messageClass = TextMessage.class;
            break;
        case PICTURE:
            messageClass = PictureMessage.class;
            break;
        case VIDEO:
            messageClass = VideoMessage.class;
            break;
        case LINK:
            messageClass = LinkMessage.class;
            break;
        case SCAN_DATA:
            messageClass = ScanDataMessage.class;
            break;
        case STICKER:
            messageClass = StickerMessage.class;
            break;
        case FRIEND_PICKER:
            messageClass = FriendPickerMessage.class;
            break;
        case IS_TYPING:
            messageClass = IsTypingMessage.class;
            break;
        case READ_RECEIPT:
            messageClass = ReadReceiptMessage.class;
            break;
        case START_CHATTING:
            messageClass = StartChattingMessage.class;
            break;
        }/*from  ww w .j  a v a  2  s  . c o  m*/

        Message message = context.deserialize(jsonMessage, messageClass);
        messageCallback.addMessage(message);
    }

    return messageCallback;
}

From source file:co.cask.cdap.api.dataset.lib.partitioned.ComparableCodec.java

License:Apache License

@Nullable
protected Comparable deserializeComparable(@Nullable JsonElement comparableJson,
        JsonDeserializationContext jsonDeserializationContext) {
    if (comparableJson == null) {
        return null;
    }/*from ww w  .  j  av  a  2 s.  com*/
    JsonArray jsonArray = comparableJson.getAsJsonArray();
    // the classname is serialized as the first element, the value is serialized as the second
    Class<? extends Comparable> comparableClass = forName(jsonArray.get(0).getAsString());
    return jsonDeserializationContext.deserialize(jsonArray.get(1), comparableClass);
}

From source file:co.cask.cdap.common.conf.PluginClassDeserializer.java

License:Apache License

@Override
public PluginClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("PluginClass should be a JSON Object");
    }/*from  w  w w . ja  v a  2  s.co  m*/

    JsonObject jsonObj = json.getAsJsonObject();

    String type = jsonObj.has("type") ? jsonObj.get("type").getAsString() : Plugin.DEFAULT_TYPE;
    String name = getRequired(jsonObj, "name").getAsString();
    String description = jsonObj.has("description") ? jsonObj.get("description").getAsString() : "";
    String className = getRequired(jsonObj, "className").getAsString();

    Set<String> endpointsSet = new HashSet<>();
    if (jsonObj.has("endpoints")) {
        endpointsSet = context.deserialize(jsonObj.get("endpoints"), ENDPOINTS_TYPE);
    }

    Map<String, PluginPropertyField> properties = jsonObj.has("properties")
            ? context.<Map<String, PluginPropertyField>>deserialize(jsonObj.get("properties"), PROPERTIES_TYPE)
            : ImmutableMap.<String, PluginPropertyField>of();

    return new PluginClass(type, name, description, className, null, properties, endpointsSet);
}

From source file:co.cask.cdap.common.zookeeper.coordination.ResourceAssignmentTypeAdapter.java

License:Apache License

@Override
public ResourceAssignment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Expect a json object, got " + json);
    }/*from w  w w. j a  v a2 s.  c om*/

    JsonObject jsonObj = json.getAsJsonObject();
    String name = jsonObj.get("name").getAsString();

    Multimap<Discoverable, PartitionReplica> assignments = TreeMultimap
            .create(DiscoverableComparator.COMPARATOR, PartitionReplica.COMPARATOR);
    JsonArray assignmentsJson = context.deserialize(jsonObj.get("assignments"), JsonArray.class);
    for (JsonElement element : assignmentsJson) {
        if (!element.isJsonArray()) {
            throw new JsonParseException("Expect a json array, got " + element);
        }

        JsonArray entryJson = element.getAsJsonArray();
        if (entryJson.size() != 2) {
            throw new JsonParseException("Expect json array of size = 2, got " + entryJson.size());
        }
        Discoverable key = context.deserialize(entryJson.get(0), Discoverable.class);
        PartitionReplica value = context.deserialize(entryJson.get(1), PartitionReplica.class);
        assignments.put(key, value);
    }

    return new ResourceAssignment(name, assignments);
}

From source file:co.cask.cdap.etl.common.SetMultimapCodec.java

License:Apache License

@Override
public SetMultimap<K, V> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    Map<K, Collection<V>> map = context.deserialize(obj.get("map"), mapType);
    SetMultimap<K, V> multimap = HashMultimap.create();
    for (Map.Entry<K, Collection<V>> entry : map.entrySet()) {
        multimap.putAll(entry.getKey(), entry.getValue());
    }/* www  .  ja v  a  2 s .  co m*/
    return multimap;
}

From source file:co.cask.cdap.etl.spark.batch.DatasetInfoTypeAdapter.java

License:Apache License

@Override
public DatasetInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    String datasetName = obj.get("datasetName").getAsString();
    Map<String, String> datasetArgs = context.deserialize(obj.get("datasetArgs"), mapType);
    if (obj.get("datasetSplitClass") == null) {
        return new DatasetInfo(datasetName, datasetArgs, null);
    }/*from  ww  w  .  ja  v a2 s  . co  m*/
    String datasetSplitClass = obj.get("datasetSplitClass").getAsString();
    ClassLoader classLoader = Objects.firstNonNull(Thread.currentThread().getContextClassLoader(),
            SparkBatchSourceFactory.class.getClassLoader());
    try {
        Class<?> splitClass = classLoader.loadClass(datasetSplitClass);
        List<Split> splits = context.deserialize(obj.get("datasetSplits"), getListType(splitClass));
        return new DatasetInfo(datasetName, datasetArgs, splits);
    } catch (ClassNotFoundException e) {
        throw new JsonParseException("Unable to deserialize splits", e);
    }
}

From source file:co.cask.cdap.etl.spark.batch.InputFormatProviderTypeAdapter.java

License:Apache License

@Override
public InputFormatProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    // if inputFormat is not present, return empty InputFormatProvider
    if (obj.get("inputFormatClass") == null) {
        return new SparkBatchSourceFactory.BasicInputFormatProvider();
    }// w ww .  ja  va2s  .  c om
    String className = obj.get("inputFormatClass").getAsString();
    Map<String, String> conf = context.deserialize(obj.get("inputFormatConfig"), mapType);
    return new SparkBatchSourceFactory.BasicInputFormatProvider(className, conf);
}

From source file:co.cask.cdap.etl.spark.batch.OutputFormatProviderTypeAdapter.java

License:Apache License

@Override
public OutputFormatProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    // if outputformat is not present, return empty OutputFormatProvider
    if (obj.get("outputFormatClass") == null) {
        return new SparkBatchSinkFactory.BasicOutputFormatProvider();
    }//ww w  . j a  v  a 2s .c  o  m
    String className = obj.get("outputFormatClass").getAsString();
    Map<String, String> conf = context.deserialize(obj.get("outputFormatConfig"), mapType);
    return new SparkBatchSinkFactory.BasicOutputFormatProvider(className, conf);
}

From source file:co.cask.cdap.internal.app.AbstractSpecificationCodec.java

License:Apache License

protected final <V> Map<String, V> deserializeMap(JsonElement json, JsonDeserializationContext context,
        Class<V> valueType) {
    Type type = new TypeToken<Map<String, V>>() {
    }.where(new TypeParameter<V>() {
    }, valueType).getType();//  www. jav  a2  s .c  om
    Map<String, V> map = context.deserialize(json, type);
    return map == null ? ImmutableMap.<String, V>of() : map;
}