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:net.sf.uadetector.json.internal.data.deserializer.DataDeserializer.java

License:Apache License

private static <T> List<T> deserializeType(final JsonDeserializationContext context,
        final Map.Entry<String, JsonElement> jsonObject, final SerializableDataField field,
        final Class<T> classOfT) {
    Check.notNull(context, "context");
    Check.notNull(jsonObject, "jsonObject");
    Check.notNull(field, "field");
    Check.notNull(classOfT, "classOfT");

    final List<T> result = new ArrayList<T>();
    if (field.getName().equals(jsonObject.getKey())) {
        final JsonArray browsers = jsonObject.getValue().getAsJsonArray();
        for (final JsonElement element : browsers) {
            final T entry = context.deserialize(element, classOfT);
            if (entry != null) {
                result.add(entry);/* w  w  w  .  jav  a 2  s.c o  m*/
            }
        }
    }
    return result;
}

From source file:net.sf.uadetector.json.internal.data.deserializer.DevicePatternDeserializer.java

License:Apache License

@Override
public DevicePattern deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    String hash = EMPTY_HASH_CODE;
    Pattern pattern = null;/*from  ww  w.  jav a 2 s . c  o  m*/

    // deserialize
    for (final Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
        if (PATTERN.getName().equals(entry.getKey())) {
            pattern = context.deserialize(entry.getValue(), Pattern.class);
        } else if (HASH.getName().equals(entry.getKey())) {
            hash = entry.getValue().getAsString();
        }
    }
    final int id = counter.incrementAndGet();

    // create device pattern
    DevicePattern devicePattern = null;
    try {
        devicePattern = new DevicePattern(id, pattern, id);

        // check hash when option is set
        checkHash(json, hash, devicePattern);

        // add pattern to map
        devicePatterns.put(hash, devicePattern);
    } catch (final Exception e) {
        addWarning(e.getLocalizedMessage());
    }

    return devicePattern;
}

From source file:net.sf.uadetector.json.internal.data.deserializer.OperatingSystemPatternDeserializer.java

License:Apache License

@Override
public OperatingSystemPattern deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    String hash = EMPTY_HASH_CODE;

    // deserialize
    Pattern pattern = null;//from w w  w .  j ava 2  s . co m
    for (final Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
        if (PATTERN.getName().equals(entry.getKey())) {
            pattern = context.deserialize(entry.getValue(), Pattern.class);
        } else if (HASH.getName().equals(entry.getKey())) {
            hash = entry.getValue().getAsString();
        }
    }
    final int id = counter.incrementAndGet();

    // create OS pattern
    OperatingSystemPattern osPattern = new OperatingSystemPattern(id, pattern, id);
    try {
        osPattern = new OperatingSystemPattern(id, pattern, id);

        // check hash when option is set
        checkHash(json, hash, osPattern);

        // add pattern to map
        operatingSystemPatterns.put(hash, osPattern);
    } catch (final Exception e) {
        addWarning(e.getLocalizedMessage());
    }

    return osPattern;
}

From source file:net.simonvt.cathode.jobqueue.JobSerializer.java

License:Apache License

@Override
public Job deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject member = (JsonObject) json;
    final JsonElement typeString = get(member, "type");
    final JsonElement data = get(member, "data");
    final Type actualType = typeForName(typeString);
    return context.deserialize(data, actualType);
}

From source file:net.swisstech.bitly.gson.converter.DateTimeTypeConverter.java

License:Apache License

@Override
public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
    try {/*from   www. java 2  s.c  om*/
        return new DateTime(json.getAsLong());
    } catch (IllegalArgumentException e) {
        // May be it can be formatted as a java.util.Date, so try that
        Date date = context.deserialize(json, Date.class);
        return new DateTime(date);
    }
}

From source file:net.swisstech.fivehundredpx.gson.converter.DateTimeTypeConverter.java

License:Apache License

@Override
public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
    try {/*from   w w w .jav a 2  s .c  o  m*/
        return new DateTime(json.getAsLong());
    } catch (IllegalArgumentException e) {
        try {
            return DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").parseDateTime(json.getAsString());
        } catch (IllegalArgumentException ee) {
            // May be it can be formatted as a java.util.Date, so try that
            Date date = context.deserialize(json, Date.class);
            return new DateTime(date);
        }
    }
}

From source file:net.vpc.app.vainruling.core.service.util.DocumentSerializer.java

