1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Java 1.8.

This commit is contained in:
Douglas Crockford 2014-05-05 15:09:32 -07:00
parent 48d31b7f5c
commit a9a0762383
26 changed files with 747 additions and 819 deletions

View file

@ -28,8 +28,8 @@ package org.json.zip;
* JSONzip is a binary-encoded JSON dialect. It is designed to compress the
* messages in a session in bandwidth constrained applications, such as mobile.
*
* JSONzip is adaptive, so with each message seen, it should
* improve its compression. It minimizes JSON's overhead, reducing punctuation
* JSONzip is adaptive, so with each message seen, it should improve its
* compression. It minimizes JSON's overhead, reducing punctuation
* to a small number of bits. It uses Huffman encoding to reduce the average
* size of characters. It uses caches (or Keeps) to keep recently seen strings
* and values, so repetitive content (such as object keys) can be
@ -44,17 +44,9 @@ package org.json.zip;
* ADEQUATELY FOR PRODUCTION USE.
*
* @author JSON.org
* @version 2014-04-28
* @version 2014-05-03
*/
public abstract class JSONzip implements None, PostMortem {
/**
* Powers of 2.
*/
public static final int[] twos = {
1, 2, 4, 8, 16, 32, 64, 128, 256, 512,
1024, 2048, 4096, 8192, 16384, 32768, 65536
};
/**
* The characters in JSON numbers can be reduced to 4 bits each.
*/
@ -87,28 +79,11 @@ public abstract class JSONzip implements None, PostMortem {
*/
public static final int endOfNumber = bcd.length;
/**
* The maximum substring length when registering many. The registration of
* one substring may be longer.
*/
public static final int maxSubstringLength = 10;
/**
* The minimum substring length.
*/
public static final int minSubstringLength = 3;
/**
* The package supports tracing for debugging.
*/
public static final boolean probe = false;
/**
* The maximum number of substrings added to the substrings keep per
* string.
*/
public static final int substringLimit = 40;
/**
* The value code for an empty object.
*/
@ -155,62 +130,55 @@ public abstract class JSONzip implements None, PostMortem {
protected final Huff namehuff;
/**
* A place to keep the names (keys).
* A Huffman encoder for names extended bytes.
*/
protected final MapKeep namekeep;
protected final Huff namehuffext;
/**
* A place to keep the strings.
* A place to keep the names (keys).
*/
protected final MapKeep stringkeep;
protected final Keep namekeep;
/**
* A Huffman encoder for string values.
*/
protected final Huff substringhuff;
protected final Huff stringhuff;
/**
* A Huffman encoder for string values extended bytes.
*/
protected final Huff stringhuffext;
/**
* A place to keep the strings.
*/
protected final TrieKeep substringkeep;
protected final Keep stringkeep;
/**
* A place to keep the values.
*/
protected final MapKeep values;
protected final Keep valuekeep;
/**
* Initialize the data structures.
*/
protected JSONzip() {
this.namehuff = new Huff(end + 1);
this.namekeep = new MapKeep(9);
this.stringkeep = new MapKeep(11);
this.substringhuff = new Huff(end + 1);
this.substringkeep = new TrieKeep(12);
this.values = new MapKeep(10);
// Increase the weights of the ASCII letters, digits, and special characters
// because they are highly likely to occur more frequently. The weight of each
// character will increase as it is used. The Huffman encoder will tend to
// use fewer bits to encode heavier characters.
this.namehuff.tick(' ', '}');
this.namehuff.tick('a', 'z');
this.namehuff.tick(end);
this.namehuff.tick(end);
this.substringhuff.tick(' ', '}');
this.substringhuff.tick('a', 'z');
this.substringhuff.tick(end);
this.substringhuff.tick(end);
this.namehuffext = new Huff(end + 1);
this.namekeep = new Keep(9);
this.stringhuff = new Huff(end + 1);
this.stringhuffext = new Huff(end + 1);
this.stringkeep = new Keep(11);
this.valuekeep = new Keep(10);
}
/**
*
* Generate the Huffman tables.
*/
protected void begin() {
protected void generate() {
this.namehuff.generate();
this.substringhuff.generate();
this.stringhuff.generate();
this.stringhuffext.generate();
}
/**
@ -223,7 +191,7 @@ public abstract class JSONzip implements None, PostMortem {
/**
* Write an integer to the console.
*
* @param integer
* @param integer The integer to write to the log.
*/
static void log(int integer) {
log(integer + " ");
@ -233,8 +201,8 @@ public abstract class JSONzip implements None, PostMortem {
* Write two integers, separated by ':' to the console.
* The second integer is suppressed if it is 1.
*
* @param integer
* @param width
* @param integer The integer to write to the log.
* @param width The width of the integer in bits.
*/
static void log(int integer, int width) {
if (width == 1) {
@ -247,7 +215,7 @@ public abstract class JSONzip implements None, PostMortem {
/**
* Write a string to the console.
*
* @param string
* @param string The string to be written to the log.
*/
static void log(String string) {
System.out.print(string);
@ -256,8 +224,8 @@ public abstract class JSONzip implements None, PostMortem {
/**
* Write a character or its code to the console.
*
* @param integer
* @param width
* @param integer The charcode to be written to the log.
* @param width The width of the charcode in bits.
*/
static void logchar(int integer, int width) {
if (integer > ' ' && integer <= '}') {
@ -280,8 +248,7 @@ public abstract class JSONzip implements None, PostMortem {
return this.namehuff.postMortem(that.namehuff)
&& this.namekeep.postMortem(that.namekeep)
&& this.stringkeep.postMortem(that.stringkeep)
&& this.substringhuff.postMortem(that.substringhuff)
&& this.substringkeep.postMortem(that.substringkeep)
&& this.values.postMortem(that.values);
&& this.stringhuff.postMortem(that.stringhuff)
&& this.valuekeep.postMortem(that.valuekeep);
}
}