Skip to content

Commit 1d4fdf8

Browse files
committed
Sync with underscore-java
1 parent a8a27af commit 1d4fdf8

File tree

1 file changed

+108
-79
lines changed

1 file changed

+108
-79
lines changed

README.md

Lines changed: 108 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,45 @@ U.xmlToJson(
101101
// "#omit-xml-declaration": "yes"
102102
// }
103103

104+
U.xmlToJsonMinimum(
105+
"<data>\n"
106+
+ " <string>Example Text</string>\n"
107+
+ " <integer>42</integer>\n"
108+
+ " <float>3.14</float>\n"
109+
+ " <boolean>true</boolean>\n"
110+
+ " <date>2025-02-26</date>\n"
111+
+ " <time>14:30:00</time>\n"
112+
+ " <datetime>2025-02-26T14:30:00Z</datetime>\n"
113+
+ " <array>Item 1</array>\n"
114+
+ " <array>Item 2</array>\n"
115+
+ " <object>\n"
116+
+ " <key1>Value 1</key1>\n"
117+
+ " <key2>Value 2</key2>\n"
118+
+ " </object>\n"
119+
+ " <null/>\n"
120+
+ "</data>\n",
121+
Json.JsonStringBuilder.Step.TWO_SPACES);
122+
// {
123+
// "data": {
124+
// "string": "Example Text",
125+
// "integer": "42",
126+
// "float": "3.14",
127+
// "boolean": "true",
128+
// "date": "2025-02-26",
129+
// "time": "14:30:00",
130+
// "datetime": "2025-02-26T14:30:00Z",
131+
// "array": [
132+
// "Item 1",
133+
// "Item 2"
134+
// ],
135+
// "object": {
136+
// "key1": "Value 1",
137+
// "key2": "Value 2"
138+
// },
139+
// "null": ""
140+
// }
141+
// }
142+
104143
U.jsonToXml(
105144
"{\n"
106145
+ " \"mydocument\": {\n"
@@ -129,6 +168,45 @@ U.jsonToXml(
129168
// </plus>
130169
// </mydocument>
131170

171+
U.jsonToXmlMinimum(
172+
"{\n"
173+
+ " \"data\": {\n"
174+
+ " \"string\": \"Example Text\",\n"
175+
+ " \"integer\": \"42\",\n"
176+
+ " \"float\": \"3.14\",\n"
177+
+ " \"boolean\": \"true\",\n"
178+
+ " \"date\": \"2025-02-26\",\n"
179+
+ " \"time\": \"14:30:00\",\n"
180+
+ " \"datetime\": \"2025-02-26T14:30:00Z\",\n"
181+
+ " \"array\": [\n"
182+
+ " \"Item 1\",\n"
183+
+ " \"Item 2\"\n"
184+
+ " ],\n"
185+
+ " \"object\": {\n"
186+
+ " \"key1\": \"Value 1\",\n"
187+
+ " \"key2\": \"Value 2\"\n"
188+
+ " },\n"
189+
+ " \"null\": \"\"\n"
190+
+ " }\n"
191+
+ "}",
192+
Xml.XmlStringBuilder.Step.TWO_SPACES);
193+
// <data>
194+
// <string>Example Text</string>
195+
// <integer>42</integer>
196+
// <float>3.14</float>
197+
// <boolean>true</boolean>
198+
// <date>2025-02-26</date>
199+
// <time>14:30:00</time>
200+
// <datetime>2025-02-26T14:30:00Z</datetime>
201+
// <array>Item 1</array>
202+
// <array>Item 2</array>
203+
// <object>
204+
// <key1>Value 1</key1>
205+
// <key2>Value 2</key2>
206+
// </object>
207+
// <null string="true"/>
208+
// </data>
209+
132210
U.Builder builder = U.objectBuilder()
133211
.add("firstName", "John")
134212
.add("lastName", "Smith")
@@ -200,108 +278,59 @@ System.out.println(builder.toXml());
200278
</root>
201279
```
202280

203-
```java
204-
String inventory =
205-
"{\n"
206-
+ " \"inventory\": {\n"
207-
+ " \"#comment\": \"Test is test comment\",\n"
208-
+ " \"book\": [\n"
209-
+ " {\n"
210-
+ " \"-year\": \"2000\",\n"
211-
+ " \"title\": \"Snow Crash\",\n"
212-
+ " \"author\": \"Neal Stephenson\",\n"
213-
+ " \"publisher\": \"Spectra\",\n"
214-
+ " \"isbn\": \"0553380958\",\n"
215-
+ " \"price\": \"14.95\"\n"
216-
+ " },\n"
217-
+ " {\n"
218-
+ " \"-year\": \"2005\",\n"
219-
+ " \"title\": \"Burning Tower\",\n"
220-
+ " \"author\": [\n"
221-
+ " \"Larry Niven\",\n"
222-
+ " \"Jerry Pournelle\"\n"
223-
+ " ],\n"
224-
+ " \"publisher\": \"Pocket\",\n"
225-
+ " \"isbn\": \"0743416910\",\n"
226-
+ " \"price\": \"5.99\"\n"
227-
+ " },\n"
228-
+ " {\n"
229-
+ " \"-year\": \"1995\",\n"
230-
+ " \"title\": \"Zodiac\",\n"
231-
+ " \"author\": \"Neal Stephenson\",\n"
232-
+ " \"publisher\": \"Spectra\",\n"
233-
+ " \"isbn\": \"0553573862\",\n"
234-
+ " \"price\": \"7.50\"\n"
235-
+ " }\n"
236-
+ " ]\n"
237-
+ " }\n"
238-
+ "}";
239-
String title = U.selectToken(U.fromJsonMap(inventory), "//book[@year>2001]/title/text()");
240-
// "Burning Tower"
241-
242-
String json =
243-
"{\n"
244-
+ " \"Stores\": [\n"
245-
+ " \"Lambton Quay\",\n"
246-
+ " \"Willis Street\"\n"
247-
+ " ],\n"
248-
+ " \"Manufacturers\": [\n"
249-
+ " {\n"
250-
+ " \"Name\": \"Acme Co\",\n"
251-
+ " \"Products\": [\n"
252-
+ " {\n"
253-
+ " \"Name\": \"Anvil\",\n"
254-
+ " \"Price\": 50\n"
255-
+ " }\n"
256-
+ " ]\n"
257-
+ " },\n"
258-
+ " {\n"
259-
+ " \"Name\": \"Contoso\",\n"
260-
+ " \"Products\": [\n"
261-
+ " {\n"
262-
+ " \"Name\": \"Elbow Grease\",\n"
263-
+ " \"Price\": 99.95\n"
264-
+ " },\n"
265-
+ " {\n"
266-
+ " \"Name\": \"Headlight Fluid\",\n"
267-
+ " \"Price\": 4\n"
268-
+ " }\n"
269-
+ " ]\n"
270-
+ " }\n"
271-
+ " ]\n"
272-
+ "}";
273-
List<String> names = U.selectTokens(U.fromJsonMap(json), "//Products[Price>=50]/Name/text()");
274-
// [Anvil, Elbow Grease]
275-
```
276281
Simplify XML document creation by structuring your code like the final document.
277282

278283
This code:
279284

280285
```java
281286
XmlBuilder builder = XmlBuilder.create("Projects")
282-
.e("underscore-kotlin").a("language", "Java").a("scm", "SVN")
287+
.e("underscore-java").a("language", "Java").a("scm", "SVN")
283288
.e("Location").a("type", "URL")
284-
.t("https://github.com/kotlindev/underscore-kotlin/")
289+
.t("https://github.com/javadev/underscore-java/")
285290
.up()
286291
.up()
287292
.e("JetS3t").a("language", "Java").a("scm", "CVS")
288293
.e("Location").a("type", "URL")
289294
.t("https://jets3t.s3.amazonaws.com/index.html");
295+
System.out.println(builder.toXml(Xml.XmlStringBuilder.Step.TWO_SPACES));
296+
System.out.println(builder.toJson(Json.JsonStringBuilder.Step.TWO_SPACES));
290297
```
291298

292-
Generates the following XML document:
299+
Generates the following XML and JSON documents:
293300

294301
```xml
295302
<?xml version="1.0" encoding="UTF-8"?>
296303
<Projects>
297-
<underscore-kotlin language="Java" scm="SVN">
298-
<Location type="URL">https://github.com/kotlindev/underscore-kotlin/</Location>
299-
</underscore-kotlin>
304+
<underscore-java language="Java" scm="SVN">
305+
<Location type="URL">https://github.com/javadev/underscore-java/</Location>
306+
</underscore-java>
300307
<JetS3t language="Java" scm="CVS">
301308
<Location type="URL">https://jets3t.s3.amazonaws.com/index.html</Location>
302309
</JetS3t>
303310
</Projects>
304311
```
312+
```json
313+
{
314+
"Projects": {
315+
"underscore-java": {
316+
"-language": "Java",
317+
"-scm": "SVN",
318+
"Location": {
319+
"-type": "URL",
320+
"#text": "https://github.com/javadev/underscore-java/"
321+
}
322+
},
323+
"JetS3t": {
324+
"-language": "Java",
325+
"-scm": "CVS",
326+
"Location": {
327+
"-type": "URL",
328+
"#text": "https://jets3t.s3.amazonaws.com/index.html"
329+
}
330+
}
331+
}
332+
}
333+
```
305334

306335
Underscore-kotlin is a kotlin port of [Underscore.js](https://underscorejs.org/).
307336

0 commit comments

Comments
 (0)