Skip to content

Commit 4d49cd5

Browse files
authored
Sync with underscore-java
1 parent 466afb2 commit 4d49cd5

19 files changed

+270
-251
lines changed

README.md

Lines changed: 109 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ Java 17 and later, [Java 11](https://github.com/javadev/underscore-java) or [Kot
2424

2525
## Installation
2626

27-
Include the following in your `pom.xml` for Maven:
27+
To configure your Maven project, add the following code to your pom.xml file:
2828

29-
```
29+
```xml
3030
<dependencies>
3131
<dependency>
3232
<groupId>com.github.javadev</groupId>
3333
<artifactId>underscore17</artifactId>
34-
<version>1.66</version>
34+
<version>1.67</version>
3535
</dependency>
3636
...
3737
</dependencies>
3838
```
3939

40-
Gradle:
40+
Gradle configuration:
4141

4242
```groovy
43-
implementation 'com.github.javadev:underscore17:1.66'
43+
implementation 'com.github.javadev:underscore17:1.67'
4444
```
4545

4646
### Usage
@@ -91,7 +91,7 @@ U.xmlToJson(
9191
+ " <many>elements</many>\n"
9292
+ " <many>more elements</many>\n"
9393
+ " </and>\n"
94-
+ " <plus a=\"complex\">\n"
94+
+ " <plus a=\"complex\">\n"
9595
+ " element as well\n"
9696
+ " </plus>\n"
9797
+ "</mydocument>",
@@ -113,6 +113,45 @@ U.xmlToJson(
113113
// "#omit-xml-declaration": "yes"
114114
// }
115115

116+
U.xmlToJsonMinimum(
117+
"<data>\n"
118+
+ " <string>Example Text</string>\n"
119+
+ " <integer>42</integer>\n"
120+
+ " <float>3.14</float>\n"
121+
+ " <boolean>true</boolean>\n"
122+
+ " <date>2025-02-26</date>\n"
123+
+ " <time>14:30:00</time>\n"
124+
+ " <datetime>2025-02-26T14:30:00Z</datetime>\n"
125+
+ " <array>Item 1</array>\n"
126+
+ " <array>Item 2</array>\n"
127+
+ " <object>\n"
128+
+ " <key1>Value 1</key1>\n"
129+
+ " <key2>Value 2</key2>\n"
130+
+ " </object>\n"
131+
+ " <null/>\n"
132+
+ "</data>\n",
133+
Json.JsonStringBuilder.Step.TWO_SPACES);
134+
// {
135+
// "data": {
136+
// "string": "Example Text",
137+
// "integer": "42",
138+
// "float": "3.14",
139+
// "boolean": "true",
140+
// "date": "2025-02-26",
141+
// "time": "14:30:00",
142+
// "datetime": "2025-02-26T14:30:00Z",
143+
// "array": [
144+
// "Item 1",
145+
// "Item 2"
146+
// ],
147+
// "object": {
148+
// "key1": "Value 1",
149+
// "key2": "Value 2"
150+
// },
151+
// "null": ""
152+
// }
153+
// }
154+
116155
U.jsonToXml(
117156
"{\n"
118157
+ " \"mydocument\": {\n"
@@ -141,6 +180,45 @@ U.jsonToXml(
141180
// </plus>
142181
// </mydocument>
143182

183+
U.jsonToXmlMinimum(
184+
"{\n"
185+
+ " \"data\": {\n"
186+
+ " \"string\": \"Example Text\",\n"
187+
+ " \"integer\": \"42\",\n"
188+
+ " \"float\": \"3.14\",\n"
189+
+ " \"boolean\": \"true\",\n"
190+
+ " \"date\": \"2025-02-26\",\n"
191+
+ " \"time\": \"14:30:00\",\n"
192+
+ " \"datetime\": \"2025-02-26T14:30:00Z\",\n"
193+
+ " \"array\": [\n"
194+
+ " \"Item 1\",\n"
195+
+ " \"Item 2\"\n"
196+
+ " ],\n"
197+
+ " \"object\": {\n"
198+
+ " \"key1\": \"Value 1\",\n"
199+
+ " \"key2\": \"Value 2\"\n"
200+
+ " },\n"
201+
+ " \"null\": \"\"\n"
202+
+ " }\n"
203+
+ "}",
204+
Xml.XmlStringBuilder.Step.TWO_SPACES);
205+
// <data>
206+
// <string>Example Text</string>
207+
// <integer>42</integer>
208+
// <float>3.14</float>
209+
// <boolean>true</boolean>
210+
// <date>2025-02-26</date>
211+
// <time>14:30:00</time>
212+
// <datetime>2025-02-26T14:30:00Z</datetime>
213+
// <array>Item 1</array>
214+
// <array>Item 2</array>
215+
// <object>
216+
// <key1>Value 1</key1>
217+
// <key2>Value 2</key2>
218+
// </object>
219+
// <null string="true"/>
220+
// </data>
221+
144222
U.Builder builder = U.objectBuilder()
145223
.add("firstName", "John")
146224
.add("lastName", "Smith")
@@ -212,79 +290,6 @@ System.out.println(builder.toXml());
212290
</root>
213291
```
214292

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

290295
This code:
@@ -299,9 +304,11 @@ XmlBuilder builder = XmlBuilder.create("Projects")
299304
.e("JetS3t").a("language", "Java").a("scm", "CVS")
300305
.e("Location").a("type", "URL")
301306
.t("https://jets3t.s3.amazonaws.com/index.html");
307+
System.out.println(builder.toXml(Xml.XmlStringBuilder.Step.TWO_SPACES));
308+
System.out.println(builder.toJson(Json.JsonStringBuilder.Step.TWO_SPACES));
302309
```
303310

304-
Generates the following XML document:
311+
Generates the following XML and JSON documents:
305312

306313
```xml
307314
<?xml version="1.0" encoding="UTF-8"?>
@@ -314,6 +321,28 @@ Generates the following XML document:
314321
</JetS3t>
315322
</Projects>
316323
```
324+
```json
325+
{
326+
"Projects": {
327+
"underscore-java": {
328+
"-language": "Java",
329+
"-scm": "SVN",
330+
"Location": {
331+
"-type": "URL",
332+
"#text": "https://github.com/javadev/underscore-java/"
333+
}
334+
},
335+
"JetS3t": {
336+
"-language": "Java",
337+
"-scm": "CVS",
338+
"Location": {
339+
"-type": "URL",
340+
"#text": "https://jets3t.s3.amazonaws.com/index.html"
341+
}
342+
}
343+
}
344+
}
345+
```
317346

318347
Underscore-java is a java port of [Underscore.js](https://underscorejs.org/).
319348

src/main/java/com/github/underscore/Base32.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2024 Valentyn Kolesnikov
4+
* Copyright 2015-2025 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

src/main/java/com/github/underscore/Json.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2024 Valentyn Kolesnikov
4+
* Copyright 2015-2025 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -83,7 +83,9 @@ public JsonStringBuilder append(final String string) {
8383
}
8484

8585
public JsonStringBuilder fillSpaces() {
86-
builder.append(String.valueOf(identStep == Step.TABS ? '\t' : ' ').repeat(Math.max(0, indent)));
86+
builder.append(
87+
String.valueOf(identStep == Step.TABS ? '\t' : ' ')
88+
.repeat(Math.max(0, indent)));
8789
return this;
8890
}
8991

src/main/java/com/github/underscore/U.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2024 Valentyn Kolesnikov
4+
* Copyright 2015-2025 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -98,16 +98,7 @@ public class U<T> extends Underscore<T> {
9898
private static final String YES = "yes";
9999
private static final java.util.regex.Pattern RE_WORDS =
100100
java.util.regex.Pattern.compile(
101-
UPPER
102-
+ "+(?="
103-
+ UPPER
104-
+ LOWER
105-
+ ")|"
106-
+ UPPER
107-
+ "?"
108-
+ LOWER
109-
+ "|"
110-
+ UPPER
101+
UPPER + "+(?=" + UPPER + LOWER + ")|" + UPPER + "?" + LOWER + "|" + UPPER
111102
+ "+|\\d+");
112103

113104
static {
@@ -2069,8 +2060,7 @@ public static long downloadUrl(final String url, final String fileName)
20692060

20702061
public static void decompressGzip(final String sourceFileName, final String targetFileName)
20712062
throws IOException {
2072-
try (GZIPInputStream gis =
2073-
new GZIPInputStream(new FileInputStream(sourceFileName))) {
2063+
try (GZIPInputStream gis = new GZIPInputStream(new FileInputStream(sourceFileName))) {
20742064
Files.copy(gis, Paths.get(targetFileName));
20752065
}
20762066
}
@@ -2524,12 +2514,16 @@ public static <T> String join(final Iterable<T> iterable, final String separator
25242514
return Underscore.join(iterable, separator);
25252515
}
25262516

2527-
public static <T> String joinToString(final Iterable<T> iterable, final String separator,
2528-
final String prefix, final String postfix,
2529-
final int limit,
2530-
final String truncated,
2531-
final Function<T, String> transform) {
2532-
return Underscore.joinToString(iterable, separator, prefix, postfix, limit, truncated, transform);
2517+
public static <T> String joinToString(
2518+
final Iterable<T> iterable,
2519+
final String separator,
2520+
final String prefix,
2521+
final String postfix,
2522+
final int limit,
2523+
final String truncated,
2524+
final Function<T, String> transform) {
2525+
return Underscore.joinToString(
2526+
iterable, separator, prefix, postfix, limit, truncated, transform);
25332527
}
25342528

25352529
public static String toJson(Collection collection) {
@@ -2914,8 +2908,7 @@ public static Map<String, Object> removeMinusesAndConvertNumbers(Map<String, Obj
29142908
} else {
29152909
newKey = entry.getKey();
29162910
}
2917-
if (!entry.getKey().equals(SELF_CLOSING)
2918-
&& !entry.getKey().equals(OMIT_XML_DECL)) {
2911+
if (!entry.getKey().equals(SELF_CLOSING) && !entry.getKey().equals(OMIT_XML_DECL)) {
29192912
outMap.put(newKey, makeObject(entry.getValue()));
29202913
}
29212914
}

0 commit comments

Comments
 (0)