https://github.com/javadev/underscore-java17
java 17 port of Underscore.js
https://github.com/javadev/underscore-java17
functional-programming java java-xmlbuilder json json-to-xml json-to-xml-converter json-to-xml-java underscore xml xml-to-json xml-to-json-java xmlbuilder
Last synced: 6 months ago
JSON representation
java 17 port of Underscore.js
- Host: GitHub
- URL: https://github.com/javadev/underscore-java17
- Owner: javadev
- License: mit
- Created: 2019-03-18T07:49:05.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-04-14T00:29:22.000Z (about 1 year ago)
- Last Synced: 2024-04-14T15:03:40.363Z (about 1 year ago)
- Topics: functional-programming, java, java-xmlbuilder, json, json-to-xml, json-to-xml-converter, json-to-xml-java, underscore, xml, xml-to-json, xml-to-json-java, xmlbuilder
- Language: Java
- Homepage: https://javadev.github.io/underscore-java/
- Size: 696 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
underscore-java17
=================[](https://central.sonatype.com/artifact/com.github.javadev/underscore17/1.64)
[ ](https://github.com/javadev/underscore-java17/blob/main/LICENSE)
[](https://github.com/javadev/underscore-java17/actions/workflows/maven.yml)
[](https://github.com/javadev/underscore-java17/actions/workflows/codeql-analysis.yml)
[](https://github.com/javadev/underscore-java17/actions/workflows/semgrep.yml)
[](https://github.com/javadev/underscore-java17/actions/workflows/scorecard.yml)
[](https://codecov.io/gh/javadev/underscore-java17)
[](https://sonarcloud.io/summary/overall?id=javadev_underscore-java11)
[](https://javadoc.io/doc/com.github.javadev/underscore17)
[](https://dev.azure.com/javadevazure/underscore-java/_build/latest?definitionId=6&branchName=main)

[](https://github.com/javadev/underscore-java17)
[](https://github.com/javadev/underscore-java17/fork)Requirements
============Java 17 and later, [Java 11](https://github.com/javadev/underscore-java) or [Kotlin](https://github.com/kotlindev/underscore-kotlin)
## Installation
Include the following in your `pom.xml` for Maven:
```
com.github.javadev
underscore17
1.64
...```
Gradle:
```groovy
implementation 'com.github.javadev:underscore17:1.64'
```### Usage
```java
U.of(/* array | list | set | map | anything based on Iterable interface */)
.filter(..)
.map(..)
...
.sortWith()
.forEach(..);
U.of(value1, value2, value3)...
U.range(0, 10)...U.of(1, 2, 3) // or java.util.Arrays.asList(1, 2, 3) or new Integer[] {1, 2, 3}
.filter(v -> v > 1)
// 2, 3
.map(v -> v + 1)
// 3, 4
.sortWith((a, b) -> b.compareTo(a))
.forEach(System.out::println);
// 4, 3
U.of(1, 2, 3) // or java.util.Arrays.asList(1, 2, 3) or new Integer[] {1, 2, 3}
.mapMulti((num, consumer) -> {
for (int i = 0; i < num; i++) {
consumer.accept("a" + num);
}
})
.forEach(System.out::println);
// "a1", "a2", "a2", "a3", "a3", "a3"U.formatXml("data", Xml.XmlStringBuilder.Step.TWO_SPACES);
//
// data
//U.formatJson("{\"a\":{\"b\":\"data\"}}", Json.JsonStringBuilder.Step.TWO_SPACES);
// {
// "a": {
// "b": "data"
// }
// }U.xmlToJson(
"\n"
+ " \n"
+ " elements\n"
+ " more elements\n"
+ " \n"
+ " \n"
+ " element as well\n"
+ " \n"
+ "",
Json.JsonStringBuilder.Step.TWO_SPACES);
// {
// "mydocument": {
// "-has": "an attribute",
// "and": {
// "many": [
// "elements",
// "more elements"
// ]
// },
// "plus": {
// "-a": "complex",
// "#text": "\n element as well\n "
// }
// },
// "#omit-xml-declaration": "yes"
// }U.jsonToXml(
"{\n"
+ " \"mydocument\": {\n"
+ " \"-has\": \"an attribute\",\n"
+ " \"and\": {\n"
+ " \"many\": [\n"
+ " \"elements\",\n"
+ " \"more elements\"\n"
+ " ]\n"
+ " },\n"
+ " \"plus\": {\n"
+ " \"-a\": \"complex\",\n"
+ " \"#text\": \"\\n element as well\\n \"\n"
+ " }\n"
+ " },\n"
+ " \"#omit-xml-declaration\": \"yes\"\n"
+ "}",
Xml.XmlStringBuilder.Step.TWO_SPACES);
//
//
// elements
// more elements
//
//
// element as well
//
//U.Builder builder = U.objectBuilder()
.add("firstName", "John")
.add("lastName", "Smith")
.add("age", 25)
.add("address", U.arrayBuilder()
.add(U.objectBuilder()
.add("streetAddress", "21 2nd Street")
.add("city", "New York")
.addNull("cityId")
.add("state", "NY")
.add("postalCode", "10021")))
.add("phoneNumber", U.arrayBuilder()
.add(U.objectBuilder()
.add("type", "home")
.add("number", "212 555-1234"))
.add(U.objectBuilder()
.add("type", "fax")
.add("number", "646 555-4567")));
System.out.println(builder.toJson());
System.out.println(builder.toXml());
```
```javascript
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": [
{
"streetAddress": "21 2nd Street",
"city": "New York",
"cityId": null,
"state": "NY",
"postalCode": "10021"
}
],
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
```
```xmlJohn
Smith
25
21 2nd Street
New York
NY
10021
home
212 555-1234
fax
646 555-4567
```
```java
String inventory =
"{\n"
+ " \"inventory\": {\n"
+ " \"#comment\": \"Test is test comment\",\n"
+ " \"book\": [\n"
+ " {\n"
+ " \"-year\": \"2000\",\n"
+ " \"title\": \"Snow Crash\",\n"
+ " \"author\": \"Neal Stephenson\",\n"
+ " \"publisher\": \"Spectra\",\n"
+ " \"isbn\": \"0553380958\",\n"
+ " \"price\": \"14.95\"\n"
+ " },\n"
+ " {\n"
+ " \"-year\": \"2005\",\n"
+ " \"title\": \"Burning Tower\",\n"
+ " \"author\": [\n"
+ " \"Larry Niven\",\n"
+ " \"Jerry Pournelle\"\n"
+ " ],\n"
+ " \"publisher\": \"Pocket\",\n"
+ " \"isbn\": \"0743416910\",\n"
+ " \"price\": \"5.99\"\n"
+ " },\n"
+ " {\n"
+ " \"-year\": \"1995\",\n"
+ " \"title\": \"Zodiac\",\n"
+ " \"author\": \"Neal Stephenson\",\n"
+ " \"publisher\": \"Spectra\",\n"
+ " \"isbn\": \"0553573862\",\n"
+ " \"price\": \"7.50\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}";
String title = U.selectToken(U.fromJsonMap(inventory), "//book[@year>2001]/title/text()");
// "Burning Tower"String json =
"{\n"
+ " \"Stores\": [\n"
+ " \"Lambton Quay\",\n"
+ " \"Willis Street\"\n"
+ " ],\n"
+ " \"Manufacturers\": [\n"
+ " {\n"
+ " \"Name\": \"Acme Co\",\n"
+ " \"Products\": [\n"
+ " {\n"
+ " \"Name\": \"Anvil\",\n"
+ " \"Price\": 50\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"Name\": \"Contoso\",\n"
+ " \"Products\": [\n"
+ " {\n"
+ " \"Name\": \"Elbow Grease\",\n"
+ " \"Price\": 99.95\n"
+ " },\n"
+ " {\n"
+ " \"Name\": \"Headlight Fluid\",\n"
+ " \"Price\": 4\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ "}";
List names = U.selectTokens(U.fromJsonMap(json), "//Products[Price>=50]/Name/text()");
// [Anvil, Elbow Grease]
```
Simplify XML document creation by structuring your code like the final document.This code:
```java
XmlBuilder builder = XmlBuilder.create("Projects")
.e("underscore-java").a("language", "Java").a("scm", "SVN")
.e("Location").a("type", "URL")
.t("https://github.com/javadev/underscore-java/")
.up()
.up()
.e("JetS3t").a("language", "Java").a("scm", "CVS")
.e("Location").a("type", "URL")
.t("https://jets3t.s3.amazonaws.com/index.html");
```Generates the following XML document:
```xml
https://github.com/javadev/underscore-java/
https://jets3t.s3.amazonaws.com/index.html
```
Underscore-java is a java port of [Underscore.js](https://underscorejs.org/).
In addition to porting Underscore's functionality, Underscore-java includes matching unit tests.
For docs, license, tests, and downloads, see:
https://javadev.github.io/underscore-javaThanks to Jeremy Ashkenas and all contributors to Underscore.js.