Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package aQute.lib.json;
import java.io.EOFException;
import java.io.File;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
/**
* 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 {@link Encoder#to()},{@link Encoder#to(File)},
* {@link Encoder#to(OutputStream)}, {@link Encoder#to(Appendable)}. To reset
* the string output call {@link 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
*/
public class JSONCodec {
final static Set keywords = Set.of("abstract", "assert", "boolean",
"break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else",
"enum", "exports", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "module", "native", "new", "package", "private", "protected",
"public", "requires", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this",
"throw", "throws", "transient", "try", "var", "void", "volatile", "while", "true", "false", "null", "_",
"record", "sealed", "non-sealed", "permits");
public static final String KEYWORD_SUFFIX = "__";
final static String START_CHARACTERSX = "[{\"-0123456789tfn";
final static String START_CHARACTERS_BAD = START_CHARACTERSX + "'TF";
// Handlers
private final static WeakHashMap handlers = new WeakHashMap<>();
private static StringHandler sh = new StringHandler();
private static BooleanHandler bh = new BooleanHandler();
private static CharacterHandler ch = new CharacterHandler();
private static CollectionHandler dch = new CollectionHandler(ArrayList.class,
Object.class);
private static SpecialHandler sph = new SpecialHandler(Pattern.class, null,
null);
private static DateHandler sdh = new DateHandler();
private static FileHandler fh = new FileHandler();
private static ByteArrayHandler byteh = new ByteArrayHandler();
private static UUIDHandler uuidh = new UUIDHandler();
final AtomicInteger fishy = new AtomicInteger();
boolean ignorenull;
Map localHandlers = new ConcurrentHashMap<>();
boolean promiscuous;
String startCharacters = START_CHARACTERSX;
/**
* Create a new Encoder with the state and appropriate API.
*
* @return an Encoder
*/
public Encoder enc() {
return new Encoder(this);
}
/**
* Create a new Decoder with the state and appropriate API.
*
* @return a Decoder
*/
public Decoder dec() {
return new Decoder(this);
}
/*
* Work horse encode methods, all encoding ends up here.
*/
void encode(Encoder app, Object object, Type type, Map