Package aQute.lib.json
Class JSONCodec
java.lang.Object
aQute.lib.json.JSONCodec
This 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). See Encoder.to(),Encoder.to(File),
Encoder.to(OutputStream), Encoder.to(Appendable). To reset
the string output call Encoder.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
FieldsModifier and TypeFieldDescriptionprivate static BooleanHandlerprivate static ByteArrayHandlerprivate static CharacterHandlerprivate static CollectionHandlerprivate static FileHandlerprivate static final WeakHashMap<Type,Handler> (package private) booleanprivate static DateHandlerprivate static StringHandlerprivate static SpecialHandler(package private) static final Stringprivate static UUIDHandler -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddHandler(Type type, Handler handler) Add a new local handlerdec()Create a new Decoder with the state and appropriate API.(package private) Objectenc()Create a new Encoder with the state and appropriate API.(package private) void(package private) HandlergetHandler(Type type, Class<?> actual) This method figures out which handler should handle the type specific stuff.(package private) Class<?>getRawClass(Type type) private inthexDigit(int c) boolean(package private) voidparseArray(Collection<Object> list, Type componentType, Decoder r) private Number(package private) StringsetIgnorenull(boolean ignorenull) Ignore null values in output and input
-
Field Details
-
START_CHARACTERS
- See Also:
-
handlers
-
sh
-
bh
-
ch
-
dch
-
sph
-
sdh
-
fh
-
byteh
-
uuidh
-
ignorenull
boolean ignorenull -
localHandlers
-
-
Constructor Details
-
JSONCodec
public JSONCodec()
-
-
Method Details
-
enc
Create a new Encoder with the state and appropriate API.- Returns:
- an Encoder
-
dec
Create a new Decoder with the state and appropriate API.- Returns:
- a Decoder
-
encode
- Throws:
Exception
-
getHandler
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:
Exception
-
decode
- Throws:
Exception
-
parseString
- Throws:
Exception
-
hexDigit
- Throws:
EOFException
-
parseNumber
- Throws:
Exception
-
parseArray
- Throws:
Exception
-
getRawClass
-
setIgnorenull
Ignore null values in output and input- Parameters:
ignorenull-- Returns:
- this
-
isIgnorenull
public boolean isIgnorenull() -
addHandler
Add a new local handler
-