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:org.eclipse.che.api.workspace.server.stack.adapters.StackSourceAdapter.java

License:Open Source License

@Override
public StackSource deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    return context.deserialize(jsonElement, StackSourceImpl.class);
}

From source file:org.eclipse.che.api.workspace.server.stack.adapters.WorkspaceConfigAdapter.java

License:Open Source License

@Override
public WorkspaceConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return context.deserialize(json, WorkspaceConfigImpl.class);
}

From source file:org.eclipse.egit.github.core.client.EventFormatter.java

License:Open Source License

public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final Event event = gson.fromJson(json, Event.class);
    if (event == null || !json.isJsonObject())
        return event;
    final JsonElement rawPayload = json.getAsJsonObject().get("payload");
    if (rawPayload == null || !rawPayload.isJsonObject())
        return event;
    final String type = event.getType();
    if (type == null || type.length() == 0)
        return event;

    Class<? extends EventPayload> payloadClass;
    if (TYPE_COMMIT_COMMENT.equals(type))
        payloadClass = CommitCommentPayload.class;
    else if (TYPE_CREATE.equals(type))
        payloadClass = CreatePayload.class;
    else if (TYPE_DELETE.equals(type))
        payloadClass = DeletePayload.class;
    else if (TYPE_DOWNLOAD.equals(type))
        payloadClass = DownloadPayload.class;
    else if (TYPE_FOLLOW.equals(type))
        payloadClass = FollowPayload.class;
    else if (TYPE_FORK.equals(type))
        payloadClass = ForkPayload.class;
    else if (TYPE_FORK_APPLY.equals(type))
        payloadClass = ForkApplyPayload.class;
    else if (TYPE_GIST.equals(type))
        payloadClass = GistPayload.class;
    else if (TYPE_GOLLUM.equals(type))
        payloadClass = GollumPayload.class;
    else if (TYPE_ISSUE_COMMENT.equals(type))
        payloadClass = IssueCommentPayload.class;
    else if (TYPE_ISSUES.equals(type))
        payloadClass = IssuesPayload.class;
    else if (TYPE_MEMBER.equals(type))
        payloadClass = MemberPayload.class;
    else if (TYPE_PUBLIC.equals(type))
        payloadClass = PublicPayload.class;
    else if (TYPE_PULL_REQUEST.equals(type))
        payloadClass = PullRequestPayload.class;
    else if (TYPE_PULL_REQUEST_REVIEW_COMMENT.equals(type))
        payloadClass = PullRequestReviewCommentPayload.class;
    else if (TYPE_PUSH.equals(type))
        payloadClass = PushPayload.class;
    else if (TYPE_TEAM_ADD.equals(type))
        payloadClass = TeamAddPayload.class;
    else if (TYPE_WATCH.equals(type))
        payloadClass = WatchPayload.class;
    else//  w  ww  .  j a v  a 2  s .co  m
        return event;

    try {
        EventPayload typedPayload = context.deserialize(rawPayload, payloadClass);
        return event.setPayload(typedPayload);
    } catch (JsonParseException jpe) {
        // Parse exception here denotes legacy payloads with differing
        // fields than built-in payload classes provide
        return event;
    }
}

From source file:org.eclipse.leshan.core.model.json.ObjectModelDeserializer.java

License:Open Source License

@Override
public ObjectModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json == null)
        return null;

    if (!json.isJsonObject())
        return null;

    JsonObject jsonObject = json.getAsJsonObject();
    if (!jsonObject.has("id"))
        return null;

    int id = jsonObject.get("id").getAsInt();
    String name = jsonObject.get("name").getAsString();
    String instancetype = jsonObject.get("instancetype").getAsString();
    boolean mandatory = jsonObject.get("mandatory").getAsBoolean();
    String description = jsonObject.get("description").getAsString();
    ResourceModel[] resourceSpecs = context.deserialize(jsonObject.get("resourcedefs"), ResourceModel[].class);

    return new ObjectModel(id, name, description, "multiple".equals(instancetype), mandatory, resourceSpecs);
}

From source file:org.eclipse.leshan.core.objectspec.json.ObjectSpecDeserializer.java

License:Open Source License

