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:edu.wpi.cs.wpisuitetng.modules.defecttracker.models.DefectEventDeserializer.java

License:Open Source License

@Override
public DefectEvent deserialize(JsonElement element, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    // we need to switch on the type field to figure out the concrete class to instantiate
    JsonObject object = element.getAsJsonObject();
    if (!object.has("type")) {
        throw new JsonParseException("DefectEvent does not have type information");
    }/*from   w w w .  ja v  a2s  .  c o m*/
    EventType eType = context.deserialize(object.get("type"), EventType.class);
    if (eType != null) { // type could be any garbage string, eType null if not in enum
        switch (eType) {
        case CHANGESET:
            return context.deserialize(element, DefectChangeset.class);
        case COMMENT:
            return context.deserialize(element, Comment.class);
        }
    }
    throw new JsonParseException("DefectEvent type is unrecognized");
}

From source file:elaborate.tag_analysis.oosm.impl.gson.BaseInterfaceDeserializer.java

protected Object processNormalElement(String name, JsonElement element, Class clz,
        JsonDeserializationContext jdc) throws JsonParseException {
    return jdc.deserialize(element, clz);
}

From source file:elaborate.tag_analysis.oosm.impl.gson.OOSMElementListInterfaceDeserializer.java

@Override
protected Object processArrayElement(String name, JsonArray element, Field field,
        JsonDeserializationContext jdc) throws JsonParseException {
    if ("elements".equals(name)) {
        List constructsList = new ArrayList();
        for (int i = 0; i < element.size(); i++) {
            JsonObject constructJsonObject = element.get(i).getAsJsonObject();
            constructsList.add((OOSMElement) jdc.deserialize(constructJsonObject, OOSMElement.class));
        }/*from   ww  w.jav a 2  s  .  com*/
        return constructsList;
    } else {
        return super.processArrayElement(name, element, field, jdc); //To change body of generated methods, choose Tools | Templates.
    }
}

From source file:elaborate.tag_analysis.oosm.impl.gson.OOSMRuleInterfaceDeserializer.java

@Override
protected Object processArrayElement(String name, JsonArray element, Field field,
        JsonDeserializationContext jdc) throws JsonParseException {
    if ("constructs".equals(name)) {
        List constructsList = new ArrayList();
        JsonArray constructsJsonArray = element;
        for (int i = 0; i < constructsJsonArray.size(); i++) {
            JsonObject constructJsonObject = constructsJsonArray.get(i).getAsJsonObject();
            if (constructJsonObject.has("elements")) {
                constructsList.add((OOSMConstruct) jdc.deserialize(constructJsonObject, OOSMElementList.class));
            } else {
                constructsList.add((OOSMConstruct) jdc.deserialize(constructJsonObject, OOSMElement.class));
            }//  www.j a va 2s . com
        }
        return constructsList;
    } else {
        return super.processArrayElement(name, element, field, jdc); //To change body of generated methods, choose Tools | Templates.
    }
}

From source file:emily.modules.reddit.CommentDataDeserializer.java

License:Apache License

@Override
public CommentData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    boolean isOp = false;

    String author = null;//ww  w.  j  av  a  2 s .  c  o m
    if (jsonObject.get("author") != null)
        author = jsonObject.get("author").getAsString();

    String body = null;
    if (jsonObject.get("selftext") != null) {
        body = jsonObject.get("selftext").getAsString();
        isOp = true;
    } else if (jsonObject.get("body") != null)
        body = jsonObject.get("body").getAsString();

    Long created = null;
    if (jsonObject.get("created") != null)
        created = jsonObject.get("created").getAsLong();

    Long created_utc = null;
    if (jsonObject.get("created_utc") != null)
        created_utc = jsonObject.get("created_utc").getAsLong();

    String subreddit = null;
    if (jsonObject.get("subreddit") != null)
        subreddit = jsonObject.get("subreddit").getAsString();

    Integer score = null;
    if (jsonObject.get("score") != null)
        score = jsonObject.get("score").getAsInt();

    String id = null;
    if (jsonObject.get("id") != null)
        id = jsonObject.get("id").getAsString();

    JsonElement replies = null;
    if (jsonObject.get("replies") != null)
        replies = jsonObject.get("replies");

    InitialDataComment comment = null;

    if (replies != null && !(replies instanceof JsonPrimitive)) {
        comment = context.deserialize(jsonObject.get("replies"), InitialDataComment.class);
    }

    CommentData commentData = new CommentData();
    commentData.isOp = isOp;
    commentData.author = author;
    commentData.body = body;
    commentData.created = created;
    commentData.created_utc = created_utc;
    commentData.subreddit = subreddit;
    commentData.score = score;
    commentData.id = id;
    commentData.replies = comment;

    return commentData;
}

From source file:es.us.isa.aml.parsers.agreements.json.InterfaceAdapterExpression.java

License:Open Source License

