Cutlet is a "batteries included" library to make working directly with XML and JSON as data structures in Java simpler.
- Simple, concise, fluent, type-safe API
- The same API for XML and JSON, as far as possible given their slightly different data models
- Reading/writing of XML and JSON from/to strings, streams and files
- Provides useful error messages
- Conversion of common data types (Strings, BigInteger, BigDecimal, LocalDate, DateTime, enums) supporting most standard formats (eg ISO8601 dates) and edge cases (exponential notation for numbers) as well as microtypes
- Pluggable data type converters
- Uses XPath to select nodes in both XML and JSON
- Nested approach: Extract sub-documents and working with them in the same manner as full documents
Non goals:
- Mapping between classes and JSON/XML
- Performance and memory efficiency are secondary goals
Hello world in JSON:
JSON.create().add("message").with("hello", "world")
creates:
{
"message": {
"hello": "world"
}
}
Hello world in XML:
XML.create("message").with("hello", "world")
creates:
<message>
<hello>world</hello>
</message>
More complex: Given an XML file containing a list of people and their home and mobile phone numbers (input.xml), output a JSON associative array of their mobile phone numbers to names, changing the keys for "firstname" and "lastname" to "forename" and "surname" (output.json):
JSON output = JSON.create();
XML.parseFile("input.xml").getList("person").forEach(person ->
output.add("mobile-" + person.getString("phonenumber[@type = 'mobile']"))
.withString("forename", person.getString("firstname"))
.withString("surname", person.getString("lastname")));
String json = output.write(PRETTY);
See the JSON and XML tests cases for more examples.
Current version is 0.4 - alpha quality code (API is subject to change).
Maven:
<dependency>
<groupId>com.snell.michael.cutlet</groupId>
<artifactId>cutlet</artifactId>
<version>0.4</version>
</dependency>
Direct download: http://repo1.maven.org/maven2/com/snell/michael/cutlet/cutlet
- Copyright 2015 Michael Snell
- Licensed under the MIT license - see LICENSE
- Things may break. Performance may suffer. Giant creatures may arise from the oceans and destroy your civilization.