Search in sources :

Example 36 with JsonReader

use of com.google.gson.stream.JsonReader in project Engine by VoltzEngine-Project.

the class JsonLoader method loadJson.

/**
     * Loads a json file from a reader
     *
     * @param fileName - file the reader loaded from, used only for error logs
     * @param reader   - reader with the data
     * @param entries  - place to put json entries into
     */
public static void loadJson(String fileName, Reader reader, HashMap<String, List<JsonEntry>> entries) {
    try {
        JsonReader jsonReader = new JsonReader(reader);
        JsonElement element = Streams.parse(jsonReader);
        loadJsonElement(fileName, element, entries);
    } catch (Exception e) {
        throw new RuntimeException("Failed to parse file '" + fileName + "'", e);
    }
}
Also used : JsonElement(com.google.gson.JsonElement) JsonReader(com.google.gson.stream.JsonReader) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Example 37 with JsonReader

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

the class StudyProjectGenerator method getCourseInfo.

/**
   * Parses course json meta file and finds course name
   *
   * @return information about course or null if course file is invalid
   */
@Nullable
private static CourseInfo getCourseInfo(File courseFile) {
    if (courseFile.isDirectory()) {
        File[] courseFiles = courseFile.listFiles((dir, name) -> name.equals(EduNames.COURSE_META_FILE));
        if (courseFiles == null || courseFiles.length != 1) {
            LOG.info("More than one or without course files");
            return null;
        }
        courseFile = courseFiles[0];
    }
    CourseInfo courseInfo = null;
    BufferedReader reader = null;
    try {
        if (courseFile.getName().equals(EduNames.COURSE_META_FILE)) {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(courseFile), "UTF-8"));
            JsonReader r = new JsonReader(reader);
            JsonParser parser = new JsonParser();
            JsonElement el = parser.parse(r);
            String courseName = el.getAsJsonObject().get(COURSE_NAME_ATTRIBUTE).getAsString();
            String courseDescription = el.getAsJsonObject().get(COURSE_DESCRIPTION).getAsString();
            JsonArray courseAuthors = el.getAsJsonObject().get(AUTHOR_ATTRIBUTE).getAsJsonArray();
            String language = el.getAsJsonObject().get(LANGUAGE_ATTRIBUTE).getAsString();
            courseInfo = new CourseInfo();
            courseInfo.setName(courseName);
            courseInfo.setDescription(courseDescription);
            courseInfo.setType("pycharm " + language);
            final ArrayList<StepicUser> authors = new ArrayList<>();
            for (JsonElement author : courseAuthors) {
                final JsonObject authorAsJsonObject = author.getAsJsonObject();
                final StepicUser stepicUser = new StepicUser();
                stepicUser.setFirstName(authorAsJsonObject.get("first_name").getAsString());
                stepicUser.setLastName(authorAsJsonObject.get("last_name").getAsString());
                authors.add(stepicUser);
            }
            courseInfo.setAuthors(authors);
        }
    } catch (Exception e) {
    //error will be shown in UI
    } finally {
        StudyUtils.closeSilently(reader);
    }
    return courseInfo;
}
Also used : CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) JsonReader(com.google.gson.stream.JsonReader) StepicUser(com.jetbrains.edu.learning.stepic.StepicUser) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) PsiFile(com.intellij.psi.PsiFile) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with JsonReader

use of com.google.gson.stream.JsonReader in project Minechem by iopleke.

the class ElementHandler method readFromStream.

private static void readFromStream(InputStream stream) {
    JsonReader jReader = new JsonReader(new InputStreamReader(stream));
    JsonParser parser = new JsonParser();
    Set<Map.Entry<String, JsonElement>> elementsSet = parser.parse(jReader).getAsJsonObject().entrySet();
    int count = 0;
    for (Map.Entry<String, JsonElement> elementEntry : elementsSet) {
        if (!elementEntry.getValue().isJsonObject()) {
            continue;
        }
        JsonObject elementObject = elementEntry.getValue().getAsJsonObject();
        ElementRegistry.getInstance().registerElement(Integer.parseInt(elementEntry.getKey()), elementObject.get("longName").getAsString(), elementObject.get("shortName").getAsString(), elementObject.get("form").getAsString(), elementObject.get("type").getAsString(), Integer.parseInt(elementObject.get("neutrons").getAsString()));
        count++;
    }
    LogHelper.info("Total of " + count + " elements registered");
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) JsonParser(com.google.gson.JsonParser)

Example 39 with JsonReader

use of com.google.gson.stream.JsonReader in project gerrit by GerritCodeReview.

the class ChangeEditIT method readContentFromJson.

private <T> T readContentFromJson(RestResponse r, Class<T> clazz) throws Exception {
    r.assertOK();
    JsonReader jsonReader = new JsonReader(r.getReader());
    jsonReader.setLenient(true);
    return newGson().fromJson(jsonReader, clazz);
}
Also used : JsonReader(com.google.gson.stream.JsonReader)

Example 40 with JsonReader

use of com.google.gson.stream.JsonReader in project gerrit by GerritCodeReview.

the class ChangeReviewersIT method readContentFromJson.

private static <T> T readContentFromJson(RestResponse r, int expectedStatus, Class<T> clazz) throws Exception {
    r.assertStatus(expectedStatus);
    JsonReader jsonReader = new JsonReader(r.getReader());
    jsonReader.setLenient(true);
    return newGson().fromJson(jsonReader, clazz);
}
Also used : JsonReader(com.google.gson.stream.JsonReader)

Aggregations

JsonReader (com.google.gson.stream.JsonReader)100 StringReader (java.io.StringReader)38 JsonElement (com.google.gson.JsonElement)31 IOException (java.io.IOException)20 JsonObject (com.google.gson.JsonObject)19 Test (org.junit.Test)19 InputStreamReader (java.io.InputStreamReader)18 JsonParser (com.google.gson.JsonParser)12 HumanReadableException (com.facebook.buck.util.HumanReadableException)10 Gson (com.google.gson.Gson)10 JsonWriter (com.google.gson.stream.JsonWriter)10 TypeToken (com.google.gson.reflect.TypeToken)8 Map (java.util.Map)8 JsonToken (com.google.gson.stream.JsonToken)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 StringWriter (java.io.StringWriter)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 GsonBuilder (com.google.gson.GsonBuilder)4