From c4f1fffcdb59ee7c92cdc56dd866cfdcfa937be1 Mon Sep 17 00:00:00 2001 From: Valentina Kiryushkina Date: Mon, 21 Nov 2016 15:55:38 +0300 Subject: [PATCH] Fix json serialization for stepic adaptive courses which has no files and no subtasks --- .../edu/learning/StudySerializationUtils.java | 56 ++++++++++--------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java b/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java index 279fad1248fc..f4a264d40fff 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java @@ -476,23 +476,26 @@ public class StudySerializationUtils { } private static JsonObject convertSubtaskInfosToMap(JsonObject stepOptionsJson) { - for (JsonElement taskFileElement : stepOptionsJson.getAsJsonArray(FILES)) { - JsonObject taskFileObject = taskFileElement.getAsJsonObject(); - JsonArray placeholders = taskFileObject.getAsJsonArray(PLACEHOLDERS); - for (JsonElement placeholder : placeholders) { - JsonObject placeholderObject = placeholder.getAsJsonObject(); - JsonArray infos = placeholderObject.getAsJsonArray(SUBTASK_INFOS); - Map objectsToInsert = new HashMap<>(); - for (JsonElement info : infos) { - JsonObject object = info.getAsJsonObject(); - int index = object.getAsJsonPrimitive(INDEX).getAsInt(); - objectsToInsert.put(index, object); - } - placeholderObject.remove(SUBTASK_INFOS); - JsonObject newInfos = new JsonObject(); - placeholderObject.add(SUBTASK_INFOS, newInfos); - for (Map.Entry entry : objectsToInsert.entrySet()) { - newInfos.add(entry.getKey().toString(), entry.getValue()); + final JsonArray files = stepOptionsJson.getAsJsonArray(FILES); + if (files != null) { + for (JsonElement taskFileElement : files) { + JsonObject taskFileObject = taskFileElement.getAsJsonObject(); + JsonArray placeholders = taskFileObject.getAsJsonArray(PLACEHOLDERS); + for (JsonElement placeholder : placeholders) { + JsonObject placeholderObject = placeholder.getAsJsonObject(); + JsonArray infos = placeholderObject.getAsJsonArray(SUBTASK_INFOS); + Map objectsToInsert = new HashMap<>(); + for (JsonElement info : infos) { + JsonObject object = info.getAsJsonObject(); + int index = object.getAsJsonPrimitive(INDEX).getAsInt(); + objectsToInsert.put(index, object); + } + placeholderObject.remove(SUBTASK_INFOS); + JsonObject newInfos = new JsonObject(); + placeholderObject.add(SUBTASK_INFOS, newInfos); + for (Map.Entry entry : objectsToInsert.entrySet()) { + newInfos.add(entry.getKey().toString(), entry.getValue()); + } } } } @@ -501,14 +504,17 @@ public class StudySerializationUtils { private static JsonObject convertToSecondVersion(JsonObject stepOptionsJson) { Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); - for (JsonElement taskFileElement : stepOptionsJson.getAsJsonArray(FILES)) { - JsonObject taskFileObject = taskFileElement.getAsJsonObject(); - JsonArray placeholders = taskFileObject.getAsJsonArray(PLACEHOLDERS); - for (JsonElement placeholder : placeholders) { - JsonObject placeholderObject = placeholder.getAsJsonObject(); - convertToAbsoluteOffset(taskFileObject, placeholderObject); - convertMultipleHints(gson, placeholderObject); - convertToSubtaskInfo(placeholderObject); + final JsonArray files = stepOptionsJson.getAsJsonArray(FILES); + if (files != null) { + for (JsonElement taskFileElement : files) { + JsonObject taskFileObject = taskFileElement.getAsJsonObject(); + JsonArray placeholders = taskFileObject.getAsJsonArray(PLACEHOLDERS); + for (JsonElement placeholder : placeholders) { + JsonObject placeholderObject = placeholder.getAsJsonObject(); + convertToAbsoluteOffset(taskFileObject, placeholderObject); + convertMultipleHints(gson, placeholderObject); + convertToSubtaskInfo(placeholderObject); + } } } return stepOptionsJson;