@Override
public ObjectSpec deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json == null)
        return null;

    if (!json.isJsonObject())
        return null;

    JsonObject jsonObject = json.getAsJsonObject();
    if (!jsonObject.has("id"))
        return null;

    int id = jsonObject.get("id").getAsInt();
    String name = jsonObject.get("name").getAsString();
    String instancetype = jsonObject.get("instancetype").getAsString();
    boolean mandatory = jsonObject.get("mandatory").getAsBoolean();
    String description = jsonObject.get("description").getAsString();
    ResourceSpec[] resourceSpecs = context.deserialize(jsonObject.get("resourcedefs"), ResourceSpec[].class);
    Map<Integer, ResourceSpec> resources = new HashMap<Integer, ResourceSpec>();
    for (ResourceSpec resourceSpec : resourceSpecs) {
        resources.put(resourceSpec.id, resourceSpec);
    }/* w  w  w .ja v  a 2s  .co m*/

    return new ObjectSpec(id, name, description, "multiple".equals(instancetype), mandatory, resources);
}

From source file:org.eclipse.leshan.server.demo.servlet.json.LwM2mNodeDeserializer.java

License:Open Source License

@Override
public LwM2mNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (json == null) {
        return null;
    }/*from w  ww  .j a v a2  s  .c  om*/

    LwM2mNode node;

    if (json.isJsonObject()) {
        JsonObject object = (JsonObject) json;

        if (!object.has("id")) {
            throw new JsonParseException("Missing id");
        }
        int id = object.get("id").getAsInt();

        if (object.has("instances")) {

            JsonArray array = object.get("instances").getAsJsonArray();
            LwM2mObjectInstance[] instances = new LwM2mObjectInstance[array.size()];

            for (int i = 0; i < array.size(); i++) {
                instances[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObject(id, instances);

        } else if (object.has("resources")) {
            JsonArray array = object.get("resources").getAsJsonArray();
            LwM2mResource[] resources = new LwM2mResource[array.size()];

            for (int i = 0; i < array.size(); i++) {
                resources[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObjectInstance(id, resources);

        } else if (object.has("value")) {
            // single value resource
            JsonPrimitive val = object.get("value").getAsJsonPrimitive();
            org.eclipse.leshan.core.model.ResourceModel.Type expectedType = getTypeFor(val);
            node = LwM2mSingleResource.newResource(id, deserializeValue(val, expectedType), expectedType);
        } else if (object.has("values")) {
            // multi-instances resource
            Map<Integer, Object> values = new HashMap<>();
            org.eclipse.leshan.core.model.ResourceModel.Type expectedType = null;
            for (Entry<String, JsonElement> entry : object.get("values").getAsJsonObject().entrySet()) {
                JsonPrimitive pval = entry.getValue().getAsJsonPrimitive();
                expectedType = getTypeFor(pval);
                values.put(Integer.valueOf(entry.getKey()), deserializeValue(pval, expectedType));
            }
            // use string by default;
            if (expectedType == null)
                expectedType = org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
            node = LwM2mMultipleResource.newResource(id, values, expectedType);
        } else {
            throw new JsonParseException("Invalid node element");
        }
    } else {
        throw new JsonParseException("Invalid node element");
    }

    return node;
}

From source file:org.eclipse.leshan.standalone.servlet.json.LwM2mNodeDeserializer.java

License:Open Source License

@Override
public LwM2mNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (json == null) {
        return null;
    }//from   ww  w. j av a 2  s  .  c om

    LwM2mNode node = null;

    if (json.isJsonObject()) {
        JsonObject object = (JsonObject) json;

        if (!object.has("id")) {
            throw new JsonParseException("Missing id");
        }
        int id = object.get("id").getAsInt();

        if (object.has("instances")) {

            JsonArray array = object.get("instances").getAsJsonArray();
            LwM2mObjectInstance[] instances = new LwM2mObjectInstance[array.size()];

            for (int i = 0; i < array.size(); i++) {
                instances[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObject(id, instances);

        } else if (object.has("resources")) {
            JsonArray array = object.get("resources").getAsJsonArray();
            LwM2mResource[] resources = new LwM2mResource[array.size()];

            for (int i = 0; i < array.size(); i++) {
                resources[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObjectInstance(id, resources);

        } else if (object.has("value")) {
            // single value resource
            node = new LwM2mResource(id, this.deserializeValue(object.get("value").getAsJsonPrimitive()));
        } else if (object.has("values")) {
            // multi-instances resource
            Collection<Value<?>> values = new ArrayList<>();
            for (JsonElement val : object.get("values").getAsJsonArray()) {
                values.add(this.deserializeValue(val.getAsJsonPrimitive()));
            }
            node = new LwM2mResource(id, values.toArray(new Value<?>[0]));
        } else {
            throw new JsonParseException("Invalid node element");
        }
    } else {
        throw new JsonParseException("Invalid node element");
    }

    return node;
}

From source file:org.eclipse.mylyn.internal.gerrit.core.client.JSonSupport.java

License:Open Source License

public JSonSupport() {
    TypeToken<Map<Id, PatchSetApproval>> approvalMapType = new TypeToken<Map<ApprovalCategory.Id, PatchSetApproval>>() {
    };//from w w w . ja v a 2 s  . c o  m
    ExclusionStrategy exclustionStrategy = new ExclusionStrategy() {

        public boolean shouldSkipField(FieldAttributes f) {
            // commentLinks requires instantiation of com.google.gwtexpui.safehtml.client.RegexFindReplace which is not on classpath
            if (f.getDeclaredClass() == List.class && f.getName().equals("commentLinks") //$NON-NLS-1$
                    && f.getDeclaringClass() == GerritConfig.class) {
                return true;
            }
            if (f.getDeclaredClass() == Map.class && f.getName().equals("given")) { //$NON-NLS-1$
                //return true;
            }
            // GSon 2.1 fails to deserialize the SubmitType enum
            if (f.getDeclaringClass() == Project.class && f.getName().equals("submitType")) { //$NON-NLS-1$
                return true;
            }
            return false;
        }

        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    };
    gson = JsonServlet.defaultGsonBuilder()
            .registerTypeAdapter(JSonResponse.class, new JSonResponseDeserializer())
            .registerTypeAdapter(Edit.class, new JsonDeserializer<Edit>() {
                public Edit deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    if (json.isJsonArray()) {
                        JsonArray array = json.getAsJsonArray();
                        if (array.size() == 4) {
                            return new Edit(array.get(0).getAsInt(), array.get(1).getAsInt(),
                                    array.get(2).getAsInt(), array.get(3).getAsInt());
                        }
                    }
                    return new Edit(0, 0);
                }
            })
            // ignore GerritForge specific AuthType "TEAMFORGE" which is unknown to Gerrit
            .registerTypeAdapter(AuthType.class, new JsonDeserializer<AuthType>() {

                @Override
                public AuthType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    String jsonString = json.getAsString();
                    if (jsonString != null) {
                        try {
                            return AuthType.valueOf(jsonString);
                        } catch (IllegalArgumentException e) {
                            // ignore the error since the connector does not make use of AuthType
                            //GerritCorePlugin.logWarning("Ignoring unkown authentication type: " + jsonString, e);
                        }
                    }
                    return null;
                }
            })
            .registerTypeAdapter(approvalMapType.getType(), new JsonDeserializer<Map<Id, PatchSetApproval>>() {

                @Override
                public Map<Id, PatchSetApproval> deserialize(JsonElement json, Type typeOfT,
                        JsonDeserializationContext context) throws JsonParseException {
                    // Gerrit 2.2: the type of PatchSetPublishDetail.given changed from a map to a list
                    Map<Id, PatchSetApproval> map = new HashMap<ApprovalCategory.Id, PatchSetApproval>();
                    if (json.isJsonArray()) {
                        JsonArray array = json.getAsJsonArray();
                        for (Iterator<JsonElement> it = array.iterator(); it.hasNext();) {
                            JsonElement element = it.next();
                            Id key = context.deserialize(element, Id.class);
                            if (key.get() != null) {
                                // Gerrit < 2.1.x: json is map
                                element = it.next();
                            }
                            PatchSetApproval value = context.deserialize(element, PatchSetApproval.class);
                            if (key.get() == null) {
                                // Gerrit 2.2: json is a list, deduct key from value
                                key = value.getCategoryId();
                            }
                            map.put(key, value);
                        }
                    }
                    return map;
                }
            }).setExclusionStrategies(exclustionStrategy).create();
}

From source file:org.eclipse.packagedrone.repo.adapter.maven.NodeAdapter.java

License:Open Source License

@Override
public Node deserialize(final JsonElement element, final Type type, final JsonDeserializationContext ctx)
        throws JsonParseException {

    final JsonObject o = element.getAsJsonObject();
    final String typeString = o.get("type").getAsString();
    final JsonObject val = o.get("node").getAsJsonObject();

    switch (typeString) {
    case "DirectoryNode":
        return ctx.deserialize(val, DirectoryNode.class);
    case "DataNode":
        return ctx.deserialize(val, DataNode.class);
    case "ArtifactNode":
        return ctx.deserialize(val, ArtifactNode.class);
    }//from   www.j  a  v  a 2s  .co  m

    return null;
}

From source file:org.eclipse.packagedrone.repo.channel.impl.DeployGroupTypeAdapter.java

License:Open Source License

private static Instant getAsTimestamp(final JsonDeserializationContext ctx, final JsonObject o2) {
    final Object result = ctx.deserialize(o2.get("timestamp"), Date.class);
    if (result == null) {
        return null;
    }//from   w  w w .j  a  v  a2s  .  c o  m
    return ((Date) result).toInstant();
}