@@ -59,36 +59,83 @@ U.of(1, 2, 3) // or java.util.Arrays.asList(1, 2, 3) or new Integer[] {1, 2, 3}
5959 .map(v - > v + 1 )
6060 // 3, 4
6161 .sortWith((a, b) - > b. compareTo(a))
62- // 4, 3
6362 .forEach(System . out:: println);
6463 // 4, 3
6564
66- U . formatXml(" <a><b>data</b></a>" );
65+ U . of(1 , 2 , 3 ) // or java.util.Arrays.asList(1, 2, 3) or new Integer[] {1, 2, 3}
66+ .mapMulti((num, consumer) - > {
67+ for (int i = 0 ; i < num; i++ ) {
68+ consumer. accept(" a" + num);
69+ }
70+ })
71+ .forEach(System . out:: println);
72+ // "a1", "a2", "a2", "a3", "a3", "a3"
73+
74+ U . formatXml(" <a><b>data</b></a>" , Xml . XmlStringBuilder . Step . TWO_SPACES );
6775 // <a>
68- // <b>data</b>
76+ // <b>data</b>
6977 // </a>
7078
71- U . formatJson(" {\" a\" :{\" b\" :\" data\" }}" );
79+ U . formatJson(" {\" a\" :{\" b\" :\" data\" }}" , Json . JsonStringBuilder . Step . TWO_SPACES );
7280 // {
73- // "a": {
74- // "b": "data"
75- // }
81+ // "a": {
82+ // "b": "data"
83+ // }
7684 // }
7785
78- U . xmlToJson(" <a attr=\" c\" ><b>data</b></a>" );
86+ U . xmlToJson(" <mydocument has=\" an attribute\" >\n " +
87+ " <and>\n " +
88+ " <many>elements</many>\n " +
89+ " <many>more elements</many>\n " +
90+ " </and>\n " +
91+ " <plus a=\" complex\" >\n " +
92+ " element as well\n " +
93+ " </plus>\n " +
94+ " </mydocument>" , Json . JsonStringBuilder . Step . TWO_SPACES );
95+
7996 // {
80- // "a": {
81- // "-attr": "c",
82- // "b": "data"
97+ // "mydocument": {
98+ // "-has": "an attribute",
99+ // "and": {
100+ // "many": [
101+ // "elements",
102+ // "more elements"
103+ // ]
104+ // },
105+ // "plus": {
106+ // "-a": "complex",
107+ // "#text": "\n element as well\n "
108+ // }
83109 // },
84110 // "#omit-xml-declaration": "yes"
85111 // }
86112
87- U . jsonToXml(" {\" a\" :{\" -attr\" :\" c\" ,\" b\" :\" data\" }}" );
88- // <?xml version="1.0" encoding="UTF-8"?>
89- // <a attr="c">
90- // <b>data</b>
91- // </a>
113+ U . jsonToXml(" {\n " +
114+ " \" mydocument\" : {\n " +
115+ " \" -has\" : \" an attribute\" ,\n " +
116+ " \" and\" : {\n " +
117+ " \" many\" : [\n " +
118+ " \" elements\" ,\n " +
119+ " \" more elements\"\n " +
120+ " ]\n " +
121+ " },\n " +
122+ " \" plus\" : {\n " +
123+ " \" -a\" : \" complex\" ,\n " +
124+ " \" #text\" : \"\\ n element as well\\ n \"\n " +
125+ " }\n " +
126+ " },\n " +
127+ " \" #omit-xml-declaration\" : \" yes\"\n " +
128+ " }" , Xml . XmlStringBuilder . Step . TWO_SPACES );
129+
130+ // <mydocument has="an attribute">
131+ // <and>
132+ // <many>elements</many>
133+ // <many>more elements</many>
134+ // </and>
135+ // <plus a="complex">
136+ // element as well
137+ // </plus>
138+ // </mydocument>
92139
93140U . Builder builder = U . objectBuilder()
94141 .add(" firstName" , " John" )
@@ -161,6 +208,80 @@ System.out.println(builder.toXml());
161208</root >
162209```
163210
211+ ``` java
212+ String inventory =
213+ " {\n "
214+ + " \" inventory\" : {\n "
215+ + " \" #comment\" : \" Test is test comment\" ,\n "
216+ + " \" book\" : [\n "
217+ + " {\n "
218+ + " \" -year\" : \" 2000\" ,\n "
219+ + " \" title\" : \" Snow Crash\" ,\n "
220+ + " \" author\" : \" Neal Stephenson\" ,\n "
221+ + " \" publisher\" : \" Spectra\" ,\n "
222+ + " \" isbn\" : \" 0553380958\" ,\n "
223+ + " \" price\" : \" 14.95\"\n "
224+ + " },\n "
225+ + " {\n "
226+ + " \" -year\" : \" 2005\" ,\n "
227+ + " \" title\" : \" Burning Tower\" ,\n "
228+ + " \" author\" : [\n "
229+ + " \" Larry Niven\" ,\n "
230+ + " \" Jerry Pournelle\"\n "
231+ + " ],\n "
232+ + " \" publisher\" : \" Pocket\" ,\n "
233+ + " \" isbn\" : \" 0743416910\" ,\n "
234+ + " \" price\" : \" 5.99\"\n "
235+ + " },\n "
236+ + " {\n "
237+ + " \" -year\" : \" 1995\" ,\n "
238+ + " \" title\" : \" Zodiac\" ,\n "
239+ + " \" author\" : \" Neal Stephenson\" ,\n "
240+ + " \" publisher\" : \" Spectra\" ,\n "
241+ + " \" isbn\" : \" 0553573862\" ,\n "
242+ + " \" price\" : \" 7.50\"\n "
243+ + " }\n "
244+ + " ]\n "
245+ + " }\n "
246+ + " }" ;
247+ String title = U . selectToken(U . fromJsonMap(inventory), " //book[@year>2001]/title/text()" );
248+ // "Burning Tower"
249+
250+ String json =
251+ " {\n "
252+ + " \" Stores\" : [\n "
253+ + " \" Lambton Quay\" ,\n "
254+ + " \" Willis Street\"\n "
255+ + " ],\n "
256+ + " \" Manufacturers\" : [\n "
257+ + " {\n "
258+ + " \" Name\" : \" Acme Co\" ,\n "
259+ + " \" Products\" : [\n "
260+ + " {\n "
261+ + " \" Name\" : \" Anvil\" ,\n "
262+ + " \" Price\" : 50\n "
263+ + " }\n "
264+ + " ]\n "
265+ + " },\n "
266+ + " {\n "
267+ + " \" Name\" : \" Contoso\" ,\n "
268+ + " \" Products\" : [\n "
269+ + " {\n "
270+ + " \" Name\" : \" Elbow Grease\" ,\n "
271+ + " \" Price\" : 99.95\n "
272+ + " },\n "
273+ + " {\n "
274+ + " \" Name\" : \" Headlight Fluid\" ,\n "
275+ + " \" Price\" : 4\n "
276+ + " }\n "
277+ + " ]\n "
278+ + " }\n "
279+ + " ]\n "
280+ + " }" ;
281+ List<String > names = U . selectTokens(U . fromJsonMap(json), " //Products[Price>=50]/Name/text()" );
282+ // [Anvil, Elbow Grease]
283+ ```
284+
164285In addition to porting Underscore's functionality, Underscore-java includes matching unit tests.
165286
166287For docs, license, tests, and downloads, see:
0 commit comments