diff --git a/README.md b/README.md index 3b4d8aa..be23f65 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,18 @@ System.out.println("Hello "+obj.getString("title")+"!"); // Expected output: "Hello World!" ```` +The library includes a JSON compression feature that uses template based encoding an http2 header cache inspired caching of repeated string values. + +```java +String source="{\"title\":\"World\"}"; +Compressor cmp=new Compressor("./compression_cache",100,1); +byte[] data=cmp.compress(source); +cmp.commit(); // Write out templates and dictionary data +String output=cmp.decompress(data); +```` + +## Performance + The following table shows a speed comparison for parsing around 551kb of JSON data. JSON Library | Min | Max | Avg | Median diff --git a/src/main/java/me/doubledutch/lazyjson/LazyElement.java b/src/main/java/me/doubledutch/lazyjson/LazyElement.java index 537eef1..e55f1e2 100644 --- a/src/main/java/me/doubledutch/lazyjson/LazyElement.java +++ b/src/main/java/me/doubledutch/lazyjson/LazyElement.java @@ -82,6 +82,15 @@ public String toString(){ return new String(cbuf,root.startIndex,root.endIndex-root.startIndex); } + /** + * Returns the character count of the source string. + * + * @return the length of the source string for this element + */ + public int getSourceLength(){ + return root.endIndex-root.startIndex; + } + public byte[] toByteArray(){ int size=root.getBufferSize(); ByteBuffer buf=ByteBuffer.allocate(size); diff --git a/src/main/java/me/doubledutch/lazyjson/compressor/Compressor.java b/src/main/java/me/doubledutch/lazyjson/compressor/Compressor.java index 2ac3ecd..e80a255 100644 --- a/src/main/java/me/doubledutch/lazyjson/compressor/Compressor.java +++ b/src/main/java/me/doubledutch/lazyjson/compressor/Compressor.java @@ -64,14 +64,19 @@ private boolean shouldCompress(Template t){ } public byte[] compress(String str){ + return compress(LazyElement.parse(str)); + } + + + public byte[] compress(LazyElement elm){ // 1. Parse data - LazyElement elm=LazyElement.parse(str); + // LazyElement elm=LazyElement.parse(str); // 2. Generate template Template t=elm.extractTemplate(); // 3. If template satisfies criterea - compress if(shouldCompress(t)){ try{ - ByteBuffer buf=ByteBuffer.allocate(str.length()-2); + ByteBuffer buf=ByteBuffer.allocate(elm.getSourceLength()-2); buf.putShort((short)templateSet.get(t)); elm.writeTemplateValues(buf,dictionary); int pos=buf.position(); @@ -85,13 +90,25 @@ public byte[] compress(String str){ } // 4. return encoded data // TODO: this is incredibly inefficient... fix! - byte[] encoded=str.getBytes(StandardCharsets.UTF_8); + byte[] encoded=elm.toString().getBytes(StandardCharsets.UTF_8); ByteBuffer buf=ByteBuffer.allocate(2+encoded.length); buf.putShort((short)-1); buf.put(encoded); return buf.array(); } + public LazyElement decompressElement(byte[] data){ + return LazyElement.parse(decompress(data)); + } + + public LazyObject decompressObject(byte[] data){ + return new LazyObject(decompress(data)); + } + + public LazyArray decompressArray(byte[] data){ + return new LazyArray(decompress(data)); + } + public String decompress(byte[] data){ ByteBuffer buf=ByteBuffer.wrap(data); // 1. If it's a raw value, simply decode diff --git a/src/main/resources/version.properties b/src/main/resources/version.properties index a593273..646f8d0 100644 --- a/src/main/resources/version.properties +++ b/src/main/resources/version.properties @@ -1,4 +1,4 @@ -#Tue Aug 16 18:32:29 EDT 2016 +#Tue Aug 16 18:54:28 EDT 2016 BUILD_VERSION=1.1.0 -BUILD_DATE=2016-08-16T22\:32\:29Z -BUILD_NUMBER=556 +BUILD_DATE=2016-08-16T22\:54\:28Z +BUILD_NUMBER=566