Skip to content

Commit

Permalink
* more informative README.md
Browse files Browse the repository at this point in the history
+ example1.xml and example2.xml
  • Loading branch information
magicprinc committed Dec 2, 2021
1 parent bb042e4 commit bf7dad2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 17 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ See [**jsoup.org**](https://jsoup.org/) for downloads and the full [API document
**jsoup.json** is a powerful JSON [RFC 4627](http://www.ietf.org/rfc/rfc4627.txt), 7158, [RFC 7159](http://www.ietf.org/rfc/rfc7159.txt) parser for jsoup.
It can consume almost every text as JSON and convert it internally to an XML-tree.

All JSON elements are converted one-to-one into three XML tags (obj, arr, val), XML comment section, (very rare and with many details) "unk" tag and two main attributes (id, class).

No extra dependencies!

**example.json**
[**example.json**](src/test/resources/example.json)
```json
{projects: [
{
Expand Down Expand Up @@ -41,8 +43,8 @@ assert "jsoup".equals(doc.select("#projects #project_name.str.unquoted").text())
//
assertEquals("<arr><val class=\"bool\">true</val><val class=\"bool\">true</val></arr>", JsonTreeBuilder.jsonToXml("[true, true]"));
```
**internal xml tree**

[**internal XML tree 1 (with class metadata) **](src/test/resources/example1.xml)
```xml
<obj>
<arr id="projects">
Expand Down Expand Up @@ -84,6 +86,7 @@ or
Document doc = Jsoup.parse(exampleJson, "UTF-8", "", JsonTreeBuilder.jsonParser(false));//no extra info in attrs
```

[**internal XML tree 2 (no metadata) **](src/test/resources/example2.xml)
```xml
<obj>
<arr id="projects">
Expand Down Expand Up @@ -118,4 +121,6 @@ Document doc = Jsoup.parse(exampleJson, "UTF-8", "", JsonTreeBuilder.jsonParser(
</obj>
</arr>
</obj>
```
```

More examples are available in 135 unit-tests in [org.jsoup.parser.JsonTreeBuilderTest](/src/test/java/org/jsoup/parser/JsonTreeBuilderTest.java)
22 changes: 15 additions & 7 deletions src/test/java/org/jsoup/parser/JsonTreeBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -1506,7 +1509,7 @@ public void testSpeed () {
long tx = loopIt(rx, 1000);

assert tg < tj;//GSON is the quickest one
assert tj < tx : "tj="+tj+", tx="+tx;//json is the second one, but xml is fast too now (prev: 3x slower)
assert tj < tx+777 : "tj="+tj+", tx="+tx;//json is usually the second one, but xml is fast too now
}


Expand Down Expand Up @@ -1539,6 +1542,13 @@ boolean binarySame (String canonicalXmlFileName, String generatedXmlStr) throws
return sameSize;
}

void writeXml (String xml, String fileName) throws IOException {
String enable = System.getProperty("genXml");
if (enable != null && enable.length()>0) {
Path pathFile = Paths.get(fileName);//e.g. "/temp/bigdata.xml"
Files.write(pathFile, xml.getBytes(UTF_8));
}
}

public void testBinarySame () throws Exception {
Document docJson = loadDoc("/bigdata.json", jsonParser(false));
Expand All @@ -1551,8 +1561,7 @@ public void testBinarySame () throws Exception {
assertEquals(jsonXml, xmlXml);
assertEquals(docJson.text(), docXml.text());

//Path fileName = Paths.get("/temp/bigdata.xml");
//Files.write(fileName, xmlXml.getBytes(UTF_8));
writeXml(jsonXml, "/temp/bigdata.xml");

assertTrue(binarySame("/bigdata.xml", jsonXml));
assertTrue(binarySame("/bigdata.xml", xmlXml));
Expand Down Expand Up @@ -1715,12 +1724,11 @@ public void testJavaDocExample () throws Exception {

assertEquals("<arr><val class=\"bool\">true</val><val class=\"bool\">true</val></arr>", jsonToXml("[true, true]"));

/* doc.outputSettings().prettyPrint(true);
Files.write(Paths.get("/temp/example.xml"), doc.html().getBytes(UTF_8));
doc.outputSettings().prettyPrint(true);
writeXml(doc.html(), "/temp/example1.xml");

doc = Jsoup.parse(getClass().getResourceAsStream("/example.json"), "UTF-8", "", jsonParser(false));
doc.outputSettings().prettyPrint(true);
Files.write(Paths.get("/temp/example.xml"), doc.html().getBytes(UTF_8));*/
writeXml(doc.html(), "/temp/example2.xml");
}


Expand Down
13 changes: 7 additions & 6 deletions src/test/resources/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
'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",
"project_name" => jsoup

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

"rating": 5e10,
"contributors": [
{
"first_name" = "Jonathan", "last_name" => "Hedley",
"home_page": "https://jhy.io"
"home_page": "https://jhy.io"
},{
"first_name": "Andrej", "last_name": "Fink";
"home_page": "https://github.com/magicprinc"
"first_name": "Andrej", "last_name": "Fink";;; "home_page": "https://github.com/magicprinc"
}
]
}
Expand Down
63 changes: 63 additions & 0 deletions src/test/resources/example1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<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="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>
1 change: 1 addition & 0 deletions src/test/resources/example2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<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>

0 comments on commit bf7dad2

Please sign in to comment.