Simple Java Generator (SJG) https://github.com/rkalla/simple-java-generator Changelog --------- 2.0 * Major release * Support for generating text for arrays was added. This follows the same code-path as List generation so existing stubs for list generating is automatically used with no extra work. * Remove IRecursable interface and replaced it with @Recursable annotation. Annotate any POJO classes that you want Simple Generator to recurse on during generation with this. * Added @Encode annotation for String fields on POJOs to indicate 3 different kinds of encoding Simple Generator can do to the values automatically for you: * URL - URL-encode the String * BASE64 - Base64-encode the String * URL_SAFE_BASE64 - Use a URL-safe Base64-encoding for the String. Usage is straight forward, for example: @Encode(Type.URL) String url; * AbstractGenerator was rewritten to allow more robust value handling and cleaner execution with less cycles. 1.2 * Changed IGenerate.generate return type from a StringBuilder to an IInput<char[], char[]>. Allows much more optimized interaction with the generated result (e.g. streaming) without needing to create a String or char[] and copy the contents of the existing char[] again. * Optimized cached indent lookup calculation to avoid the new Integer creation. 1.1 * Initial public release. License ------- This library is released under the Apache 2 License. See LICENSE. Description ----------- Simple Java Generator is a simple, reflection-based, text generation library for Java. Out of the box, SJG provides generators for JSON and XML and support for generated a formatted (pretty-printed) or compact version of those formats. Simple Java Generator supports all the basic value types in Java, all Collection types and arrays as well as recursively generating output for custom object types marked with the @Recursable annotation. Extending SJG to generate other file types is trivial; extending AbstractGenerator gives you 8 or so, 1 or 2 line stub methods that need to be given logic on what text to append(...) to the running output buffer. All reflection, type determination and pre-processing/vetting of values is handled by AbstractGenerator. You can see an example of how simple end-user implementations are by glancing at the source for JSONGenerator or XMLGenerator; any custom generator you write will roughly as simple (file format allowing). Performance ----------- Benchmarks can be reproduced by running the Benchmark class in the /src/test/java code tree. NOTE: While Simple Java Generator only requires Java 5 to run the bytecode generated by compiling the library, the test classes use classes that require Java 6 JRE libraries to run (namely JAXB). It is easiest to run the /src/test classes with Java 6, but SJG itself runs fine in a Java 5 environment. Benchmarks compare some of the most common approaches to Object->JSON and Object->XML text generation; namely Gson for JSON and JAXB from Java 6 for XML. [Platform] * Java 1.6.0_24 on Windows 7 64-bit * Dual Core Intel E6850 processor * 8 GB of ram ==== Benchmarking 50000 iterations ==== [JSON] Gson, Formatted... 7657 ms (7.657 secs - 6529 ops/sec) Gson, Compact... 5201 ms (5.201 secs - 9613 ops/sec) Simple Gen, Formatted... 1788 ms (1.788 secs - 27964 ops/sec) Simple Gen, Compact... 876 ms (0.876 secs - 57077 ops/sec) [XML] JAXB, Formatted... 1525 ms (1.525 secs - 32786 ops/sec) JAXB, Compact... 973 ms (0.973 secs - 51387 ops/sec) Simple Gen, Formatted... 2238 ms (2.238 secs - 22341 ops/sec) Simple Gen, Compact... 1020 ms (1.02 secs - 49019 ops/sec) Example ------- TBD (note about IRecursable) How it Works ------------ TBD Memory/CPU Overhead ------------------- TBD (note about indent and reflection caching) Runtime Requirements -------------------- Requires * tbm-common-lib-<VER>.jar History ------- This project, like many others, was open-sourced from work on imgscalr.com. imgscalr.com is a REST API driven, image-hosting SaaS product. Interaction with the product is done almost exclusively via the web API. Generation of JSON, JSONP or XML from resulting query POJOs inside the system is the most common operation the deployed system performs so it has to be fast. After evaluating popular frameworks out there, I found some to be way too slow (Gson) and while other frameworks were fast (Jackson) the APIs made usage complex or the resulting output reflected from the POJO objects wasn't exactly what I needed and customization was harder than I thought it should be. Wanting a straight forward, fast and *simple* approach was important, so SJG was written and open sourced to the community. Troubleshooting --------------- Here are some issues you might run into and what you can do to correct it. * Simple Java Generator isn't recursing into the custom member classes on my POJO, it just prints out the toString() result! You probably forgot to have any and all custom POJO classes implement the IRecursable interface. This interface "tags" custom classes as something that SJG should use reflection to recurse into to pull values out of. Without it, you are telling SJG to leave those classes alone.
rkalla/simple-java-generator
Simple and fast Object-to-text generation library using Java reflection. SJG is focused on generating Object representations in formats like JSON and XML for web APIs.
JavaApache-2.0