Search in sources :

Example 6 with JsonElement

use of org.apache.hbase.thirdparty.com.google.gson.JsonElement in project hbase by apache.

the class RESTApiClusterManager method getServiceName.

// Convert a service (e.g. "HBASE," "HDFS") into a service name (e.g. "HBASE-1," "HDFS-1").
private String getServiceName(Service service) {
    String serviceName = null;
    URI uri = UriBuilder.fromUri(serverHostname).path("api").path(API_VERSION).path("clusters").path(clusterName).path("services").build();
    JsonElement services = parser.parse(getFromURIGet(uri)).getAsJsonObject().get("items");
    if (services != null) {
        // Iterate through the list of services, stopping once the requested one is found.
        for (JsonElement serviceEntry : services.getAsJsonArray()) {
            if (serviceEntry.getAsJsonObject().get("type").getAsString().equals(service.toString())) {
                serviceName = serviceEntry.getAsJsonObject().get("name").getAsString();
                break;
            }
        }
    }
    return serviceName;
}
Also used : JsonElement(org.apache.hbase.thirdparty.com.google.gson.JsonElement) URI(java.net.URI)

Example 7 with JsonElement

use of org.apache.hbase.thirdparty.com.google.gson.JsonElement in project hbase by apache.

the class ProtobufUtil method toProcedureJson.

/**
 * Helper to convert the protobuf Procedure to JSON String
 * @return Convert the current Protocol Buffers Procedure to JSON String
 */
public static String toProcedureJson(List<ProcedureProtos.Procedure> procProtos) {
    JsonArray procJsons = new JsonArray(procProtos.size());
    for (ProcedureProtos.Procedure procProto : procProtos) {
        try {
            JsonElement procJson = ProtobufMessageConverter.toJsonElement(procProto);
            procJsons.add(procJson);
        } catch (InvalidProtocolBufferException e) {
            procJsons.add(e.toString());
        }
    }
    return procJsons.toString();
}
Also used : JsonArray(org.apache.hbase.thirdparty.com.google.gson.JsonArray) JsonElement(org.apache.hbase.thirdparty.com.google.gson.JsonElement) InvalidProtocolBufferException(org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException) ProcedureProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos)

Example 8 with JsonElement

use of org.apache.hbase.thirdparty.com.google.gson.JsonElement in project hbase by apache.

the class ProtobufMessageConverter method toJavaObject.

private static Object toJavaObject(JsonElement element) {
    if (element.isJsonNull()) {
        return null;
    } else if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isNumber()) {
            return primitive.getAsNumber();
        } else if (primitive.isString()) {
            return primitive.getAsString();
        } else {
            return null;
        }
    } else if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        List<Object> list = new ArrayList<>();
        for (JsonElement arrayElement : array) {
            Object javaObject = toJavaObject(arrayElement);
            list.add(javaObject);
        }
        return list;
    } else if (element.isJsonObject()) {
        JsonObject object = element.getAsJsonObject();
        Map<String, Object> map = new LinkedHashMap<>();
        for (Entry<String, JsonElement> entry : object.entrySet()) {
            Object javaObject = toJavaObject(entry.getValue());
            map.put(entry.getKey(), javaObject);
        }
        return map;
    } else {
        return null;
    }
}
Also used : JsonArray(org.apache.hbase.thirdparty.com.google.gson.JsonArray) Entry(java.util.Map.Entry) JsonPrimitive(org.apache.hbase.thirdparty.com.google.gson.JsonPrimitive) JsonElement(org.apache.hbase.thirdparty.com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(org.apache.hbase.thirdparty.com.google.gson.JsonObject) JsonObject(org.apache.hbase.thirdparty.com.google.gson.JsonObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

JsonElement (org.apache.hbase.thirdparty.com.google.gson.JsonElement)8 JsonArray (org.apache.hbase.thirdparty.com.google.gson.JsonArray)4 URI (java.net.URI)3 JsonObject (org.apache.hbase.thirdparty.com.google.gson.JsonObject)3 InvalidProtocolBufferException (org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 LockServiceProtos (org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos)1 ProcedureProtos (org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos)1 JsonParser (org.apache.hbase.thirdparty.com.google.gson.JsonParser)1 JsonPrimitive (org.apache.hbase.thirdparty.com.google.gson.JsonPrimitive)1