Skip to content

Commit

Permalink
* more informative README.md
Browse files Browse the repository at this point in the history
+ very dirty example.json
  • Loading branch information
magicprinc committed Dec 2, 2021
1 parent bf7dad2 commit 58dd798
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 55 deletions.
101 changes: 60 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,35 @@ No extra dependencies!

[**example.json**](src/test/resources/example.json)
```json
/*
I am here: src\test\resources/example.json **/
{projects: [
{
project_name = 'Google Gson',
'url': "https://github.com/google/gson",
"rating": 4.956,
"contributors": [{
first_name: Jesse, 'last_name': 'Wilson',
"home_page": "https://medium.com/@swankjesse"
}]
},{
"project_name" => jsoup,
"url": "https://jsoup.org",
"rating": 5e10,
"contributors": [
{
"first_name" = "Jonathan", "last_name" => "Hedley",
"home_page": "https://jhy.io"
},{
"first_name": "Andrej", "last_name": "Fink"; "home_page": "https://github.com/magicprinc"
}]}]}
{
project_name = 'Google Gson',
'url': "https://github.com/google/gson",
"rating": 4.956,
"contributors": [{
first_name: Jesse,,,, ' last name ': "'Wilson ' ",
"home_page": "https://medium.com/@swankjesse"
}]
},{
"project_name" => jsoup

"url": "https://jsoup.org";,;,;

"rating"=5e10,
'contributors': [
{
"first_name" = "Jonathan", "last_name" => "Hedley",
"home_page": "https://jhy.io"
},{///2nd OBJ in Array
"first_name": "Andrej", "last_name": "Fink";;; " home
page ": " https://github.com/
magicprinc ";
}
]
}
]}
```

