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.ibm.common.activitystreams.internal.ASObjectAdapter.java

License:Apache License

/**
 * Method processArray.//from   w w w. j  a  v a  2  s.co  m
 * @param arr JsonArray
 * @param _class Class<?>
 * @param context JsonDeserializationContext
 * @param list ImmutableList.Builder<Object>
 */
private void processArray(JsonArray arr, Class<?> _class, JsonDeserializationContext context,
        ImmutableList.Builder<Object> list) {
    for (JsonElement mem : arr) {
        if (mem.isJsonPrimitive())
            list.add(_class != null ? context.deserialize(mem, _class)
                    : primConverter.convert(mem.getAsJsonPrimitive()));
        else if (mem.isJsonObject())
            list.add(context.deserialize(mem, _class != null ? _class : ASObject.class));
        else if (mem.isJsonArray())
            list.add(convert(mem.getAsJsonArray(), _class, context, builder()));
    }
}

From source file:com.ibm.common.activitystreams.internal.LinkValueAdapter.java

License:Apache License

/**
 * Method deserialize./*from   w w  w.java 2  s  . c o m*/
 * @param el JsonElement
 * @param type Type
 * @param context JsonDeserializationContext
        
        
        
 * @return LinkValue * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
public LinkValue deserialize(JsonElement el, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    checkArgument(el.isJsonArray() || el.isJsonObject() || el.isJsonPrimitive());
    if (el.isJsonArray()) {
        LinkValue.ArrayLinkValue.Builder builder = linkValues();
        for (JsonElement aryel : el.getAsJsonArray())
            builder.add(context.<LinkValue>deserialize(aryel, LinkValue.class));
        return builder.get();
    } else if (el.isJsonObject()) {
        JsonObject obj = el.getAsJsonObject();
        if (obj.has("objectType")) {
            TypeValue tv = context.deserialize(obj.get("objectType"), TypeValue.class);
            Model pMap = schema.forObjectType(tv.id());
            return context.deserialize(el, pMap != null && pMap.type() != null ? pMap.type() : ASObject.class);
        } else {
            return context.deserialize(el, ASObject.class);
        }
    } else {
        JsonPrimitive prim = el.getAsJsonPrimitive();
        checkArgument(prim.isString());
        return linkValue(prim.getAsString());
    }
}

From source file:com.ibm.common.activitystreams.internal.MultimapAdapter.java

License:Apache License

/**
 * Method arraydes./*w  w  w.j  a v  a  2  s  . c  o  m*/
 * @param array JsonArray
 * @param context JsonDeserializationContext
        
 * @return ImmutableList<Object> */
protected static ImmutableList<Object> arraydes(JsonArray array, JsonDeserializationContext context) {
    ImmutableList.Builder<Object> builder = ImmutableList.builder();
    for (JsonElement child : array)
        if (child.isJsonArray())
            builder.add(arraydes(child.getAsJsonArray(), context));
        else if (child.isJsonObject())
            builder.add(context.deserialize(child, ASObject.class));
        else if (child.isJsonPrimitive())
            builder.add(primConverter.convert(child.getAsJsonPrimitive()));
    return builder.build();
}

From source file:com.ibm.common.activitystreams.internal.MultimapAdapter.java

License:Apache License

