Example usage for com.google.gson JsonObject add

List of usage examples for com.google.gson JsonObject add

Introduction

In this page you can find the example usage for com.google.gson JsonObject add.

Prototype

public void add(String property, JsonElement value) 

Source Link

Document

Adds a member, which is a name-value pair, to self.

Usage

From source file:com.cloudant.client.internal.views.ViewMultipleRequester.java

License:Open Source License

public List<ViewResponse<K, V>> getViewResponses() throws IOException {
    //build the queries array of data to POST
    JsonArray queries = new JsonArray();
    ViewQueryParameters<K, V> viewQueryParameters = null;
    for (ViewQueryParameters<K, V> params : requestParameters) {
        if (viewQueryParameters == null) {
            viewQueryParameters = params;
        }//  w  ww .  ja va 2 s  .  c  o m
        queries.add(params.asJson());
    }
    JsonObject queryJson = new JsonObject();
    queryJson.add("queries", queries);
    //construct and execute the POST request
    HttpPost post = new HttpPost(viewQueryParameters.getViewURIBuilder().buildEncoded());
    StringEntity entity = new StringEntity(queryJson.toString(), "UTF-8");
    entity.setContentType("application/json");
    post.setEntity(entity);
    JsonObject jsonResponse = ViewRequester.executeRequestWithResponseAsJson(viewQueryParameters, post);
    //loop the returned array creating the ViewResponse objects
    List<ViewResponse<K, V>> responses = new ArrayList<ViewResponse<K, V>>();
    JsonArray jsonResponses = jsonResponse.getAsJsonArray("results");
    if (jsonResponses != null) {
        int index = 0;
        for (ViewQueryParameters<K, V> params : requestParameters) {
            JsonObject response = jsonResponses.get(index).getAsJsonObject();
            responses.add(new ViewResponseImpl<K, V>(params, response));
            index++;
        }
        return responses;
    } else {
        return Collections.emptyList();
    }
}

From source file:com.cloudant.client.org.lightcouch.Replication.java

License:Open Source License