private Expression deserializeInternal(JsonElement json, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String type = jsonObject.get("_type").getAsString();
    JsonElement element = jsonObject.get("properties");
    Gson gson = new Gson();
    try {//from  w ww .  j a  v  a2  s  . com
        switch (type) {
        case "DuringExpression":
            DuringExpression exp = context.deserialize(element,
                    Class.forName("es.us.isa.aml.model.expression." + type));
            exp.setState(
                    deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("state"), context));
            exp.setNum(deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("num"), context));
            return exp;
        case "FrecuencyExpression":
            FrecuencyExpression exp2 = context.deserialize(element,
                    Class.forName("es.us.isa.aml.model.expression." + type));
            exp2.setState(
                    deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("state"), context));
            exp2.setNtimes(
                    deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("ntimes"), context));
            return exp2;
        case "Atomic":
            return new Atomic(jsonObject.get("properties").getAsJsonObject().get("value").getAsNumber());
        case "Var":
            return new Var(jsonObject.get("properties").getAsJsonObject().get("id").getAsString());
        case "ArithmeticExpression":
            Expression ae1 = deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("exp1"),
                    context);
            Expression ae2 = deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("exp2"),
                    context);
            ArithmeticOperator ao = gson.fromJson(
                    jsonObject.get("properties").getAsJsonObject().get("operator"), ArithmeticOperator.class);
            ArithmeticExpression expA = new ArithmeticExpression(ae1, ae2, ao);
            return expA;
        case "AssignmentExpression":
            AssignmentExpression expAss = context.deserialize(element,
                    Class.forName("es.us.isa.aml.model.expression." + type));
            expAss.setExpression1(
                    deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("exp1"), context));
            expAss.setExpression2(
                    deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("exp2"), context));
            return expAss;
        case "LogicalExpression":
            Expression le1 = deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("exp1"),
                    context);
            Expression le2 = deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("exp2"),
                    context);
            LogicalOperator lo = gson.fromJson(jsonObject.get("properties").getAsJsonObject().get("operator"),
                    LogicalOperator.class);
            LogicalExpression expL = new LogicalExpression(le1, le2, lo);
            return expL;
        case "RelationalExpression":
            Expression re1 = deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("exp1"),
                    context);
            Expression re2 = deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("exp2"),
                    context);
            RelationalOperator ro = gson.fromJson(
                    jsonObject.get("properties").getAsJsonObject().get("operator"), RelationalOperator.class);
            RelationalExpression expR = new RelationalExpression(re1, re2, ro);
            return expR;

        case "ParenthesisExpression":
            ParenthesisExpression expP = context.deserialize(element,
                    Class.forName("es.us.isa.aml.model.expression." + type));
            expP.setExpression(
                    deserializeInternal(jsonObject.get("properties").getAsJsonObject().get("exp"), context));
            return expP;
        default:
            return context.deserialize(element, Class.forName("es.us.isa.aml.model.expression." + type));
        }
    } catch (ClassNotFoundException cnfe) {
        LOGGER.log(Level.SEVERE, cnfe.getMessage());
        throw new JsonParseException("Unknown element type: " + type, cnfe);

    }
}

From source file:es.us.isa.aml.parsers.agreements.json.InterfaceAdapterModel.java

License:Open Source License

@Override
public AgreementModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/*from  w w  w. java  2 s  .c om*/
        Gson gson = new Gson();
        JsonObject jsonObject = json.getAsJsonObject();
        Map metrics = new HashMap();
        if (jsonObject.get("context").getAsJsonObject().get("metrics") != null) {
            for (Entry<String, JsonElement> jse : jsonObject.get("context").getAsJsonObject().get("metrics")
                    .getAsJsonObject().entrySet()) {
                Metric met = gson.fromJson(jse.getValue(), Metric.class);
                if (jse.getValue().getAsJsonObject().get("domain").getAsJsonObject().get("max") != null) {
                    Range d1 = gson.fromJson(jse.getValue().getAsJsonObject().get("domain"),
                            es.us.isa.aml.model.Range.class);
                    met.setDomain(d1);
                }
                if (jse.getValue().getAsJsonObject().get("domain").getAsJsonObject().get("values") != null) {
                    Enumerated d1 = gson.fromJson(jse.getValue().getAsJsonObject().get("domain"),
                            es.us.isa.aml.model.Enumerated.class);
                    met.setDomain(d1);
                }
                metrics.put(met.getId(), met);
            }
        }
        switch (jsonObject.get("docType").getAsString()) {
        case "TEMPLATE":
            AgreementTemplate agt = context.deserialize(json,
                    Class.forName("es.us.isa.aml.model.AgreementTemplate"));
            agt.getContext().setMetrics(metrics);
            return agt;
        case "OFFER":
            AgreementOffer ago = context.deserialize(json, Class.forName("es.us.isa.aml.model.AgreementOffer"));
            ago.getContext().setMetrics(metrics);
            return ago;
        case "AGREEMENT":
            Agreement ag = context.deserialize(json, Class.forName("es.us.isa.aml.model.Agreement"));
            ag.getContext().setMetrics(metrics);
            return ag;
        default:
            return null;
        }
    } catch (ClassNotFoundException e) {
        LOGGER.log(Level.SEVERE, e.getMessage());
    }
    return null;
}

From source file:eu.crushedpixel.littlstar.api.gson.ErrorsDeserializer.java

License:Apache License

@Override
public Errors deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonPrimitive())
        return new Errors(new String[] { json.getAsString() });
    String[] errors = context.deserialize(json, String[].class);
    return new Errors(errors);
}

From source file:eu.over9000.cathode.data.deserializers.ThumbnailListDeserializer.java

License:Open Source License

@Override
public List<Thumbnail> deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException {
    if (json.isJsonPrimitive()) {
        final String url = json.getAsString();
        return Collections.singletonList(new Thumbnail(url, "processing"));
    }//from   w w w .j av  a2  s.  co m

    return context.deserialize(json, ThumbnailList.class);
}

From source file:fr.dudie.keolis.gson.BikeStationDeserializer.java

License:Open Source License

@Override
public List<BikeStation> deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {

    final ArrayList<BikeStation> bikeStations = new ArrayList<BikeStation>();

    if (json instanceof JsonArray) {
        for (int i = 0; i < ((JsonArray) json).size(); i++) {
            bikeStations.add(KeoUtils.getGsonInstance().fromJson(((JsonArray) json).get(i), BikeStation.class));
        }//from w  w  w. ja va 2 s.  c om

    } else {
        bikeStations.add((BikeStation) context.deserialize(json, BikeStation.class));
    }

    return bikeStations;
}