Skip to content

Commit a9bd54e

Browse files
committed
Update Code
Update Code and Readme.md file also added change log file.
1 parent a930dbd commit a9bd54e

10 files changed

+242
-52
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
.mtj.tmp/
1212

1313
# Package Files #
14-
*.jar
1514
*.war
1615
*.nar
1716
*.ear

ChangeLog

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--Change Log--
2+
18/05/2018
3+
Version = 1.0
4+
5+
- First release
6+
7+
24/05/2018
8+
Version = 1.1
9+
10+
- Json Array Index now change 0 to 1 so, your first array element index is 1 not 0

README.md

+106
Original file line numberDiff line numberDiff line change
@@ -1 +1,107 @@
11
# JsonParser
2+
JsonParser is simple JSON parsing library, you can parse JSONObject or JSONArray using path and key. Simply you just need to pass the name of the object or object key and index number of the array item in your path.
3+
4+
## Installation
5+
6+
This JsonParser JAR add in the `lib` folder then after set in build path. Download JAR form this link [JsonParser_DLab_1.1.jar](Release/JsonParser_DLab_1.1.jar&raw=true). For, the more information refer this [`JsonParser`](src/main/) sample project.
7+
8+
## Path
9+
Path is combination of the object key and index number of the array item.
10+
11+
`Dpath=[menu/items/12/label]`
12+
13+
here, `menu` `items` `label` is your object key and 12 is array item index number now, the question arises how to parse using this path so let's little talk on it. When you give path to the library, it is converted path to token then after, first check is JSONObject or JSONArray if object then token must have key value(if not found then it is throwing exception and process break) then after this process recursively doing till your path's all token finished, once token finished then process break and return the result in the Object format.
14+
15+
> Note : Array index is starting from the 1 so, your array first item index is 1.
16+
17+
### Example:
18+
19+
You can refer more example [here](src/main/JsonParserTest.java),
20+
21+
```json
22+
{"menu": {
23+
"header": "SVG Viewer",
24+
"items": [
25+
{"id": "Open"},
26+
{"id": "OpenNew", "label": "Open New"},
27+
null,
28+
{"id": "ZoomIn", "label": "Zoom In"},
29+
{"id": "ZoomOut", "label": "Zoom Out"},
30+
{"id": "OriginalView", "label": "Original View"},
31+
null,
32+
{"id": "Quality"},
33+
{"id": "Pause"},
34+
{"id": "Mute"},
35+
null,
36+
{"id": "Find", "label": "Find..."}
37+
]
38+
}}
39+
```
40+
41+
## Key
42+
Key is a unique element of the object so, duplication of the key is not allowed in the object but whole JSONObject or JSONArray may be have same name key.
43+
44+
`Dkey=[value]`
45+
46+
here, `value` is your key and more then one time if it exists in the whole object then during this recursively process, if key found then notify the user on `onValueFound(String key, Object value)`.
47+
48+
### Example:
49+
50+
You can refer more example [here](src/main/JsonParserTest.java),
51+
52+
```json
53+
{"menu": {
54+
"id": "file",
55+
"value": "File",
56+
"popup": {
57+
"menuitem": [
58+
{"value": "New", "onclick": "CreateNewDoc()"},
59+
{"value": "Open", "onclick": "OpenDoc()"},
60+
{"value": "Close", "onclick": "CloseDoc()"}
61+
]
62+
}
63+
}}
64+
```
65+
66+
```java
67+
new JsonParser().addObserver(new OnValueObserver() {
68+
@Override
69+
public void onValueFound(String key, Object value) {
70+
System.out.println(String.valueOf("Find By Key : " + value));
71+
}
72+
73+
}).inFind(GetJson.strJson_4, "Dkey=[value]");
74+
75+
// Output :-
76+
// Find By Key : New
77+
// Find By Key : Open
78+
// Find By Key : Close
79+
```
80+
81+
***
82+
83+
### End User
84+
85+
* QA Engineer, Developer and who not like to do manually parsing of the JSONObject and JSONArray.
86+
* How I get the idea to develope this type of library, when I am helping to the one of the my QA friend in his automation process that time, I observed that he man facing trouble on the parsing of the JSONObject then I decided to develop this type of library, which have structure as like selenium Xpath.
87+
88+
89+
### Current issues
90+
91+
* Integer key value of object's is not supported
92+
* Only JSONObject or JSONArray as string passing in the arguments
93+
* multiple path is not supported
94+
* array of key or array or path not supported as arguments
95+
* always need listener as a notifier
96+
97+
### Contributing
98+
99+
Contributions are welcome! If you find a bug please report it and if you want add new feature then please suggest to me. If you want to contribute code please file an issue and create a branch off of the current dev branch and file a pull request.
100+
101+
### About me
102+
103+
Kishan Donga ([@ikishan92](https://twitter.com/ikishan92))
104+
105+
### License
106+
107+
`JsonParser` is released under the [Apache License 2.0](LICENSE).

Release/JsonParser_DLab_1.0.jar

86.2 KB
Binary file not shown.

Release/JsonParser_DLab_1.1.jar

83.9 KB
Binary file not shown.

lib/java-json.jar

82.7 KB
Binary file not shown.

manifest

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
--JsonParser--
2-
FileVersion = 1.0
3-
CodeVersion = 1.0
2+
Version = 1.1
43
JavaVersion = 1.8
54

65
--Developer Info--
76
Kishan Donga
87
Software Engineer
98
109
contactNo = +919712598499
11-
12-
--Change Log--
13-
18/05/2018
14-
Version = 1.0
15-
- First release

src/dlab/JsonParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private void inFindByPath(String jsonString, String[] token) throws JSONExceptio
6262

6363
for(String vt : token){
6464
if(isInt(vt)){
65-
object = inFindByPath((JSONArray)object, Integer.parseInt(vt));
65+
object = inFindByPath((JSONArray)object, (Integer.parseInt(vt) - 1));
6666
}else{
6767
object = inFindByPath((JSONObject)object, vt);
6868
}

src/main/GetJson.java

+80-39
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,93 @@
22

33
public class GetJson {
44

5+
public static String strJson_4 = "{\"menu\": {\r\n" +
6+
" \"id\": \"file\",\r\n" +
7+
" \"value\": \"File\",\r\n" +
8+
" \"popup\": {\r\n" +
9+
" \"menuitem\": [\r\n" +
10+
" {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\r\n" +
11+
" {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\r\n" +
12+
" {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\r\n" +
13+
" ]\r\n" +
14+
" }\r\n" +
15+
"}}";
16+
17+
public static String strJson_3 = "{\"menu\": {\r\n" +
18+
" \"header\": \"SVG Viewer\",\r\n" +
19+
" \"items\": [\r\n" +
20+
" {\"id\": \"Open\"},\r\n" +
21+
" {\"id\": \"OpenNew\", \"label\": \"Open New\"},\r\n" +
22+
" null,\r\n" +
23+
" {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},\r\n" +
24+
" {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},\r\n" +
25+
" {\"id\": \"OriginalView\", \"label\": \"Original View\"},\r\n" +
26+
" null,\r\n" +
27+
" {\"id\": \"Quality\"},\r\n" +
28+
" {\"id\": \"Pause\"},\r\n" +
29+
" {\"id\": \"Mute\"},\r\n" +
30+
" null,\r\n" +
31+
" {\"id\": \"Find\", \"label\": \"Find...\"},\r\n" +
32+
" {\"id\": \"FindAgain\", \"label\": \"Find Again\"},\r\n" +
33+
" {\"id\": \"Copy\"},\r\n" +
34+
" {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},\r\n" +
35+
" {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},\r\n" +
36+
" {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},\r\n" +
37+
" {\"id\": \"ViewSource\", \"label\": \"View Source\"},\r\n" +
38+
" {\"id\": \"SaveAs\", \"label\": \"Save As\"},\r\n" +
39+
" null,\r\n" +
40+
" {\"id\": \"Help\"},\r\n" +
41+
" {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}\r\n" +
42+
" ]\r\n" +
43+
"}}";
44+
545
public static String strJson_2 = "{\r\n" +
646
" \"name\":\"John\",\r\n" +
747
" \"age\":30,\r\n" +
848
" \"cars\": {\r\n" +
949
" \"car1\":\"Ford\",\r\n" +
1050
" \"car2\":\"BMW\",\r\n" +
1151
" \"car3\":\"Fiat\"\r\n" +
12-
" }\r\n" +
52+
" },\r\n" +
53+
" \"No\": [\"1\",\"2\",\"3\"]\r\n" +
1354
" }";
1455

15-
public static String strJson_1 = "{\n" +
16-
"\t\"problems\": [{\n" +
17-
"\t\t\"Diabetes\": [{\n" +
18-
"\t\t\t\"medications\": [{\n" +
19-
"\t\t\t\t\"medicationsClasses\": [{\n" +
20-
"\t\t\t\t\t\"className\": [{\n" +
21-
"\t\t\t\t\t\t\"associatedDrug\": [{\n" +
22-
"\t\t\t\t\t\t\t\"name\": \"asprin\",\n" +
23-
"\t\t\t\t\t\t\t\"dose\": \"\",\n" +
24-
"\t\t\t\t\t\t\t\"strength\": \"500 mg\"\n" +
25-
"\t\t\t\t\t\t}],\n" +
26-
"\t\t\t\t\t\t\"associatedDrug#2\": [{\n" +
27-
"\t\t\t\t\t\t\t\"name\": \"somethingElse\",\n" +
28-
"\t\t\t\t\t\t\t\"dose\": \"\",\n" +
29-
"\t\t\t\t\t\t\t\"strength\": \"500 mg\"\n" +
30-
"\t\t\t\t\t\t}]\n" +
31-
"\t\t\t\t\t}],\n" +
32-
"\t\t\t\t\t\"className2\": [{\n" +
33-
"\t\t\t\t\t\t\"associatedDrug\": [{\n" +
34-
"\t\t\t\t\t\t\t\"name\": \"asprin\",\n" +
35-
"\t\t\t\t\t\t\t\"dose\": \"\",\n" +
36-
"\t\t\t\t\t\t\t\"strength\": \"500 mg\"\n" +
37-
"\t\t\t\t\t\t}],\n" +
38-
"\t\t\t\t\t\t\"associatedDrug#2\": [{\n" +
39-
"\t\t\t\t\t\t\t\"name\": \"somethingElse\",\n" +
40-
"\t\t\t\t\t\t\t\"dose\": \"\",\n" +
41-
"\t\t\t\t\t\t\t\"strength\": \"500 mg\"\n" +
42-
"\t\t\t\t\t\t}]\n" +
43-
"\t\t\t\t\t}]\n" +
44-
"\t\t\t\t}]\n" +
45-
"\t\t\t}],\n" +
46-
"\t\t\t\"labs\": [{\n" +
47-
"\t\t\t\t\"missing_field\": \"missing_value\"\n" +
48-
"\t\t\t}]\n" +
49-
"\t\t}],\n" +
50-
"\t\t\"Asthma\": [{}]\n" +
51-
"\t}]\n" +
52-
"}";
56+
public static String strJson_1 = "{\r\n" +
57+
"\t\"problems\": [{\r\n" +
58+
"\t\t\"Diabetes\": [{\r\n" +
59+
"\t\t\t\"medications\": [{\r\n" +
60+
"\t\t\t\t\"medicationsClasses\": [{\r\n" +
61+
"\t\t\t\t\t\"className\": [{\r\n" +
62+
"\t\t\t\t\t\t\"associatedDrug\": [{\r\n" +
63+
"\t\t\t\t\t\t\t\"name\": \"asprin\",\r\n" +
64+
"\t\t\t\t\t\t\t\"dose\": \"\",\r\n" +
65+
"\t\t\t\t\t\t\t\"strength\": \"500 mg\"\r\n" +
66+
"\t\t\t\t\t\t}],\r\n" +
67+
"\t\t\t\t\t\t\"associatedDrug#2\": [{\r\n" +
68+
"\t\t\t\t\t\t\t\"name\": \"somethingElse\",\r\n" +
69+
"\t\t\t\t\t\t\t\"dose\": \"\",\r\n" +
70+
"\t\t\t\t\t\t\t\"strength\": \"500 mg\"\r\n" +
71+
"\t\t\t\t\t\t}]\r\n" +
72+
"\t\t\t\t\t}],\r\n" +
73+
"\t\t\t\t\t\"className2\": [{\r\n" +
74+
"\t\t\t\t\t\t\"associatedDrug\": [{\r\n" +
75+
"\t\t\t\t\t\t\t\"name\": \"asprin\",\r\n" +
76+
"\t\t\t\t\t\t\t\"dose\": \"\",\r\n" +
77+
"\t\t\t\t\t\t\t\"strength\": \"500 mg\"\r\n" +
78+
"\t\t\t\t\t\t}],\r\n" +
79+
"\t\t\t\t\t\t\"associatedDrug#2\": [{\r\n" +
80+
"\t\t\t\t\t\t\t\"name\": \"somethingElse\",\r\n" +
81+
"\t\t\t\t\t\t\t\"dose\": \"\",\r\n" +
82+
"\t\t\t\t\t\t\t\"strength\": \"500 mg\"\r\n" +
83+
"\t\t\t\t\t\t}]\r\n" +
84+
"\t\t\t\t\t}]\r\n" +
85+
"\t\t\t\t}]\r\n" +
86+
"\t\t\t}],\r\n" +
87+
"\t\t\t\"labs\": [{\r\n" +
88+
"\t\t\t\t\"missing_field\": \"missing_value\"\r\n" +
89+
"\t\t\t}]\r\n" +
90+
"\t\t}],\r\n" +
91+
"\t\t\"Asthma\": [{}]\r\n" +
92+
"\t}]\r\n" +
93+
"}";
5394
}

src/main/JsonParserTest.java

+44-4
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,52 @@ public static void main(String[] args) {
1414
new JsonParser().addObserver(new OnValueObserver() {
1515
@Override
1616
public void onValueFound(String key, Object value) {
17-
System.out.println(String.valueOf("Find By Path : " + value));
17+
System.out.println(String.valueOf("Find By Key : " + value));
18+
}
19+
20+
}).inFind(GetJson.strJson_4, "Dkey=[value]");
21+
22+
// Output :-
23+
// Find By Key : New
24+
// Find By Key : Open
25+
// Find By Key : Close
26+
27+
new JsonParser().addObserver(new OnValueObserver() {
28+
@Override
29+
public void onValueFound(String key, Object value) {
30+
System.out.println(String.valueOf("Find By Path : " + value.toString()));
31+
}
32+
33+
}).inFind(GetJson.strJson_3, "Dpath=[menu/items/12/label]");
34+
35+
// Output :- Find By Path : Find...
36+
37+
new JsonParser().addObserver(new OnValueObserver() {
38+
@Override
39+
public void onValueFound(String key, Object value) {
40+
System.out.println(String.valueOf("Find By Path : " + value.toString()));
1841
}
1942

20-
}).inFind(GetJson.strJson_2, "Dpath=[cars/car1]");
43+
}).inFind(GetJson.strJson_2, "Dpath=[No/1]");
44+
45+
// Output :- Find By Path : 1
2146

2247
new JsonParser().addObserver(new OnValueObserver() {
2348
@Override
2449
public void onValueFound(String key, Object value) {
25-
System.out.println(String.valueOf("Find By Path : " + value));
50+
51+
new JsonParser().addObserver(new OnValueObserver() {
52+
@Override
53+
public void onValueFound(String key, Object value) {
54+
System.out.println(String.valueOf("Find By Path : " + value.toString()));
55+
}
56+
57+
}).inFind(value.toString(), "Dpath=[medicationsClasses/1/className/1/associatedDrug/1/name]");
2658
}
2759

28-
}).inFind(GetJson.strJson_1, "Dpath=[problems/0/Diabetes/0/medications/0/medicationsClasses/0/className/0/associatedDrug/0/name]");
60+
}).inFind(GetJson.strJson_1, "Dpath=[problems/1/Diabetes/1/medications/1/]");
61+
62+
// Output :- Find By Path : asprin
2963

3064
new JsonParser().addObserver(new OnValueObserver() {
3165
@Override
@@ -34,5 +68,11 @@ public void onValueFound(String key, Object value) {
3468
}
3569

3670
}).inFind(GetJson.strJson_1, "Dkey=[name]");
71+
72+
// Output :-
73+
// Find By Key : asprin
74+
// Find By Key : somethingElse
75+
// Find By Key : asprin
76+
// Find By Key : somethingElse
3777
}
3878
}

0 commit comments

Comments
 (0)