public class JSONContext
extends java.lang.Object
You can use the default context - by a call to JSONContext.getInstance() - , or create a new one. Default context use a default set or converters, which should cover most usages, and use a "nice" value of true : marshaller and unmarshaller will have a tolerance to error in source code - meaning request to parse an unknown field, bad setter, etc. note an error into a js object structure still will fail, whatever the nice parameter is ( open { or [ etc...)
For internatialization you can set the local with a call to JSONContext.SetLocal. note this is a global parameter, shared by any context.
Context is thread safe. You can use the same context to marshal or unmarshal different objects at the same time.
If you don't need specific converter, you should use JSonContext.GetInstance
The default converter list cover :
int, float, long, boolean, double, char, short, [], String, Long, Integer, Double, Float, Boolean, Date, Calendar, Character, Short, List, Collection, Iterable, ArrayList, Set, TreeSet, SortedSet, NavigableSet, Queue, Deque, LinkedList, ArrayDeque, HashSet, PriorityQueue, Stack, Vector, Locale, Currency, bean, pojo, and conbinaison of such
A converter should implement either the ObjectConverterInterface or (and) the SerieConverterInterface than you extend the default converter list by creating a newly context for example :
new JSONContext(true, MyClass.class.getcanonicalName(), myClassConverter.class)
Note there is no priority list for converter and check is done on a strict fields type / classes name comparaison. For example if MyClass extend List, and a field is of type MyClass, you still have to add a converter. If the field is of type List, default converter might be enough but warning, you wont be able to cast it as MyClass (ListConverter create an ArrayList).
this library use - and include - part of code from json.org<.p>
Convert json source into some java object :
Convert some java object :Unmarshaller bp = JSONContext.getInstance().getUnmarshaller(); String str = (String) bp.unmarshal(String.class, "my json string"); Integer val = (Integer) bp.unmarshal(Integer.class, "666"); int[] vali = (int[]) bp.unmarshal(int[].class, "[1,2,3,4]"); String data = "[{val:\"first\"},{val:\"second\"},{val:\"third\"}]"; SimpleBean[] array = (SimpleBean[]) bp.unmarshal(SimpleBean[].class, data);
see sample project for more usagesMarshaller bp = JSONContext.getInstance().getMarshaller(); Double d = Math.PI; String answer = bp.marshal(d); SimpleBean bean = new SimpleBean(); // init bean values ... String answer = bp.marshal(bean)
Marshaller
,
Unmarshaller
,
ObjectConverterInterface
,
SerieConverterInterface
,
Discard
Modifier and Type | Field and Description |
---|---|
protected java.util.HashMap<java.lang.String,java.lang.Class> |
converter |
protected static java.util.Locale |
locale |
protected Marshaller |
marshaller |
protected boolean |
nice |
protected Unmarshaller |
unmarshaller |
Modifier | Constructor and Description |
---|---|
protected |
JSONContext()
default construtor with default parameters, use getInstance instead.
|
|
JSONContext(boolean nice,
java.util.Map<java.lang.String,java.lang.Class> extendConverter)
create a new context, adding some more converters.
|
|
JSONContext(boolean nice,
java.lang.String canonicalName,
java.lang.Class converterClass)
Create a new context just adding a single more converter, convenience contructor.
|
Modifier and Type | Method and Description |
---|---|
java.util.HashMap<java.lang.String,java.lang.Class> |
getConverter()
return the list of all converters this context knows
|
static JSONContext |
getInstance()
return default context with default converters list and nice = true
|
static java.util.Locale |
getLocale()
return Locale ANY instances will use for classes such currency, date, calendar...
|
Marshaller |
getMarshaller()
Marshaller is responsible for serializing Java code into js object
|
Unmarshaller |
getUnmarshaller()
Unmarshaller are responsible of parsing js object/data into java class
|
boolean |
isNice()
return the nice level for this converter.
|
static void |
setLocale(java.util.Locale local)
set locale ANY instances will use. default to system locale
|
protected static java.util.Locale locale
protected final java.util.HashMap<java.lang.String,java.lang.Class> converter
protected final boolean nice
protected Marshaller marshaller
protected Unmarshaller unmarshaller
public JSONContext(boolean nice, java.util.Map<java.lang.String,java.lang.Class> extendConverter) throws java.lang.Exception
nice
- : with false, the marshaller / unmarshaller will perform strict convertion, stopping on any error. With true, it will have a tolerance to error.extendConverter
- java.lang.Exception
org.vostok.json.converter
public JSONContext(boolean nice, java.lang.String canonicalName, java.lang.Class converterClass) throws java.lang.Exception
nice
- : with false will perform a strict convertioncanonicalName
- converterClass
- java.lang.Exception
protected JSONContext()
public static JSONContext getInstance()
public static java.util.Locale getLocale()
public static void setLocale(java.util.Locale local)
local
- public boolean isNice()
public java.util.HashMap<java.lang.String,java.lang.Class> getConverter()
public Marshaller getMarshaller()
Marshaller
public Unmarshaller getUnmarshaller()
Unmarshaller