se.l4.commons.serialization.format.JsonOutput Maven / Gradle / Ivy
The newest version!
package se.l4.commons.serialization.format;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
/**
* Streamer that outputs JSON.
*
* @author andreas
*
*/
public class JsonOutput
implements StreamingOutput
{
private static final int HEX_MASK = (1 << 4) - 1;
private static final char[] DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
private final static char[] BASE64 = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};
private static final int LEVELS = 20;
protected final Writer writer;
private final boolean beautify;
private boolean[] lists;
private boolean[] hasData;
private int level;
private final char[] encoded;
/**
* Create a JSON streamer that will write to the given output.
*
* @param out
*/
public JsonOutput(OutputStream out)
{
this(out, false);
}
/**
* Create a JSON streamer that will write to the given output, optionally
* with beautification of the generated JSON.
*
* @param out
* @param beautify
*/
public JsonOutput(OutputStream out, boolean beautify)
{
this(new OutputStreamWriter(out, StandardCharsets.UTF_8), beautify);
}
/**
* Create a JSON streamer that will write to the given output.
*
* @param out
*/
public JsonOutput(Writer writer)
{
this(writer, false);
}
/**
* Create a JSON streamer that will write to the given output, optionally
* with beautification of the generated JSON.
*
* @param out
* @param beautify
*/
public JsonOutput(Writer writer, boolean beautify)
{
this.writer = writer;
this.beautify = beautify;
lists = new boolean[LEVELS];
hasData = new boolean[LEVELS];
encoded = new char[4];
}
@Override
public void close()
throws IOException
{
flush();
writer.close();
}
/**
* Escape and write the given string.
*
* @param in
* @throws IOException
*/
private void writeEscaped(String in)
throws IOException
{
for(int i=0, n=in.length(); i>>= 4;
}
while (v != 0);
for(int j=0; j 0)
{
writer.write('\n');
for(int i=0; i 0 ? (data[pos] << 24) >>> 8 : 0) |
(len > 1 ? (data[pos+1] << 24) >>> 16 : 0) |
(len > 2 ? (data[pos+2] << 24) >>> 24 : 0);
switch(len)
{
case 3:
writer.write(chars[loc >>> 18]);
writer.write(chars[(loc >>> 12) & 0x3f]);
writer.write(chars[(loc >>> 6) & 0x3f]);
writer.write(chars[loc & 0x3f]);
break;
case 2:
writer.write(chars[loc >>> 18]);
writer.write(chars[(loc >>> 12) & 0x3f]);
writer.write(chars[(loc >>> 6) & 0x3f]);
writer.write('=');
break;
case 1:
writer.write(chars[loc >>> 18]);
writer.write(chars[(loc >>> 12) & 0x3f]);
writer.write('=');
writer.write('=');
}
}
@Override
public void writeNull(String name)
throws IOException
{
write(name, (String) null);
}
@Override
public void flush() throws IOException
{
writer.flush();
}
}