Package aQute.lib.json
Class JSONCodec
- java.lang.Object
-
- aQute.lib.json.JSONCodec
-
public class JSONCodec extends java.lang.ObjectThis is a simple JSON Coder and Encoder that uses the Java type system to convert data objects to JSON and JSON to (type safe) Java objects. The conversion is very much driven by classes and their public fields. Generic information, when present is taken into account. Usage patterns to encode:JSONCoder codec = new JSONCodec(); // assert "1".equals( codec.enc().to().put(1).toString()); assert "[1,2,3]".equals( codec.enc().to().put(Arrays.asList(1,2,3).toString()); Map m = new HashMap(); m.put("a", "A"); assert "{\"a\":\"A\"}".equals( codec.enc().to().put(m).toString()); static class D { public int a; } D d = new D(); d.a = 41; assert "{\"a\":41}".equals( codec.enc().to().put(d).toString());It is possible to redirect the encoder to another output (default is a string). SeeEncoder.to(),Encoder.to(File),Encoder.to(OutputStream),Encoder.to(Appendable). To reset the string output callEncoder.to(). This Codec class can be used in a concurrent environment. The Decoders and Encoders, however, must only be used in a single thread. Will now use hex for encoding byte arrays
-
-
Field Summary
Fields Modifier and Type Field Description private static BooleanHandlerbhprivate static ByteArrayHandlerbytehprivate static CharacterHandlerchprivate static CollectionHandlerdchprivate static FileHandlerfhprivate static java.util.WeakHashMap<java.lang.reflect.Type,Handler>handlers(package private) booleanignorenull(package private) java.util.Map<java.lang.reflect.Type,Handler>localHandlersprivate static DateHandlersdhprivate static StringHandlershprivate static SpecialHandlersph(package private) static java.lang.StringSTART_CHARACTERSprivate static UUIDHandleruuidh
-
Constructor Summary
Constructors Constructor Description JSONCodec()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JSONCodecaddHandler(java.lang.reflect.Type type, Handler handler)Add a new local handlerDecoderdec()Create a new Decoder with the state and appropriate API.(package private) java.lang.Objectdecode(java.lang.reflect.Type type, Decoder isr)Encoderenc()Create a new Encoder with the state and appropriate API.(package private) voidencode(Encoder app, java.lang.Object object, java.lang.reflect.Type type, java.util.Map<java.lang.Object,java.lang.reflect.Type> visited)(package private) HandlergetHandler(java.lang.reflect.Type type, java.lang.Class<?> actual)This method figures out which handler should handle the type specific stuff.(package private) java.lang.Class<?>getRawClass(java.lang.reflect.Type type)private inthexDigit(int c)booleanisIgnorenull()(package private) voidparseArray(java.util.Collection<java.lang.Object> list, java.lang.reflect.Type componentType, Decoder r)private java.lang.NumberparseNumber(Decoder r)(package private) java.lang.StringparseString(Decoder r)JSONCodecsetIgnorenull(boolean ignorenull)Ignore null values in output and input
-
-
-
Field Detail
-
START_CHARACTERS
static final java.lang.String START_CHARACTERS
- See Also:
- Constant Field Values
-
handlers
private static final java.util.WeakHashMap<java.lang.reflect.Type,Handler> handlers
-
sh
private static StringHandler sh
-
bh
private static BooleanHandler bh
-
ch
private static CharacterHandler ch
-
dch
private static CollectionHandler dch
-
sph
private static SpecialHandler sph
-
sdh
private static DateHandler sdh
-
fh
private static FileHandler fh
-
byteh
private static ByteArrayHandler byteh
-
uuidh
private static UUIDHandler uuidh
-
ignorenull
boolean ignorenull
-
localHandlers
java.util.Map<java.lang.reflect.Type,Handler> localHandlers
-
-
Method Detail
-
enc
public Encoder enc()
Create a new Encoder with the state and appropriate API.- Returns:
- an Encoder
-
dec
public Decoder dec()
Create a new Decoder with the state and appropriate API.- Returns:
- a Decoder
-
encode
void encode(Encoder app, java.lang.Object object, java.lang.reflect.Type type, java.util.Map<java.lang.Object,java.lang.reflect.Type> visited) throws java.lang.Exception
- Throws:
java.lang.Exception
-
getHandler
Handler getHandler(java.lang.reflect.Type type, java.lang.Class<?> actual) throws java.lang.Exception
This method figures out which handler should handle the type specific stuff. It returns a handler for each type. If no appropriate handler exists, it will create one for the given type. There are actually quite a lot of handlers since Java is not very object oriented.- Parameters:
type-- Returns:
- a
Handlerappropriate fortype - Throws:
java.lang.Exception
-
decode
java.lang.Object decode(java.lang.reflect.Type type, Decoder isr) throws java.lang.Exception- Throws:
java.lang.Exception
-
parseString
java.lang.String parseString(Decoder r) throws java.lang.Exception
- Throws:
java.lang.Exception
-
hexDigit
private int hexDigit(int c) throws java.io.EOFException- Throws:
java.io.EOFException
-
parseNumber
private java.lang.Number parseNumber(Decoder r) throws java.lang.Exception
- Throws:
java.lang.Exception
-
parseArray
void parseArray(java.util.Collection<java.lang.Object> list, java.lang.reflect.Type componentType, Decoder r) throws java.lang.Exception- Throws:
java.lang.Exception
-
getRawClass
java.lang.Class<?> getRawClass(java.lang.reflect.Type type)
-
setIgnorenull
public JSONCodec setIgnorenull(boolean ignorenull)
Ignore null values in output and input- Parameters:
ignorenull-- Returns:
- this
-
isIgnorenull
public boolean isIgnorenull()
-
-