# JsonParser2.0 **Repository Path**: warrior__night/JsonParser2.0 ## Basic Information - **Project Name**: JsonParser2.0 - **Description**: 手写Json解析器,使用了生成式理论来分析各个部分,而且完成了序列化的功能。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-05-18 - **Last Updated**: 2021-05-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 项目:Json解析器2.0 这是我的Json解析器2.0版本,使用了编译原理的一些原理, 通过自定义的生成式来解析Json字符串为自定义的对象,并且完成了将对象转换为Json字符串的功能(即反序列化) 测试1: ```json # first.json { "name": "mike", "age": 10, "height": 168.5, "friends": ["John", "Sara", "CodeReaper"], "family": { "mom": "Holom", "dad": "Geo" }, "message": "his old name is \"haha\"" } ``` 代码 ```java String filePath = "testFile/first.json"; String codeText = readJsonText(filePath); Lexer lexer = new Lexer(codeText); Parser parser = new Parser(lexer); ObjectVal dict = (ObjectVal) parser.parse(); JsonPrinter jsonPrinter = new JsonPrinter(); jsonPrinter.printValue(dict); System.out.println("\nfriends: "); ArrayVal friends = (ArrayVal) dict.getValue("friends"); for (JsonVal jsonVal : friends.getValueList()) { System.out.println(jsonVal); } System.out.println("\nfamily:"); ObjectVal family = (ObjectVal) dict.getValue("family"); jsonPrinter.printValue(family); ``` 运行输出: ``` { "name": "mike", "family": { "dad": "Geo", "mom": "Holom" }, "message": "his old name is \"haha\"", "age": 10.0, "friends": [ "John", "Sara", "CodeReaper" ], "height": 168.5 } friends: "John" "Sara" "CodeReaper" family: { "dad": "Geo", "mom": "Holom" } ``` 测试2:测试反序列化 ```java ObjectVal personInfo = new ObjectVal(); personInfo.putKeyVal("name","codeReaper"); personInfo.putKeyVal("age",new NumVal(20)); ArrayVal hobbies = new ArrayVal(); hobbies.addValue("coding"); hobbies.addValue("lol"); hobbies.addValue("playing pingpong"); personInfo.putKeyVal("hobbies", hobbies); JsonPrinter jsonPrinter = new JsonPrinter(); jsonPrinter.printValue(personInfo); System.out.println("--------"); String serializedStr = jsonPrinter.getSerializedStr(personInfo); System.out.println(serializedStr); ``` 运行输出: ```json { "hobbies": [ "coding", "lol", "playing pingpong" ], "name": "codeReaper", "age": 20.0 } ``` 测试3:数组类型json ```json # array.json [ { "id": 15, "article_tags": [ { "id": 9, "tag_name": "日常" } ], "create_time": "2021-04-26T04:30:20.161820Z", "update_time": "2021-04-26T15:06:18.641641Z", "article_title": "第一篇博客", "article_abstract": "第一篇博客", "article_content": "# 标题\n## 子标题\n- 1\n- 2\n- 3\n- 4\n1. java\n2. python\n3. js\n```java\nclass Person{\n void sayHello(){\n System.out.println(\"Hello\");\n }\n}\n```\n![image.png](http://127.0.0.1:8000/media/image_c3uq8Wk.png)" }, { "id": 16, "article_tags": [ { "id": 9, "tag_name": "日常" } ], "create_time": "2021-04-26T10:27:19.423945Z", "update_time": "2021-04-26T10:27:19.423945Z", "article_title": "编辑", "article_abstract": "编辑", "article_content": "编辑" }, { "id": 17, "article_tags": [ { "id": 1, "tag_name": "c++基础内容" } ], "create_time": "2021-04-26T10:30:47.436020Z", "update_time": "2021-04-26T10:30:47.436020Z", "article_title": "test", "article_abstract": "test", "article_content": "test" }, ... ] ``` 代码: ```java Parser parser = ParserBuilder.produceParser("testFile/array.json"); ArrayVal articles = (ArrayVal) parser.parse(); JsonPrinter jsonPrinter = new JsonPrinter(); jsonPrinter.printValue(articles); ``` 运行输出: ```json [ { "update_time": "2021-04-26T15:06:18.641641Z", "article_title": "第一篇博客", "create_time": "2021-04-26T04:30:20.161820Z", "article_abstract": "第一篇博客", "article_content": "# 标题\n## 子标题\n- 1\n- 2\n- 3\n- 4\n1. java\n2. python\n3. js\n```java\nclass Person{\n void sayHello(){\n System.out.println(\"Hello\");\n }\n}\n```\n![image.png](http://127.0.0.1:8000/media/image_c3uq8Wk.png)", "id": 15.0, "article_tags": [ { "tag_name": "日常", "id": 9.0 } ] }, { "update_time": "2021-04-26T10:27:19.423945Z", "article_title": "编辑", "create_time": "2021-04-26T10:27:19.423945Z", "article_abstract": "编辑", "article_content": "编辑", "id": 16.0, "article_tags": [ { "tag_name": "日常", "id": 9.0 } ] }, { "update_time": "2021-04-26T10:30:47.436020Z", "article_title": "test", "create_time": "2021-04-26T10:30:47.436020Z", "article_abstract": "test", "article_content": "test", "id": 17.0, "article_tags": [ { "tag_name": "c++基础内容", "id": 1.0 } ] }, ... ] ``` 测试4:取值 ```json # tags.json [ { "id": 1, "related_article_num": 1, "create_time": "2021-04-20T14:42:47.295950Z", "update_time": "2021-04-20T14:42:47.295950Z", "tag_name": "c++基础内容" }, { "id": 2, "related_article_num": 0, "create_time": "2021-04-20T14:43:00.665498Z", "update_time": "2021-04-20T14:43:00.665498Z", "tag_name": "设计模式" }, { "id": 3, "related_article_num": 0, "create_time": "2021-04-20T14:43:39.640646Z", "update_time": "2021-04-20T14:43:39.640646Z", "tag_name": "计算机系统2" }, ... ] ``` 代码: ```java Parser parser = ParserBuilder.produceParser("testFile/tags.json"); ArrayVal articleArray = (ArrayVal) parser.parse(); JsonPrinter jsonPrinter = new JsonPrinter(); jsonPrinter.printValue(articleArray); for (JsonVal val : articleArray.getValueList()) { ObjectVal obj = (ObjectVal)val; String tag_name = ((StringVal)obj.getValue("tag_name")).value; System.out.println(tag_name); } ``` 运行输出: ``` [ { "update_time": "2021-04-20T14:42:47.295950Z", "create_time": "2021-04-20T14:42:47.295950Z", "tag_name": "c++基础内容", "related_article_num": 1.0, "id": 1.0 }, { "update_time": "2021-04-20T14:43:00.665498Z", "create_time": "2021-04-20T14:43:00.665498Z", "tag_name": "设计模式", "related_article_num": 0.0, "id": 2.0 }, { "update_time": "2021-04-20T14:43:39.640646Z", "create_time": "2021-04-20T14:43:39.640646Z", "tag_name": "计算机系统2", "related_article_num": 0.0, "id": 3.0 }, ... ] c++基础内容 设计模式 计算机系统2 数据结构 555 心情 日常 新标签 哈哈 新 ```