Search in sources :

Example 91 with JsonArray

use of com.google.gson.JsonArray in project intellij-plugins by StepicOrg.

the class ReplyDeserializer method deserialize.

@Override
public Reply deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json == null || !(json instanceof JsonObject)) {
        return null;
    }
    Reply reply = new Reply();
    JsonObject object = json.getAsJsonObject();
    String language = getString(object, "language");
    reply.setLanguage(language);
    String code = getString(object, "code");
    reply.setCode(code);
    String formula = getString(object, "formula");
    reply.setFormula(formula);
    String text = getString(object, "text");
    reply.setText(text);
    String number = getString(object, "number");
    reply.setNumber(number);
    JsonArray ordering = getJsonArray(object, "ordering");
    if (ordering != null) {
        List<Integer> intOrdering = new ArrayList<>();
        ordering.forEach(item -> intOrdering.add(item.getAsInt()));
        reply.setOrdering(intOrdering);
    }
    reply.setAttachments(getList(object, "attachments", (jsonElement) -> context.deserialize(jsonElement, Attachment.class)));
    reply.setFiles(getStringList(object, "files"));
    JsonArray choices = getJsonArray(object, "choices");
    if (choices != null && choices.size() > 0) {
        if (choices.get(0).isJsonPrimitive()) {
            List<Boolean> list = new ArrayList<>();
            choices.forEach(item -> list.add(item.getAsBoolean()));
            reply.setChoices(list);
        } else {
            List<Choice> list = new ArrayList<>();
            choices.forEach(item -> list.add(context.deserialize(item, Choice.class)));
            reply.setChoices(list);
        }
    }
    reply.setBlanks(getStringList(object, "blanks"));
    return reply;
}
Also used : Utils.getJsonArray(org.stepik.api.Utils.getJsonArray) JsonArray(com.google.gson.JsonArray) JsonParseException(com.google.gson.JsonParseException) JsonObject(com.google.gson.JsonObject) Utils.getList(org.stepik.api.Utils.getList) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) ArrayList(java.util.ArrayList) Utils.getJsonArray(org.stepik.api.Utils.getJsonArray) JsonElement(com.google.gson.JsonElement) JsonArray(com.google.gson.JsonArray) List(java.util.List) Type(java.lang.reflect.Type) Utils.getString(org.stepik.api.Utils.getString) Utils.getStringList(org.stepik.api.Utils.getStringList) JsonDeserializer(com.google.gson.JsonDeserializer) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Utils.getString(org.stepik.api.Utils.getString)

Example 92 with JsonArray

use of com.google.gson.JsonArray in project intellij-plugins by StepicOrg.

the class Utils method getList.

@Nullable
public static <T> List<T> getList(@NotNull JsonObject object, @NotNull String fieldName, @NotNull Function<JsonElement, T> getter) {
    if (!object.has(fieldName) || !object.get(fieldName).isJsonArray()) {
        return null;
    }
    JsonArray array = getJsonArray(object, fieldName);
    if (array != null) {
        List<T> list = new ArrayList<>();
        array.forEach(item -> list.add(getter.apply(item)));
        return list;
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable)

Example 93 with JsonArray

use of com.google.gson.JsonArray in project intellij-plugins by StepicOrg.

the class DatasetDeserializer method deserialize.

@Override
public Dataset deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json == null || !(json instanceof JsonObject)) {
        return null;
    }
    Dataset dataset = new Dataset();
    JsonObject object = json.getAsJsonObject();
    Boolean multipleChoice = getBoolean(object, "is_multiple_choice");
    dataset.setMultipleChoice(multipleChoice);
    Boolean textDisabled = getBoolean(object, "is_text_disabled");
    dataset.setTextDisabled(textDisabled);
    dataset.setOptions(getStringList(object, "options"));
    JsonArray pairs = getJsonArray(object, "pairs");
    if (pairs != null) {
        List<StringPair> array = new ArrayList<>();
        pairs.forEach(pair -> array.add(context.deserialize(pair, StringPair.class)));
        dataset.setPairs(array);
    }
    dataset.setRows(getStringList(object, "rows"));
    dataset.setColumns(getStringList(object, "columns"));
    Boolean isCheckbox = getBoolean(object, "is_checkbox");
    dataset.setCheckbox(isCheckbox);
    String description = getString(object, "description");
    dataset.setDescription(description);
    JsonArray components = getJsonArray(object, "components");
    if (components != null) {
        List<Component> array = new ArrayList<>();
        components.forEach(component -> array.add(context.deserialize(component, Component.class)));
        dataset.setComponents(array);
    }
    return dataset;
}
Also used : Utils.getJsonArray(org.stepik.api.Utils.getJsonArray) JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Utils.getString(org.stepik.api.Utils.getString) Utils.getBoolean(org.stepik.api.Utils.getBoolean)

