Search in sources :

Example 1 with JsonArray

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

the class ProtobufUtil method toLockJson.

public static String toLockJson(List<LockServiceProtos.LockedResource> lockedResourceProtos) {
    JsonArray lockedResourceJsons = new JsonArray(lockedResourceProtos.size());
    for (LockServiceProtos.LockedResource lockedResourceProto : lockedResourceProtos) {
        try {
            JsonElement lockedResourceJson = ProtobufMessageConverter.toJsonElement(lockedResourceProto);
            lockedResourceJsons.add(lockedResourceJson);
        } catch (InvalidProtocolBufferException e) {
            lockedResourceJsons.add(e.toString());
        }
    }
    return lockedResourceJsons.toString();
}
Also used : JsonArray(org.apache.hbase.thirdparty.com.google.gson.JsonArray) LockServiceProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos) JsonElement(org.apache.hbase.thirdparty.com.google.gson.JsonElement) InvalidProtocolBufferException(org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException)

Example 2 with JsonArray

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

the class ProcedureTestUtil method getProcedure.

private static Optional<JsonObject> getProcedure(HBaseTestingUtil util, Class<? extends Procedure<?>> clazz, JsonParser parser) throws IOException {
    JsonArray array = parser.parse(util.getAdmin().getProcedures()).getAsJsonArray();
    Iterator<JsonElement> iterator = array.iterator();
    while (iterator.hasNext()) {
        JsonElement element = iterator.next();
        JsonObject obj = element.getAsJsonObject();
        String className = obj.get("className").getAsString();
        if (className.equals(clazz.getName())) {
            return Optional.of(obj);
        }
    }
    return Optional.empty();
}
Also used : JsonArray(org.apache.hbase.thirdparty.com.google.gson.JsonArray) JsonElement(org.apache.hbase.thirdparty.com.google.gson.JsonElement) JsonObject(org.apache.hbase.thirdparty.com.google.gson.JsonObject)

Example 3 with JsonArray

use of org.apache.hbase.thirdparty.com.google.gson.JsonArray 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 4 with JsonArray

use of org.apache.hbase.thirdparty.com.google.gson.JsonArray 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

JsonArray (org.apache.hbase.thirdparty.com.google.gson.JsonArray)4 JsonElement (org.apache.hbase.thirdparty.com.google.gson.JsonElement)4 JsonObject (org.apache.hbase.thirdparty.com.google.gson.JsonObject)2 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 JsonPrimitive (org.apache.hbase.thirdparty.com.google.gson.JsonPrimitive)1