private JsonObject createJson() {
    JsonObject json = new JsonObject();
    addProperty(json, "source", source);
    addProperty(json, "cancel", cancel);
    addProperty(json, "continuous", continuous);
    addProperty(json, "filter", filter);

    if (queryParams != null) {
        json.add("query_params", queryParams);
    }/*from   w w  w  . j  a  v  a  2s . co  m*/
    if (docIds != null) {
        json.add("doc_ids", client.getGson().toJsonTree(docIds, String[].class));
    }

    addProperty(json, "proxy", proxy);
    addProperty(json, "since_seq", sinceSeq);
    addProperty(json, "create_target", createTarget);

    if (targetOauth != null) {
        JsonObject auth = new JsonObject();
        JsonObject oauth = new JsonObject();
        addProperty(oauth, "consumer_secret", consumerSecret);
        addProperty(oauth, "consumer_key", consumerKey);
        addProperty(oauth, "token_secret", tokenSecret);
        addProperty(oauth, "token", token);

        addProperty(targetOauth, "url", target);
        auth.add("oauth", oauth);

        targetOauth.add("auth", auth);
        json.add("target", targetOauth);
    } else {
        addProperty(json, "target", target);
    }
    return json;
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

public static String toJsonBinaryType(SourceTypeBinding binding) {
    JsonObject object = new JsonObject();
    if (!binding.isAnnotationType()) {
        object.add("annotations", toJsonAnnotations(binding.getAnnotations()));
    } else {//from  w ww.ja  v a2  s  .c o m
        object.add("annotations", JsonNull.INSTANCE);

    }
    object.add("enclosingMethod", null);
    object.add("enclosingTypeName", binding.enclosingType() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(binding.enclosingType().constantPoolName())));

    object.add("fields", toJsonFields(binding.fields()));
    object.add("genericSignature", binding.genericSignature() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(binding.genericSignature())));
    object.add("interfaceNames", toJsonInterfaces(binding.superInterfaces()));
    object.add("memberTypes", toJsonMemberTypes(binding.memberTypes()));
    object.add("methods", toJsonMethods(binding.methods()));
    object.add("missingTypeNames", null);
    object.add("name", binding.constantPoolName() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(binding.constantPoolName())));
    object.add("sourceName", binding.sourceName() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(binding.sourceName())));
    object.add("superclassName", binding.superclass() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(binding.superclass().constantPoolName())));
    long annotationTagBits = binding.getAnnotationTagBits();
    //remove AreMethodsComplete bit tag from type
    annotationTagBits &= ~TagBits.AreMethodsComplete;

    object.add("tagBits", new JsonPrimitive(String.valueOf(annotationTagBits)));
    object.add("anonymous", new JsonPrimitive(binding.isAnonymousType()));
    object.add("local", new JsonPrimitive(binding.isLocalType()));
    object.add("member", new JsonPrimitive(binding.isMemberType()));
    object.add("sourceFileName", binding.sourceName() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(binding.sourceName())));
    object.add("modifiers", new JsonPrimitive(binding.modifiers));
    object.add("binaryType", new JsonPrimitive(true));
    object.add("fileName", null);
    return gson.toJson(object);
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonMethod(MethodBinding method) {
    JsonObject object = new JsonObject();
    object.addProperty("modifiers", method.getAccessFlags());
    object.addProperty("constructor", method.isConstructor());
    object.add("argumentNames", toJsonParametersName(method.sourceMethod()));
    object.add("annotations", toJsonAnnotations(method.getAnnotations()));
    object.add("defaultValue", toJsonDefaultValue(method.getDefaultValue()));
    object.add("exceptionTypeNames", toJsonExceptionTypeNames(method.thrownExceptions));
    object.add("genericSignature", method.genericSignature() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(method.genericSignature())));
    object.add("methodDescriptor",
            method.signature() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(method.signature())));
    object.add("parameterAnnotations", toJsonParameterAnnotations(method));
    object.add("selector",
            method.selector == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(method.selector)));
    object.addProperty("tagBits", String.valueOf(method.getAnnotationTagBits()));
    object.addProperty("clinit", false);
    return object;
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonDefaultValue(Object defaultValue) {
    if (defaultValue == null)
        return JsonNull.INSTANCE;
    JsonObject object = new JsonObject();
    if (defaultValue instanceof Constant) {
        object.add("constant", BinaryTypeConvector.toJsonConstant((Constant) defaultValue));
    } else if (defaultValue instanceof TypeBinding) {
        object.addProperty("class", new String(((TypeBinding) defaultValue).constantPoolName()));
    } else if (defaultValue instanceof AnnotationBinding) {
        object.add("annotation", toJsonAnnotation((AnnotationBinding) defaultValue));
    } else if (defaultValue instanceof FieldBinding) {
        FieldBinding signature = (FieldBinding) defaultValue;
        JsonObject enumSignature = new JsonObject();
        enumSignature.addProperty("typeName", new String(signature.type.signature()));
        enumSignature.addProperty("constantName", new String(signature.name));
        object.add("enum", enumSignature);
    } else if (defaultValue instanceof Object[]) {
        JsonArray array = new JsonArray();
        for (Object o : (Object[]) defaultValue) {
            array.add(toJsonDefaultValue(o));
        }/*from  w w w. j  ava2  s.c o  m*/
        object.add("array", array);
    }

    return object;
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonMemberType(ReferenceBinding type) {
    JsonObject object = new JsonObject();
    object.add("enclosingTypeName", type.enclosingType() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(type.enclosingType().constantPoolName())));
    object.addProperty("modifiers", type.modifiers);
    object.add("name", type.constantPoolName() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(type.constantPoolName())));
    return object;
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonField(FieldBinding field) {
    JsonObject object = new JsonObject();
    object.addProperty("modifiers", field.modifiers);
    object.add("constant", BinaryTypeConvector.toJsonConstant(field.constant()));
    object.add("genericSignature", field.genericSignature() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(field.genericSignature())));
    object.add("name", field.name == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(field.name)));
    object.addProperty("tagBits", String.valueOf(field.tagBits));
    object.add("typeName",
            field.type == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(field.type.signature())));
    object.add("annotations", toJsonAnnotations(field.getAnnotations()));
    return object;
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonAnnotation(AnnotationBinding annotation) {
    JsonObject object = new JsonObject();
    object.add("typeName", annotation.getAnnotationType() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(annotation.getAnnotationType().constantPoolName())));
    object.add("elementValuePairs", toJsonElementValuePairs(annotation.getElementValuePairs()));
    return object;
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonElementValuePair(ElementValuePair pair) {
    JsonObject object = new JsonObject();
    object.add("name",
            pair.getName() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(pair.getName())));
    object.add("value", toJsonDefaultValue(pair.getValue()));
    return object;
}

From source file:com.cognifide.actions.msg.push.PushMessageProducer.java

License:Apache License

@Override
public boolean sendMessage(String type, Map<String, String> message) {
    final JsonObject msg = new JsonObject();
    msg.addProperty("type", type);
    msg.add("payload", GSON.toJsonTree(message));
    final String serializedMsg = GSON.toJson(msg);
    return sender.sendMessage("ACTION", serializedMsg);
}