Example 94 with JsonArray

use of com.google.gson.JsonArray in project pratilipi by Pratilipi.

the class PratilipiContentDocImpl method getIndex.

@Override
public JsonArray getIndex() {
    JsonArray index = new JsonArray();
    if (chapters != null) {
        for (int chapterNo = 1; chapterNo <= chapters.size(); chapterNo++) {
            Chapter chapter = chapters.get(chapterNo - 1);
            JsonObject indexItem = new JsonObject();
            indexItem.addProperty("chapterNo", chapterNo);
            indexItem.addProperty("title", chapter.getTitle());
            indexItem.addProperty("nesting", chapter.getNesting());
            index.add(indexItem);
        }
    }
    return index;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject)

Example 95 with JsonArray

use of com.google.gson.JsonArray in project intellij-community by JetBrains.

the class JiraIntegrationTest method testSetTimeSpend.

public void testSetTimeSpend() throws Exception {
    // only REST API 2.0 supports this feature
    myRepository.setUrl(JIRA_5_TEST_SERVER_URL);
    final String issueId = createIssueViaRestApi("BTTTU", "Test issue for time tracking updates (" + SHORT_TIMESTAMP_FORMAT.format(new Date()) + ")");
    final Task task = myRepository.findTask(issueId);
    assertNotNull("Test task not found", task);
    // timestamp as comment
    final String comment = "Timestamp: " + TaskUtil.formatDate(new Date());
    final Couple<Integer> duration = generateWorkItemDuration();
    final int hours = duration.getFirst(), minutes = duration.getSecond();
    myRepository.updateTimeSpent(new LocalTaskImpl(task), String.format("%dh %dm", hours, minutes), comment);
    final GetMethod request = new GetMethod(myRepository.getRestUrl("issue", task.getId(), "worklog"));
    final String response = myRepository.executeMethod(request);
    final JsonObject object = new Gson().fromJson(response, JsonObject.class);
    final JsonArray worklogs = object.get("worklogs").getAsJsonArray();
    final JsonObject last = worklogs.get(worklogs.size() - 1).getAsJsonObject();
    assertEquals(comment, last.get("comment").getAsString());
    // don't depend on concrete response format: zero hours stripping, zero padding and so on
    assertEquals((hours * 60 + minutes) * 60, last.get("timeSpentSeconds").getAsInt());
}
Also used : JsonArray(com.google.gson.JsonArray) LocalTaskImpl(com.intellij.tasks.impl.LocalTaskImpl) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Aggregations

JsonArray (com.google.gson.JsonArray)424 JsonObject (com.google.gson.JsonObject)290 JsonElement (com.google.gson.JsonElement)167 JsonPrimitive (com.google.gson.JsonPrimitive)118 Test (org.testng.annotations.Test)59 JsonParser (com.google.gson.JsonParser)57 ArrayList (java.util.ArrayList)50 HashMap (java.util.HashMap)34 Map (java.util.Map)34 Gson (com.google.gson.Gson)22 IOException (java.io.IOException)17 Test (org.junit.Test)16 Type (java.lang.reflect.Type)12 List (java.util.List)11 Matchers.anyString (org.mockito.Matchers.anyString)10 TextView (android.widget.TextView)9 Point (android.graphics.Point)8 JsonParseException (com.google.gson.JsonParseException)7 GsonUtilities.jboolean (com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean)7 KcaUtils.getStringFromException (com.antest1.kcanotify.KcaUtils.getStringFromException)6