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;
}
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;
}
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;
}
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;
}
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());
}
Aggregations