/**
 * Method deserialize.//from  w  ww.  ja  v  a 2 s.  c  o m
 * @param json JsonElement
 * @param typeOfT Type
 * @param context JsonDeserializationContext
        
        
        
 * @return Multimap * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
public Multimap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    ImmutableMultimap.Builder mm = ImmutableMultimap.builder();
    JsonObject obj = json.getAsJsonObject();
    for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
        String key = entry.getKey();
        JsonElement val = entry.getValue();
        if (val.isJsonArray()) {
            for (JsonElement el : val.getAsJsonArray()) {
                if (el.isJsonArray())
                    mm.put(key, arraydes(el.getAsJsonArray(), context));
                else if (el.isJsonObject())
                    mm.put(key, context.deserialize(el, ASObject.class));
                else if (el.isJsonPrimitive())
                    mm.put(key, primConverter.convert(el.getAsJsonPrimitive()));
            }
        } else if (val.isJsonObject()) {
            mm.put(key, context.deserialize(val, ASObject.class));
        } else if (val.isJsonPrimitive()) {
            mm.put(key, primConverter.convert(val.getAsJsonPrimitive()));
        }
    }
    return mm.build();
}

From source file:com.ibm.common.activitystreams.internal.TypeValueAdapter.java

License:Apache License

/**
 * Method deserialize.//  w  w w .j  av  a  2s  . c  om
 * @param el JsonElement
 * @param type Type
 * @param context JsonDeserializationContext
        
        
        
 * @return TypeValue * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
public TypeValue deserialize(JsonElement el, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    checkArgument(el.isJsonPrimitive() || el.isJsonObject());
    if (el.isJsonPrimitive()) {
        JsonPrimitive prim = el.getAsJsonPrimitive();
        checkArgument(prim.isString());
        return type(prim.getAsString());
    } else {
        JsonObject obj = el.getAsJsonObject();
        if (obj.has("objectType")) {
            TypeValue tv = context.deserialize(obj.get("objectType"), TypeValue.class);
            Model pMap = schema.forObjectType(tv.id());
            return context.<ASObject>deserialize(el, pMap.type() != null ? pMap.type() : ASObject.class);
        } else {
            return context.<ASObject>deserialize(el, ASObject.class);
        }
    }
}

From source file:com.ibm.common.activitystreams.legacy.MediaLinkAdapter.java

License:Apache License

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

    checkArgument(json.isJsonObject());//from ww w .ja va2  s . com
    JsonObject obj = (JsonObject) json;
    MediaLink.Builder builder = LegacyMakers.mediaLink();
    for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
        String name = entry.getKey();
        JsonElement val = entry.getValue();
        if (val.isJsonPrimitive())
            builder.set(name, primConverter.convert(val.getAsJsonPrimitive()));
        else if (val.isJsonArray())
            builder.set(name, context.deserialize(val, Iterable.class));
        else if (val.isJsonObject())
            builder.set(name, context.deserialize(val, ASObject.class));
    }
    return builder.get();
}

From source file:com.ibm.common.geojson.as2.GeoAdapter.java

License:Apache License

@Override
public GeoObject deserialize(JsonElement element, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    GeoObject.Builder geo = null;/*from w  w  w.  j a  va  2 s .  c o  m*/
    checkArgument(element.isJsonObject());
    JsonObject obj = element.getAsJsonObject();
    checkArgument(obj.has("type"));
    GeoObject.Type et = Enums.getIfPresent(GeoObject.Type.class, obj.get("type").getAsString().toUpperCase())
            .orNull();
    checkArgument(et != null);
    switch (et) {
    case FEATURE:
        geo = GeoMakers.feature();
        break;
    case FEATURECOLLECTION:
        geo = GeoMakers.featureCollection();
        type = Feature.class;
        break;
    case GEOMETRYCOLLECTION:
        geo = GeoMakers.geometryCollection();
        type = Geometry.class;
        break;
    case LINESTRING:
        geo = GeoMakers.linestring();
        type = Position.class;
        break;
    case MULTILINESTRING:
        geo = GeoMakers.multiLineString();
        type = LineString.class;
        break;
    case MULTIPOINT:
        geo = GeoMakers.multipoint();
        type = Position.class;
        break;
    case MULTIPOLYGON:
        geo = GeoMakers.multiPolygon();
        type = Polygon.class;
        break;
    case POINT:
        geo = GeoMakers.point();
        type = null;
        break;
    case POLYGON:
        geo = GeoMakers.polygon();
        type = LineString.class;
        break;
    }

    for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
        JsonElement el = entry.getValue();
        String name = entry.getKey();
        if ("crs".equals(name)) {
            CRS.Builder cb = new CRS.Builder();
            JsonObject o = el.getAsJsonObject();
            if (o.has("type"))
                cb.type(o.get("type").getAsString());
            if (o.has("properties")) {
                JsonObject p = o.get("properties").getAsJsonObject();
                for (Map.Entry<String, JsonElement> e : p.entrySet()) {
                    cb.set(e.getKey(), context.deserialize(e.getValue(), Object.class));
                }
            }
            geo.crs(cb.get());
        } else if ("properties".equals(name)) {
            geo.set("properties", context.deserialize(el, Map.class));
        } else if ("bbox".equals(name)) {
            BoundingBox.Builder bb = new BoundingBox.Builder();
            float[] points = context.deserialize(el, float[].class);
            bb.add(points);
            geo.boundingBox(bb.get());
        } else if ("features".equals(name)) {
            Feature[] features = context.deserialize(el, Feature[].class);
            FeatureCollection.Builder fcb = (FeatureCollection.Builder) geo;
            for (Feature f : features)
                fcb.add(f);
        } else if ("coordinates".equals(name)) {
            switch (et) {
            case LINESTRING: {
                LineString.Builder lsb = (LineString.Builder) geo;
                float[][] positions = context.deserialize(el, float[][].class);
                boolean ring = ring(positions);
                if (ring)
                    lsb.linearRing();
                for (int n = 0; n < positions.length; n++) {
                    if (!ring || (ring && n < positions.length - 1))
                        lsb.add(toPosition(positions[n]));
                }
                break;
            }
            case MULTIPOINT: {
                MultiPoint.Builder lsb = (MultiPoint.Builder) geo;
                float[][] positions = context.deserialize(el, float[][].class);
                for (float[] pos : positions)
                    lsb.add(toPosition(pos));
                break;
            }
            case MULTILINESTRING: {
                MultiLineString.Builder mlb = (MultiLineString.Builder) geo;
                float[][][] positions = context.deserialize(el, float[][][].class);
                for (float[][] lines : positions) {
                    LineString.Builder lsb = GeoMakers.linestring();
                    boolean ring = ring(lines);
                    if (ring)
                        lsb.linearRing();
                    for (int n = 0; n < lines.length; n++) {
                        if (!ring || (ring && n < lines.length - 1))
                            lsb.add(toPosition(lines[n]));
                    }
                    for (float[] pos : lines)
                        lsb.add(toPosition(pos));
                    mlb.add(lsb);
                }
                break;
            }
            case POLYGON: {
                Polygon.Builder mlb = (Polygon.Builder) geo;
                float[][][] positions = context.deserialize(el, float[][][].class);
                for (float[][] lines : positions) {
                    LineString.Builder lsb = GeoMakers.linestring();
                    for (float[] pos : lines)
                        lsb.add(toPosition(pos));
                    mlb.add(lsb);
                }
                break;
            }
            case MULTIPOLYGON: {
                MultiPolygon.Builder mpb = (MultiPolygon.Builder) geo;
                float[][][][] positions = context.deserialize(el, float[][][][].class);
                for (float[][][] polygons : positions) {
                    Polygon.Builder pb = GeoMakers.polygon();
                    for (float[][] lines : polygons) {
                        LineString.Builder lsb = GeoMakers.linestring();
                        for (float[] pos : lines)
                            lsb.add(toPosition(pos));
                        pb.add(lsb);
                    }
                    mpb.add(pb);
                }
                break;
            }
            case POINT:
                Point.Builder pb = (Point.Builder) geo;
                float[] position = context.deserialize(el, float[].class);
                pb.position(toPosition(position));
                break;
            default:
                break;
            }
        } else if ("geometries".equals(name)) {
            Geometry[] geos = context.deserialize(el, Geometry[].class);
            GeometryCollection.Builder fcb = (GeometryCollection.Builder) geo;
            for (Geometry<?, ?> g : geos)
                fcb.add(g);
        } else {
            if (el.isJsonArray()) {
                geo.set(name, context.deserialize(el, Object.class));
            } else if (el.isJsonObject()) {
                geo.set(name, context.deserialize(el, GeoObject.class));
            } else if (el.isJsonPrimitive()) {
                JsonPrimitive p = el.getAsJsonPrimitive();
                if (p.isBoolean())
                    geo.set(name, p.getAsBoolean());
                else if (p.isNumber())
                    geo.set(name, p.getAsNumber());
                else if (p.isString())
                    geo.set(name, p.getAsString());
            }
        }
    }

    return geo.get();
}