@Override
public Document deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Object m = context.deserialize(json, Map.class);
    if (of == null) {
        of = UPA.getBootstrapFactory();//from   w ww  . j  av  a2  s .c om
    }
    Document d = of.createObject(Document.class);
    d.setAll((Map<String, Object>) m);
    return d;
}

From source file:nl.hardijzer.bitonsync.client.User.java

License:Open Source License

@Override
public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json instanceof JsonObject) {
        JsonObject obj = (JsonObject) json;
        return new User((String) context.deserialize(obj.get("naam"), String.class),
                (String) context.deserialize(obj.get("voornaam"), String.class),
                (String) context.deserialize(obj.get("achternaam"), String.class),
                (String) context.deserialize(obj.get("vaste telefoon"), String.class),
                (String) context.deserialize(obj.get("mobiel"), String.class),
                (String) context.deserialize(obj.get("e-mail"), String.class),
                (String) context.deserialize(obj.get("adres"), String.class),
                (String) context.deserialize(obj.get("geboortedatum"), String.class));
    }//from w  w w  .  j  a va 2  s  .c om
    return null;
}

From source file:nz.ac.otago.psyanlab.common.model.util.AbsModelGsonAdapter.java

License:Open Source License

@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext jctx)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String type = jsonObject.get("type").getAsString();
    JsonElement element = jsonObject.get("properties");

    try {/*w w  w  . j a  va  2 s.co m*/
        return jctx.deserialize(element, Class.forName(mNamespace + type));
    } catch (ClassNotFoundException e) {
        throw new JsonParseException("Unknown element type: " + type, e);
    }
}

From source file:org.apache.abdera2.activities.io.gson.BaseAdapter.java

License:Apache License

public ASBase deserialize(JsonElement el, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = (JsonObject) el;/* w  ww  .  j a  v a2s. c  o m*/
    ASBase.Builder<?, ?> builder;
    if (type == Collection.class)
        builder = Collection.makeCollection();
    else if (type == Activity.class)
        builder = Activity.makeActivity();
    else if (type == MediaLink.class)
        builder = MediaLink.makeMediaLink();
    else if (type == PlaceObject.class)
        builder = PlaceObject.makePlace();
    else if (type == Mood.class)
        builder = Mood.makeMood();
    else if (type == Address.class)
        builder = Address.makeAddress();
    else {
        JsonPrimitive ot = obj.getAsJsonPrimitive("objectType");
        if (ot != null) {
            String ots = ot.getAsString();
            Class<? extends ASObject.Builder> _class = objsmap.get(ots);
            if (_class != null) {
                builder = Discover.locate(_class, _class.getName());
                try {
                    builder = _class.getConstructor(String.class).newInstance(ots);
                } catch (Throwable t) {
                }

            } else
                builder = ASObject.makeObject(ots);
        } else {
            if (obj.has("verb") && (obj.has("actor") || obj.has("object") || obj.has("target"))) {
                builder = Activity.makeActivity();
            } else if (obj.has("items")) {
                builder = Collection.makeCollection();
            } else {
                builder = ASObject.makeObject(); // anonymous
            }
        }
    }
    for (Entry<String, JsonElement> entry : obj.entrySet()) {
        String name = entry.getKey();
        if (name.equalsIgnoreCase("objectType"))
            continue;
        Class<?> _class = map.get(name);
        JsonElement val = entry.getValue();
        if (val.isJsonPrimitive()) {
            if (_class != null) {
                builder.set(name, context.deserialize(val, _class));
            } else {
                JsonPrimitive prim = val.getAsJsonPrimitive();
                if (prim.isBoolean())
                    builder.set(name, prim.getAsBoolean());
                else if (prim.isNumber())
                    builder.set(name, prim.getAsNumber());
                else {
                    builder.set(name, prim.getAsString());
                }
            }
        } else if (val.isJsonArray()) {
            ImmutableList.Builder<Object> list = ImmutableList.builder();
            JsonArray arr = val.getAsJsonArray();
            processArray(arr, _class, context, list);
            builder.set(name, list.build());
        } else if (val.isJsonObject()) {
            if (map.containsKey(name)) {
                builder.set(name, context.deserialize(val, map.get(name)));
            } else
                builder.set(name, context.deserialize(val, ASObject.class));
        }
    }
    return builder.get();
}