Skip to content

Commit

Permalink
Merge pull request #9 from doubledutch/feat-serialize-graph
Browse files Browse the repository at this point in the history
Compression functionality
  • Loading branch information
kasperjj authored Aug 16, 2016
2 parents af001d4 + eefca11 commit 5152fa6
Show file tree
Hide file tree
Showing 22 changed files with 1,895 additions and 272 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ jacoco {
reportsDir = file("$buildDir/jacoco")
}

sourceCompatibility = 1.7
targetCompatibility = 1.7

jacocoTestReport {
reports {
xml.enabled true
}
}

sourceCompatibility = 1.7
targetCompatibility = 1.7



defaultTasks = ['clean', 'jar']


Expand Down Expand Up @@ -52,7 +54,7 @@ artifacts {
}

check{
jacocoTestReport
dependsOn jacocoTestReport
}


Expand Down
145 changes: 56 additions & 89 deletions src/main/java/me/doubledutch/lazyjson/LazyArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
/**
* An array used to parse and inspect JSON data given in the form of a string.
*/
public class LazyArray{
private LazyToken root;
private char[] cbuf;

// Cache value for length
private int length=-1;

public class LazyArray extends LazyElement{
// Stored traversal location for fast in order traversals
private LazyToken selectToken=null;
private LazyNode selectToken=null;
private int selectInt=-1;

/**
Expand All @@ -23,32 +17,15 @@ public class LazyArray{
public LazyArray(String raw) throws LazyException{
LazyParser parser=new LazyParser(raw);
parser.tokenize();
if(parser.root.type!=LazyToken.ARRAY){
if(parser.root.type!=LazyNode.ARRAY){
throw new LazyException("JSON Array must start with [",0);
}
root=parser.root;
cbuf=parser.cbuf;
}

protected LazyArray(LazyToken root,char[] source){
this.root=root;
this.cbuf=source;
}

/**
* Returns the number of values in this array
*
* @return the number of values
*/
public int length(){
if(root.child==null){
return 0;
}
if(length>-1){
return length;
}
length=root.getChildCount();
return length;
protected LazyArray(LazyNode root,char[] source){
super(root,source);
}

/**
Expand All @@ -59,8 +36,8 @@ public int length(){
* @throws LazyException if the index is out of bounds
*/
public LazyArray getJSONArray(int index) throws LazyException{
LazyToken token=getValueToken(index);
if(token.type!=LazyToken.ARRAY)throw new LazyException("Requested value is not an array",token);
LazyNode token=getValueToken(index);
if(token.type!=LazyNode.ARRAY)throw new LazyException("Requested value is not an array",token);
return new LazyArray(token,cbuf);
}

Expand All @@ -72,10 +49,10 @@ public LazyArray getJSONArray(int index) throws LazyException{
* @throws LazyException if the index is out of bounds
*/
public LazyArray optJSONArray(int index) throws LazyException{
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return null;
if(token.type==LazyToken.VALUE_NULL)return null;
if(token.type!=LazyToken.ARRAY)throw new LazyException("Requested value is not an array",token);
if(token.type==LazyNode.VALUE_NULL)return null;
if(token.type!=LazyNode.ARRAY)throw new LazyException("Requested value is not an array",token);
return new LazyArray(token,cbuf);
}

Expand All @@ -87,8 +64,8 @@ public LazyArray optJSONArray(int index) throws LazyException{
* @throws LazyException if the index is out of bounds
*/
public LazyObject getJSONObject(int index) throws LazyException{
LazyToken token=getValueToken(index);
if(token.type!=LazyToken.OBJECT)throw new LazyException("Requested value is not an object",token);
LazyNode token=getValueToken(index);
if(token.type!=LazyNode.OBJECT)throw new LazyException("Requested value is not an object",token);
return new LazyObject(token,cbuf);
}

Expand All @@ -100,10 +77,10 @@ public LazyObject getJSONObject(int index) throws LazyException{
* @throws LazyException if the index is out of bounds
*/
public LazyObject optJSONObject(int index) throws LazyException{
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return null;
if(token.type==LazyToken.VALUE_NULL)return null;
if(token.type!=LazyToken.OBJECT)throw new LazyException("Requested value is not an object",token);
if(token.type==LazyNode.VALUE_NULL)return null;
if(token.type!=LazyNode.OBJECT)throw new LazyException("Requested value is not an object",token);
return new LazyObject(token,cbuf);
}

Expand All @@ -115,9 +92,9 @@ public LazyObject optJSONObject(int index) throws LazyException{
* @throws LazyException if the index is out of bounds
*/
public boolean getBoolean(int index){
LazyToken token=getValueToken(index);
if(token.type==LazyToken.VALUE_TRUE)return true;
if(token.type==LazyToken.VALUE_FALSE)return false;
LazyNode token=getValueToken(index);
if(token.type==LazyNode.VALUE_TRUE)return true;
if(token.type==LazyNode.VALUE_FALSE)return false;
throw new LazyException("Requested value is not a boolean",token);
}

Expand All @@ -129,11 +106,11 @@ public boolean getBoolean(int index){
* @throws LazyException if the index is out of bounds
*/
public boolean optBoolean(int index){
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return false;
if(token.type==LazyToken.VALUE_NULL)return false;
if(token.type==LazyToken.VALUE_TRUE)return true;
if(token.type==LazyToken.VALUE_FALSE)return false;
if(token.type==LazyNode.VALUE_NULL)return false;
if(token.type==LazyNode.VALUE_TRUE)return true;
if(token.type==LazyNode.VALUE_FALSE)return false;
throw new LazyException("Requested value is not a boolean",token);
}

Expand All @@ -146,11 +123,11 @@ public boolean optBoolean(int index){
* @throws LazyException if the index is out of bounds
*/
public boolean optBoolean(int index,boolean defaultValue){
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return defaultValue;
if(token.type==LazyToken.VALUE_NULL)return defaultValue;
if(token.type==LazyToken.VALUE_TRUE)return true;
if(token.type==LazyToken.VALUE_FALSE)return false;
if(token.type==LazyNode.VALUE_NULL)return defaultValue;
if(token.type==LazyNode.VALUE_TRUE)return true;
if(token.type==LazyNode.VALUE_FALSE)return false;
throw new LazyException("Requested value is not a boolean",token);
}

Expand All @@ -162,7 +139,7 @@ public boolean optBoolean(int index,boolean defaultValue){
* @throws LazyException if the index is out of bounds
*/
public String getString(int index) throws LazyException{
LazyToken token=getValueToken(index);
LazyNode token=getValueToken(index);
return token.getStringValue(cbuf);
}

Expand All @@ -174,9 +151,9 @@ public String getString(int index) throws LazyException{
* @throws LazyException if the index is out of bounds
*/
public String optString(int index){
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return null;
if(token.type==LazyToken.VALUE_NULL)return null;
if(token.type==LazyNode.VALUE_NULL)return null;
return token.getStringValue(cbuf);
}

Expand All @@ -189,9 +166,9 @@ public String optString(int index){
* @throws LazyException if the index is out of bounds
*/
public String optString(int index,String defaultValue){
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return defaultValue;
if(token.type==LazyToken.VALUE_NULL)return defaultValue;
if(token.type==LazyNode.VALUE_NULL)return defaultValue;
return token.getStringValue(cbuf);
}

Expand All @@ -203,7 +180,7 @@ public String optString(int index,String defaultValue){
* @throws LazyException if the index is out of bounds
*/
public int getInt(int index) throws LazyException{
LazyToken token=getValueToken(index);
LazyNode token=getValueToken(index);
return token.getIntValue(cbuf);
}

Expand All @@ -215,9 +192,9 @@ public int getInt(int index) throws LazyException{
* @throws LazyException if the index is out of bounds
*/
public int optInt(int index){
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return 0;
if(token.type==LazyToken.VALUE_NULL)return 0;
if(token.type==LazyNode.VALUE_NULL)return 0;
return token.getIntValue(cbuf);
}

Expand All @@ -230,9 +207,9 @@ public int optInt(int index){
* @throws LazyException if the index is out of bounds
*/
public int optInt(int index,int defaultValue){
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return defaultValue;
if(token.type==LazyToken.VALUE_NULL)return defaultValue;
if(token.type==LazyNode.VALUE_NULL)return defaultValue;
return token.getIntValue(cbuf);
}

Expand All @@ -244,7 +221,7 @@ public int optInt(int index,int defaultValue){
* @throws LazyException if the index is out of bounds
*/
public long getLong(int index) throws LazyException{
LazyToken token=getValueToken(index);
LazyNode token=getValueToken(index);
return token.getLongValue(cbuf);
}

Expand All @@ -256,9 +233,9 @@ public long getLong(int index) throws LazyException{
* @throws LazyException if the index is out of bounds
*/
public long optLong(int index){
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return 0l;
if(token.type==LazyToken.VALUE_NULL)return 0l;
if(token.type==LazyNode.VALUE_NULL)return 0l;
return token.getLongValue(cbuf);
}

Expand All @@ -271,9 +248,9 @@ public long optLong(int index){
* @throws LazyException if the index is out of bounds
*/
public long optLong(int index,long defaultValue){
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return defaultValue;
if(token.type==LazyToken.VALUE_NULL)return defaultValue;
if(token.type==LazyNode.VALUE_NULL)return defaultValue;
return token.getLongValue(cbuf);
}

Expand All @@ -285,7 +262,7 @@ public long optLong(int index,long defaultValue){
* @throws LazyException if the index is out of bounds
*/
public double getDouble(int index) throws LazyException{
LazyToken token=getValueToken(index);
LazyNode token=getValueToken(index);
return token.getDoubleValue(cbuf);
}

Expand All @@ -297,9 +274,9 @@ public double getDouble(int index) throws LazyException{
* @throws LazyException if the index is out of bounds
*/
public double optDouble(int index){
LazyToken token=getOptionalValueToken(index);
LazyNode token=getOptionalValueToken(index);
if(token==null)return 0.0;
if(token.type==LazyToken.VALUE_NULL)return 0.0;
if(token.type==LazyNode.VALUE_NULL)return 0.0;
return token.getDoubleValue(cbuf);
}

Expand All @@ -311,10 +288,10 @@ public double optDouble(int index){
* @return the value if it could be parsed as a string or the default value if there was no such value
* @throws LazyException if the index is out of bounds
*/
public double optDouble(int index,long defaultValue){
LazyToken token=getOptionalValueToken(index);
public double optDouble(int index,double defaultValue){
LazyNode token=getOptionalValueToken(index);
if(token==null)return defaultValue;
if(token.type==LazyToken.VALUE_NULL)return defaultValue;
if(token.type==LazyNode.VALUE_NULL)return defaultValue;
return token.getDoubleValue(cbuf);
}

Expand All @@ -326,8 +303,8 @@ public double optDouble(int index,long defaultValue){
* @throws LazyException if the index is out of bounds
*/
public boolean isNull(int index) throws LazyException{
LazyToken token=getValueToken(index);
if(token.type==LazyToken.VALUE_NULL)return true;
LazyNode token=getValueToken(index);
if(token.type==LazyNode.VALUE_NULL)return true;
return false;
}

Expand All @@ -346,10 +323,10 @@ public boolean isNull(int index) throws LazyException{
* @return the child for the given index
* @throws LazyException if the index is out of bounds
*/
private LazyToken getValueToken(int index) throws LazyException{
private LazyNode getValueToken(int index) throws LazyException{
if(index<0)throw new LazyException("Array undex can not be negative");
int num=0;
LazyToken child=root.child;
LazyNode child=root.child;
// If the value we are looking for is past our previous traversal point
// continue at the previous point
if(selectInt>-1 && index>=selectInt){
Expand Down Expand Up @@ -384,10 +361,10 @@ private LazyToken getValueToken(int index) throws LazyException{
* @return the child for the given index or null if the index does not exist
* @throws LazyException if the index is out of bounds
*/
private LazyToken getOptionalValueToken(int index) throws LazyException{
private LazyNode getOptionalValueToken(int index) throws LazyException{
if(index<0)throw new LazyException("Array undex can not be negative");
int num=0;
LazyToken child=root.child;
LazyNode child=root.child;
// If the value we are looking for is past our previous traversal point
// continue at the previous point
if(selectInt>-1 && index>=selectInt){
Expand All @@ -413,19 +390,9 @@ private LazyToken getOptionalValueToken(int index) throws LazyException{
* @param token the token for which to extract a string
* @return the string value of the given token
*/
private String getString(LazyToken token){
return token.getStringValue(cbuf);
}

/**
* Returns a raw string extracted from the source string that covers the
* start and end index of this object.
*
* @return as string representation of this object as given in the source string
*/
public String toString(){
return new String(cbuf,root.startIndex,root.endIndex-root.startIndex);
}
// private String getString(LazyNode token){
// return token.getStringValue(cbuf);
// }

/*
// For debug purposes only
Expand Down
Loading

0 comments on commit 5152fa6

Please sign in to comment.