From source file:com.ibm.util.merge.json.DirectiveDeserializer.java

License:Apache License

@Override
public AbstractDirective deserialize(JsonElement json, Type directive, JsonDeserializationContext context) {
    int myType = json.getAsJsonObject().get("type").getAsInt();
    switch (myType) {
    case Directives.TYPE_REPLACE_VALUE:
        return context.deserialize(json, ReplaceValue.class);
    case Directives.TYPE_REQUIRE:
        return context.deserialize(json, Require.class);
    case Directives.TYPE_SQL_INSERT:
        return context.deserialize(json, InsertSubsSql.class);
    case Directives.TYPE_SQL_REPLACE_COL:
        return context.deserialize(json, ReplaceColSql.class);
    case Directives.TYPE_SQL_REPLACE_ROW:
        return context.deserialize(json, ReplaceRowSql.class);
    case Directives.TYPE_CSV_INSERT:
        return context.deserialize(json, InsertSubsCsv.class);
    case Directives.TYPE_CSV_REPLACE_COL:
        return context.deserialize(json, ReplaceColCsv.class);
    case Directives.TYPE_CSV_REPLACE_ROW:
        return context.deserialize(json, ReplaceRowCsv.class);
    case Directives.TYPE_HTML_INSERT:
        return context.deserialize(json, InsertSubsHtml.class);
    case Directives.TYPE_HTML_REPLACE_COL:
        return context.deserialize(json, ReplaceColHtml.class);
    case Directives.TYPE_HTML_REPLACE_ROW:
        return context.deserialize(json, ReplaceRowHtml.class);
    case Directives.TYPE_HTML_REPLACE_MARKUP:
        return context.deserialize(json, ReplaceMarkupHtml.class);
    case Directives.TYPE_TAG_INSERT:
        return context.deserialize(json, InsertSubsTag.class);
    default://from  w ww  . j a  va2 s  . c  o m
        return null;
    }
}