**java**
Expand All @@ -46,34 +55,43 @@ assertEquals("<arr><val class=\"bool\">true</val><val class=\"bool\">true</val><

[**internal XML tree 1 (with class metadata) **](src/test/resources/example1.xml)
```xml
<!--
I am here: src\test\resources/example.json *-->
<obj>
<arr id="projects">
<obj>
<val id="project_name" class="apos quoted str"> Google Gson </val>
<val id="url" class="quot quoted str"> https://github.com/google/gson </val>
<val id="rating" class="unquoted num"> 4.956 </val>

<arr id="contributors">
<obj>
<val id="first_name" class="unquoted str"> Jesse </val>
<val id="last_name" class="apos quoted str"> Wilson </val>
<val id=" last name " class="quot quoted str"> 'Wilson ' </val>
<val id="home_page" class="quot quoted str"> https://medium.com/@swankjesse </val>
</obj>
</arr>
</obj>

<obj>
<val id="project_name" class="unquoted str"> jsoup </val>
<val id="url" class="quot quoted str"> https://jsoup.org </val>
<val id="rating" class="unquoted num"> 5e10 </val>

<arr id="contributors">
<obj>
<val id="first_name" class="quot quoted str"> Jonathan </val>
<val id="last_name" class="quot quoted str"> Hedley </val>
<val id="home_page" class="quot quoted str"> https://jhy.io </val>
</obj>

<obj>
<!--/2nd OBJ in Array-->
<val id="first_name" class="quot quoted str"> Andrej </val>
<val id="last_name" class="quot quoted str"> Fink </val>
<val id="home_page" class="quot quoted str"> https://github.com/magicprinc </val>
<val id=" home
page " class="quot quoted str"> https://github.com/
magicprinc </val>
</obj>
</arr>
</obj>
Expand All @@ -88,39 +106,40 @@ Document doc = Jsoup.parse(exampleJson, "UTF-8", "", JsonTreeBuilder.jsonParser(

[**internal XML tree 2 (no metadata) **](src/test/resources/example2.xml)
```xml
<obj>
<arr id="projects">
<!--
I am here: src\test\resources/example.json *-->
<obj><arr id="projects">
<obj>
<val id="project_name"> Google Gson </val>
<val id="url"> https://github.com/google/gson </val>
<val id="rating"> 4.956 </val>
<val id="project_name">Google Gson</val>
<val id="url">https://github.com/google/gson</val>
<val id="rating">4.956</val>
<arr id="contributors">
<obj>
<val id="first_name"> Jesse </val>
<val id="last_name"> Wilson </val>
<val id="home_page"> https://medium.com/@swankjesse </val>
<val id="first_name">Jesse</val>
<val id=" last name ">'Wilson ' </val>
<val id="home_page">https://medium.com/@swankjesse</val>
</obj>
</arr>
</obj>

<obj>
<val id="project_name"> jsoup </val>
<val id="url"> https://jsoup.org </val>
<val id="rating"> 5e10 </val>
<val id="project_name">jsoup</val>
<val id="url">https://jsoup.org</val>
<val id="rating">5e10</val>
<arr id="contributors">
<obj>
<val id="first_name"> Jonathan </val>
<val id="last_name"> Hedley </val>
<val id="home_page"> https://jhy.io </val>
<val id="first_name">Jonathan</val>
<val id="last_name">Hedley</val><val id="home_page">https://jhy.io</val>
</obj>
<obj>
<val id="first_name"> Andrej </val>
<val id="last_name"> Fink </val>
<val id="home_page"> https://github.com/magicprinc </val>
<obj><!--/2nd OBJ in Array-->
<val id="first_name">Andrej</val>
<val id="last_name">Fink</val><val id=" home
page "> https://github.com/
magicprinc </val>
</obj>
</arr>
</obj>
</arr>
</obj>
</arr></obj>
```

More examples are available in 135 unit-tests in [org.jsoup.parser.JsonTreeBuilderTest](/src/test/java/org/jsoup/parser/JsonTreeBuilderTest.java)
10 changes: 6 additions & 4 deletions src/test/java/org/jsoup/parser/JsonTreeBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1702,21 +1702,23 @@ public void testJavaDocExample () throws Exception {
Document doc = Jsoup.parse(getClass().getResourceAsStream("/example.json"), "UTF-8", "", jsonParser());
System.out.println(repeat('#', 100)+doc.html()+repeat('#', 100));
//doc.outputSettings().prettyPrint(false);
assertEquals("<obj><arr id=\"projects\">" +
assertEquals(
"<!--\nI am here: src\\test\\resources/example.json *--><obj><arr id=\"projects\">" +
"<obj><val id=\"project_name\" class=\"apos quoted str\">Google Gson</val>" +
"<val id=\"url\" class=\"quot quoted str\">https://github.com/google/gson</val>" +
"<val id=\"rating\" class=\"unquoted num\">4.956</val>"+
"<arr id=\"contributors\"><obj><val id=\"first_name\" class=\"unquoted str\">Jesse</val>" +
"<val id=\"last_name\" class=\"apos quoted str\">Wilson</val>" +
"<val id=\" last name \" class=\"quot quoted str\">'Wilson ' </val>" +
"<val id=\"home_page\" class=\"quot quoted str\">https://medium.com/@swankjesse</val></obj></arr></obj>" +
"<obj><val id=\"project_name\" class=\"unquoted str\">jsoup</val>" +
"<val id=\"url\" class=\"quot quoted str\">https://jsoup.org</val>" +
"<val id=\"rating\" class=\"unquoted num\">5e10</val>"+
"<arr id=\"contributors\"><obj><val id=\"first_name\" class=\"quot quoted str\">Jonathan</val>" +
"<val id=\"last_name\" class=\"quot quoted str\">Hedley</val>" +
"<val id=\"home_page\" class=\"quot quoted str\">https://jhy.io</val></obj>" +
"<obj><val id=\"first_name\" class=\"quot quoted str\">Andrej</val><val id=\"last_name\" class=\"quot quoted str\">Fink</val>" +
"<val id=\"home_page\" class=\"quot quoted str\">https://github.com/magicprinc</val></obj></arr></obj></arr></obj>", doc.html());
"<obj><!--/2nd OBJ in Array-->"+
"<val id=\"first_name\" class=\"quot quoted str\">Andrej</val><val id=\"last_name\" class=\"quot quoted str\">Fink</val>" +
"<val id=\" home\n page \" class=\"quot quoted str\"> https://github.com/\n magicprinc </val></obj></arr></obj></arr></obj>",doc.html());
assertEquals("Fink", doc.select("#contributors obj:eq(1) #last_name").text());
assertEquals("jsoup", doc.select("#projects #project_name.str.unquoted").text());
assert "Fink".equals(doc.select("#contributors obj:eq(1) #last_name").text());
Expand Down
14 changes: 9 additions & 5 deletions src/test/resources/example.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
/*
I am here: src\test\resources/example.json **/
{projects: [
{
project_name = 'Google Gson',
'url': "https://github.com/google/gson",
"rating": 4.956,
"contributors": [{
first_name: Jesse,,,, 'last_name': 'Wilson',
first_name: Jesse,,,, ' last name ': "'Wilson ' ",
"home_page": "https://medium.com/@swankjesse"
}]
},{
"project_name" => jsoup

"url": "https://jsoup.org";,;,;

"rating": 5e10,
"contributors": [
"rating"=5e10,
'contributors': [
{
"first_name" = "Jonathan", "last_name" => "Hedley",
"home_page": "https://jhy.io"
},{
"first_name": "Andrej", "last_name": "Fink";;; "home_page": "https://github.com/magicprinc"
},{///2nd OBJ in Array
"first_name": "Andrej", "last_name": "Fink";;; " home
page ": " https://github.com/
magicprinc ";
}
]
}
Expand Down
12 changes: 8 additions & 4 deletions src/test/resources/example1.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!--
I am here: src\test\resources/example.json *-->
<obj>
<arr id="projects">
<obj>
Expand All @@ -15,8 +17,8 @@
<val id="first_name" class="unquoted str">
Jesse
</val>
<val id="last_name" class="apos quoted str">
Wilson
<val id=" last name " class="quot quoted str">
'Wilson '
</val>
<val id="home_page" class="quot quoted str">
https://medium.com/@swankjesse
Expand Down Expand Up @@ -47,14 +49,16 @@
</val>
</obj>
<obj>
<!--/2nd OBJ in Array-->
<val id="first_name" class="quot quoted str">
Andrej
</val>
<val id="last_name" class="quot quoted str">
Fink
</val>
<val id="home_page" class="quot quoted str">
https://github.com/magicprinc
<val id=" home
page " class="quot quoted str">
https://github.com/ magicprinc
</val>
</obj>
</arr>
Expand Down
5 changes: 4 additions & 1 deletion src/test/resources/example2.xml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
<obj><arr id="projects"><obj><val id="project_name">Google Gson</val><val id="url">https://github.com/google/gson</val><val id="rating">4.956</val><arr id="contributors"><obj><val id="first_name">Jesse</val><val id="last_name">Wilson</val><val id="home_page">https://medium.com/@swankjesse</val></obj></arr></obj><obj><val id="project_name">jsoup</val><val id="url">https://jsoup.org</val><val id="rating">5e10</val><arr id="contributors"><obj><val id="first_name">Jonathan</val><val id="last_name">Hedley</val><val id="home_page">https://jhy.io</val></obj><obj><val id="first_name">Andrej</val><val id="last_name">Fink</val><val id="home_page">https://github.com/magicprinc</val></obj></arr></obj></arr></obj>
<!--
I am here: src\test\resources/example.json *--><obj><arr id="projects"><obj><val id="project_name">Google Gson</val><val id="url">https://github.com/google/gson</val><val id="rating">4.956</val><arr id="contributors"><obj><val id="first_name">Jesse</val><val id=" last name ">'Wilson ' </val><val id="home_page">https://medium.com/@swankjesse</val></obj></arr></obj><obj><val id="project_name">jsoup</val><val id="url">https://jsoup.org</val><val id="rating">5e10</val><arr id="contributors"><obj><val id="first_name">Jonathan</val><val id="last_name">Hedley</val><val id="home_page">https://jhy.io</val></obj><obj><!--/2nd OBJ in Array--><val id="first_name">Andrej</val><val id="last_name">Fink</val><val id=" home
page "> https://github.com/
magicprinc </val></obj></arr></obj></arr></obj>

0 comments on commit 58dd798

Please sign in to comment.