Search in sources :

Example 81 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project intellij-plugins by JetBrains.

the class ExtractMethodFeedback method toJson.

public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("offset", offset);
    jsonObject.addProperty("length", length);
    jsonObject.addProperty("returnType", returnType);
    JsonArray jsonArrayNames = new JsonArray();
    for (String elt : names) {
        jsonArrayNames.add(new JsonPrimitive(elt));
    }
    jsonObject.add("names", jsonArrayNames);
    jsonObject.addProperty("canCreateGetter", canCreateGetter);
    JsonArray jsonArrayParameters = new JsonArray();
    for (RefactoringMethodParameter elt : parameters) {
        jsonArrayParameters.add(elt.toJson());
    }
    jsonObject.add("parameters", jsonArrayParameters);
    JsonArray jsonArrayOffsets = new JsonArray();
    for (int elt : offsets) {
        jsonArrayOffsets.add(new JsonPrimitive(elt));
    }
    jsonObject.add("offsets", jsonArrayOffsets);
    JsonArray jsonArrayLengths = new JsonArray();
    for (int elt : lengths) {
        jsonArrayLengths.add(new JsonPrimitive(elt));
    }
    jsonObject.add("lengths", jsonArrayLengths);
    return jsonObject;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 82 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project intellij-plugins by JetBrains.

the class ContextData method toJson.

public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("name", name);
    jsonObject.addProperty("explicitFileCount", explicitFileCount);
    jsonObject.addProperty("implicitFileCount", implicitFileCount);
    jsonObject.addProperty("workItemQueueLength", workItemQueueLength);
    JsonArray jsonArrayCacheEntryExceptions = new JsonArray();
    for (String elt : cacheEntryExceptions) {
        jsonArrayCacheEntryExceptions.add(new JsonPrimitive(elt));
    }
    jsonObject.add("cacheEntryExceptions", jsonArrayCacheEntryExceptions);
    return jsonObject;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 83 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project CodeUtils by boredream.

the class JsonUtils method getJsonPrimitiveType.

/**
     * 将json元素中的json基础类型,转换为String.class,int.class等具体的类型
     *
     * @param je json元素
     * @return 具体数据类型, 无法预估的类型统一视为Object.class类型
     */
private static Class<?> getJsonPrimitiveType(JsonElement je) {
    Class<?> clazz = Object.class;
    JsonPrimitive jp = je.getAsJsonPrimitive();
    // json中的类型会将数字集合成一个总的number类型,需要分别判断
    if (jp.isNumber()) {
        String num = jp.getAsString();
        if (num.contains(".")) {
            // 如果包含"."则为小数,先尝试解析成float,如果失败则视为double
            try {
                Float.parseFloat(num);
                clazz = float.class;
            } catch (NumberFormatException e) {
                clazz = double.class;
            }
        } else {
            // 如果不包含"."则为整数,先尝试解析成int,如果失败则视为long
            try {
                Integer.parseInt(num);
                clazz = int.class;
            } catch (NumberFormatException e) {
                clazz = long.class;
            }
        }
    } else if (jp.isBoolean()) {
        clazz = boolean.class;
    } else if (jp.isString()) {
        clazz = String.class;
    }
    // json中没有其他具体类型如byte等
    return clazz;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 84 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project GeoGig by boundlessgeo.

the class HttpRemoteRepo method createFetchMessage.

private JsonObject createFetchMessage(List<ObjectId> want, Set<ObjectId> have) {
    JsonObject message = new JsonObject();
    JsonArray wantArray = new JsonArray();
    for (ObjectId id : want) {
        wantArray.add(new JsonPrimitive(id.toString()));
    }
    JsonArray haveArray = new JsonArray();
    for (ObjectId id : have) {
        haveArray.add(new JsonPrimitive(id.toString()));
    }
    message.add("want", wantArray);
    message.add("have", haveArray);
    return message;
}
Also used : JsonArray(com.google.gson.JsonArray) ObjectId(org.locationtech.geogig.api.ObjectId) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 85 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project tika by apache.

the class JsonMetadataSerializer method serialize.

/**
     * Serializes a Metadata object into effectively Map<String, String[]>.
     * 
     * @param metadata object to serialize
     * @param type (ignored)
     * @param context (ignored)
     * @return JsonElement with key/value(s) pairs or JsonNull if metadata is null.
     */
@Override
public JsonElement serialize(Metadata metadata, Type type, JsonSerializationContext context) {
    if (metadata == null) {
        return JsonNull.INSTANCE;
    }
    String[] names = getNames(metadata);
    if (names == null) {
        return JsonNull.INSTANCE;
    }
    JsonObject root = new JsonObject();
    for (String n : names) {
        String[] vals = metadata.getValues(n);
        if (vals == null) {
            //silently skip
            continue;
        }
        if (vals.length == 1) {
            root.addProperty(n, vals[0]);
        } else {
            JsonArray jArr = new JsonArray();
            for (int i = 0; i < vals.length; i++) {
                jArr.add(new JsonPrimitive(vals[i]));
            }
            root.add(n, jArr);
        }
    }
    return root;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Aggregations

JsonPrimitive (com.google.gson.JsonPrimitive)230 JsonObject (com.google.gson.JsonObject)137 JsonArray (com.google.gson.JsonArray)118 JsonElement (com.google.gson.JsonElement)106 Test (org.testng.annotations.Test)56 Test (org.junit.Test)12 Map (java.util.Map)10 Matchers.anyString (org.mockito.Matchers.anyString)8 JsonProcessorInjectionMap (com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap)7 Gson (com.google.gson.Gson)6 HashMap (java.util.HashMap)5 GsonBuilder (com.google.gson.GsonBuilder)4 JsonParser (com.google.gson.JsonParser)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Type (java.lang.reflect.Type)3 HashSet (java.util.HashSet)3 Matcher (java.util.regex.Matcher)3 LobWrapper (angularBeans.io.LobWrapper)2 DatasetCreationSpec (co.cask.cdap.internal.dataset.DatasetCreationSpec)2