From source file:com.ibm.util.merge.json.ProviderDeserializer.java

License:Apache License

@Override
public AbstractProvider deserialize(JsonElement json, Type provider, JsonDeserializationContext context) {
    JsonElement jsonType = json.getAsJsonObject().get("type");
    if (jsonType == null) {
        return null;
    }//  www.ja  va 2 s  .  c  om
    int myType = jsonType.getAsInt();
    switch (myType) {
    case Providers.TYPE_CSV:
        return context.deserialize(json, ProviderCsv.class);
    case Providers.TYPE_SQL:
        return context.deserialize(json, ProviderSql.class);
    case Providers.TYPE_TAG:
        return context.deserialize(json, ProviderTag.class);
    case Providers.TYPE_HTML:
        return context.deserialize(json, ProviderHtml.class);
    default:
        return null;
    }
}

From source file:com.indragie.cmput301as1.ExpenseItemJSONDeserializer.java

License:Open Source License

@Override
public ExpenseItem deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    if (obj == null)
        return null;

    String name = JSONHelpers.getStringIfPossible(obj.get("name"));
    String description = JSONHelpers.getStringIfPossible(obj.get("description"));
    String category = JSONHelpers.getStringIfPossible(obj.get("category"));
    boolean incomplete = JSONHelpers.getBooleanIfPossible(obj.get("incomplete"));
    JsonObject amountObject = JSONHelpers.getJsonObjectIfPossible(obj.get("amount"));
    JsonElement dateElement = obj.get("date");
    String receiptBase64Str = JSONHelpers.getStringIfPossible(obj.get("receipt_base64"));

    Money amount = null;/*from   w w  w  .j  a v a  2  s . c om*/
    if (amountObject != null) {
        amount = context.deserialize(amountObject, Money.class);
    }

    Date date = null;
    if (dateElement != null) {
        date = context.deserialize(dateElement, Date.class);
    }

    ExpenseItem item = new ExpenseItem(name, description, category, amount, date);
    item.setIncomplete(incomplete);

    if (receiptBase64Str != null) {
        Uri receiptUri = receiptController.constructNewReceiptImageUri();
        byte buffer[] = Base64.decode(receiptBase64Str, Base64.DEFAULT);
        try {
            FileOutputStream fos = new FileOutputStream(receiptUri.getPath(), false);
            fos.write(buffer);
            fos.flush();
            fos.close();
            item.setReceiptUri(receiptUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return item;
}