Three common JSON parsing performance comparison

Today I used three third-party packages to parse JSON files and tested their performance:

JSON file (for testing):
private static String json = "{\"id\":1,\"title\":\"Black Forest, Germany Rare rainstorm washed the road away\",\n" +
            " \"questions\":[\n" +
            " {\"sequence\":1,\"title\":\"Who are you?\", \"answers\":[{\"result\":\"A:name\"},{\"result\":\"B:liuha\"},{\"result\":\"C :Integral\"},{\"result\":\"D:Golden Buddha's amount\"}]},\n" +
            " {\"sequence\":2,\"title\":\"German black A rare rainstorm in the forest washed away the road\",\"answers\":[{\"result\":\"A:name\"},{\"result\":\"B:liuha\"}, {\"result\":\"C:Integral\"},{\"result\":\"D: Golden Buddha's amount\"}]},\n" +
            " {\"sequence\":3,\"title\":\"Rare rainstorm in Germany's Black Forest washed away the road\",\"answers\":[ {\"result\":\"A:Name\"},{\"result\":\"B:Liuha\"},{\"result\":\"C:Points\"},{ \"result\":\"D:Golden Buddha's amount\"}]},\n" +
            " {\"sequence\":4,\"title\":\"You are the one\",\"answers\":[{\"result\":\"A:name\"},{\" result\":\"B: Liuha\"},{\"result\":\"C:Points\"},{\"result\":\"D:Golden Buddha's Amount\"}]}, \n" +
            " {\"sequence\":5,\"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed whole by a killer whale\",\"answers\":[{\"result\" :\"A: Name\"},{\"result\":\"B: Liuha\"},{\"result\":\"C:Points\"},{\"result\": \"D:Golden Buddha's Forehead\"}]},\n" +
            " {\"sequence\":6,\"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed by a killer whale\", \"answers\":[{\"result\":\"A:name\"},{\"result\":\"B:liuha\"},{\" result\":\"C:Points\"},{\"result\":\"D:Golden Buddha Amount\"}]},\n" +
            " {\"sequence\":7,\"title \":\"The beautiful dolphin is scarred and suspected to have been swallowed whole by a killer whale\",\"answers\":[{\"result\":\"A:name\"},{\"result\": \"B: Liu Ha\"},{\"result\":\"C:Points\"},{\"result\":\"D:Golden Buddha's Amount\"}]},\n" +
            " {\"sequence\":8,\"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed whole by a killer whale\",\"answers\":[{\"result\":\"A :Name\"},{\"result\":\"B:Liuha\"},{\"result\":\"C:Points\"},{\"result\":\"D: Golden Buddha's forehead\"}]},\n" +
            " {\"sequence\":9,\"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed by a killer whale\",\"answers\ ":[{\"result\":\"A:name\"},{\"result\":\"B:liuha\"},{\"result\":\"C:points\" },{\"result\":\"D:Golden Buddha's Forehead\"}]},\n" +
            " {\"sequence\":10,\"title\":\"The beautiful dolphin is full of scars Eaten whole by killer whales\",\"answers\":[{\"result\":\"A:name\"},{\"result\":\"B:liuha\"}, {\"result\":\"C:Integral\"},{\"result\":\"D:Golden Buddha Amount\"}]},\n" +
            " {\"sequence\":11, \"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed whole by a killer whale\",\"answers\":[{\"result\":\"A:name\"},{\"result \":\"B: Six Ha\"},{\"result\":\"C:Points\"},{\"result\":\"D:Golden Buddha's Amount\"}]},\ n" +
            " {\"sequence\":12,\"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed whole by a killer whale\",\"answers\":[{\"result\":\"A :Name\"},{\"result\":\"B:Liuha\"},{\"result\":\"C:Points\"},{\"result\":\"D: Golden Buddha's Forehead\"}]},\n" +
            " {\"sequence\":13,\"title\":\"Beautiful dolphins are scarred and suspected to have been swallowed whole by killer whales\",\"answers\ ":[{\"result\":\"A:name\"},{\"result\":\"B:liuha\"},{\"result\":\"C:points\" },{\"result\":\"D:Golden Buddha's Forehead\"}]},\n" +
            " {\"sequence\":14,\"title\":\"The beautiful dolphin is full of scars Eaten whole by killer whales\",\"answers\":[{\"result\":\"A:name\"},{\"result\":\"B:liuha\"}, {\"result\":\"C:Integral\"},{\"result\":\"D:Golden Buddha Amount\"}]},\n" +
            " {\"sequence\":15, \"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed whole by a killer whale\",\"answers\":[{\"result\":\"A:name\"},{\"result \":\"B: Six Ha\"},{\"result\":\"C:Points\"},{\"result\":\"D:Golden Buddha's Amount\"}]},\ n" +
            " {\"sequence\":16,\"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed whole by a killer whale\",\"answers\":[{\"result\":\"A :Name\"},{\"result\":\"B:Liuha\"},{\"result\":\"C:Points\"},{\"result\":\"D: Golden Buddha's forehead\"}]},\n" +
            " {\"sequence\":17,\"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed by a killer whale\",\"answers\ ":[{\"result\":\"A:name\"},{\"result\":\"B:liuha\"},{\"result\":\"C:points\" },{\"result\":\"D:Golden Buddha's Forehead\"}]},\n" +
            " {\"sequence\":18,\"title\":\"The beautiful dolphin is full of scars Eaten whole by killer whales\",\"answers\":[{\"result\":\"A:name\"},{\"result\":\"B:liuha\"}, {\"result\":\"C:Integral\"},{\"result\":\"D:Golden Buddha Amount\"}]},\n" +
            " {\"sequence\":19, \"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed whole by a killer whale\",\"answers\":[{\"result\":\"A:name\"},{\"result \":\"B: Six Ha\"},{\"result\":\"C:Points\"},{\"result\":\"D:Golden Buddha's Amount\"}]},\ n" +
            " {\"sequence\":20,\"title\":\"The beautiful dolphin is scarred and suspected to have been swallowed whole by a killer whale\",\"answers\":[{\"result\":\"A :Name\"},{\"result\":\"B:Liuha\"},{\"result\":\"C:Points\"},{\"result\":\"D: Jinfo'e \"}]}\n" +
            " ]\n" +
            "}" ;

1 - json-20070829.jar (I don't know where it came from the Internet), execution time 11s-12s
public void testParseJsonWith_org_json_jar() throws Exception {
        long start = System.currentTimeMillis();

        for (int loop = 0; loop < 10000; loop++) {

            ExaminationDTO examinationDTO = new ExaminationDTO();
            JSONObject jExamination = new JSONObject(json);

            //handle examination info
            int id = jExamination.optInt("id");
            String title = jExamination.optString("title");
            examinationDTO.setId(id);
            examinationDTO.setTitle(title);

            //hanle question
            JSONArray jQuestions = jExamination.getJSONArray("questions");
            for (int i = 0; i < jQuestions.length(); i++) {
                JSONObject jQuestion = jQuestions.getJSONObject(i);
                int sequence = jQuestion.optInt("sequence");
                String questionTitle = jQuestion.optString("title");
                QuestionDTO questionDTO = new QuestionDTO();
                questionDTO.setSequence(sequence);
                questionDTO.setTitle(questionTitle);

                //handle answer
                JSONArray jAnswers = jQuestion.getJSONArray("answers");
                for (int j = 0; j < jAnswers.length(); j++) {
                    JSONObject jAnswer = jAnswers.getJSONObject(j);
                    String result = jAnswer.optString("result");
                    String[] tokens = StringUtils.delimitedListToStringArray(result, ":");
                    AnswerDTO answerDTO = new AnswerDTO();
                    answerDTO.setSequence(tokens[0]);
                    answerDTO.setResult(tokens[1]);
                    questionDTO.addAnswer(answerDTO);
                }

                examinationDTO.addQuestion(questionDTO);
            }
        }

        long end = System.currentTimeMillis();
        long during = end - start;
        System.out.println("10000 times taken time for org.json is " + during);
    }

2 - alibaba fastjson-1.1.34.android.jar,执行时间8s-9s
public void testParseJsonWith_fast_json_jar() throws Exception {
        long start = System.currentTimeMillis();

        for (int loop = 0; loop < 10000; loop++) {

            ExaminationDTO examinationDTO = new ExaminationDTO();
            JSONObject jExamination = JSON.parseObject(json);

            //handle examination info
            int id = jExamination.getInteger("id");
            String title = jExamination.getString("title");
            examinationDTO.setId(id);
            examinationDTO.setTitle(title);

            //hanle question
            JSONArray jQuestions = jExamination.getJSONArray("questions");
            for (int i = 0; i < jQuestions.size(); i++) {
                JSONObject jQuestion = jQuestions.getJSONObject(i);
                int sequence = jQuestion.getInteger("sequence");
                String questionTitle = jQuestion.getString("title");
                QuestionDTO questionDTO = new QuestionDTO();
                questionDTO.setSequence(sequence);
                questionDTO.setTitle(questionTitle);

                //handle answer
                JSONArray jAnswers = jQuestion.getJSONArray("answers");
                for (int j = 0; j < jAnswers.size(); j++) {
                    JSONObject jAnswer = jAnswers.getJSONObject(j);
                    String result = jAnswer.getString("result");
                    String[] tokens = StringUtils.delimitedListToStringArray(result, ":");
                    AnswerDTO answerDTO = new AnswerDTO();
                    answerDTO.setSequence(tokens[0]);
                    answerDTO.setResult(tokens[1]);
                    questionDTO.addAnswer(answerDTO);
                }

                examinationDTO.addQuestion(questionDTO);
            }
        }

        long end = System.currentTimeMillis();
        long during = end - start;
        System.out.println("10000 times taken time for alibaba fast json is " + during);
    }

3 - jackson-all-1.9.11.jar, 执行时间15s-17s
public void testParseJsonWith_jackson_jar() throws Exception {
        long start = System.currentTimeMillis();

        for (int loop = 0; loop < 10000; loop++) {
            ObjectMapper objectMapper = new ObjectMapper();

            ExaminationDTO examinationDTO = new ExaminationDTO();
            JsonNode jExamination = objectMapper.readTree(json);

            //handle examination info
            int id = jExamination.get("id").getIntValue();
            String title = jExamination.get("title").getTextValue();
            examinationDTO.setId(id);
            examinationDTO.setTitle(title);

            //hanle question
            ArrayNode jQuestions = (ArrayNode) jExamination.get("questions");
            for (int i = 0; i < jQuestions.size(); i++) {
                JsonNode jQuestion = jQuestions.get(i);
                int sequence = jQuestion.get("sequence").getIntValue();
                String questionTitle = jQuestion.get("title").getTextValue();
                QuestionDTO questionDTO = new QuestionDTO();
                questionDTO.setSequence(sequence);
                questionDTO.setTitle(questionTitle);

                //handle answer
                ArrayNode jAnswers = (ArrayNode) jQuestion.get("answers");
                for (int j = 0; j < jAnswers.size(); j++) {
                    JsonNode jAnswer = jAnswers.get(j);
                    String result = jAnswer.get("result").getTextValue();
                    String[] tokens = StringUtils.delimitedListToStringArray(result, ":");
                    AnswerDTO answerDTO = new AnswerDTO();
                    answerDTO.setSequence(tokens[0]);
                    answerDTO.setResult(tokens[1]);
                    questionDTO.addAnswer(answerDTO);
                }

                examinationDTO.addQuestion(questionDTO);
            }
        }

        long end = System.currentTimeMillis();
        long during = end - start;
        System.out.println("10000 times taken time for jackson is " + during);

    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326593992&siteId=291194637