Interface Json.Factory
-
- All Known Implementing Classes:
Json.DefaultFactory
- Enclosing class:
- Json
public static interface Json.FactoryThis interface defines how
Jsoninstances are constructed. There is a default implementation for each kind ofJsonvalue, but you can provide your own implementation. For example, you might want a different representation of an object than a regularHashMap. Or you might want string comparison to be case insensitive.In addition, the
make(Object)method allows you plug-in your own mapping of arbitrary Java objects toJsoninstances. You might want to implement a Java Beans to JSON mapping or any other JSON serialization that makes sense in your project.To avoid implementing all methods in that interface, you can extend the
Json.DefaultFactorydefault implementation and simply overwrite the ones you're interested in.The factory implementation used by the
Jsonclasses is specified simply by calling theJson.setGlobalFactory(Factory)method. The factory is a static, global variable by default. If you need different factories in different areas of a single application, you may attach them to different threads of execution using theJson.attachFactory(Factory). Recall a separate copy of static variables is made per ClassLoader, so for example in a web application context, that global factory can be different for each web application (as Java web servers usually use a separate class loader per application). Thread-local factories are really a provision for special cases.- Author:
- Borislav Iordanov
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Jsonarray()Construct and return a JSON object.Jsonbool(boolean value)Construct and return a JSON boolean.Jsonmake(Object anything)Construct and return a JSON object.Jsonnil()Construct and return an object representing JSONnull.Jsonnumber(Number value)Construct and return a JSON number.Jsonobject()Construct and return a JSON object.Jsonstring(String value)Construct and return a JSON string.
-
-
-
Method Detail
-
nil
Json nil()
Construct and return an object representing JSONnull. Implementations are free to cache a return the same instance. The resulting value must returntruefromisNull()andnullfromgetValue().- Returns:
- The representation of a JSON
nullvalue.
-
bool
Json bool(boolean value)
Construct and return a JSON boolean. The resulting value must returntruefromisBoolean()and the passed in parameter fromgetValue().- Parameters:
value- The boolean value.- Returns:
- A JSON with
isBoolean() == true. Implementations are free to cache and return the same instance for true and false.
-
string
Json string(String value)
Construct and return a JSON string. The resulting value must returntruefromisString()and the passed in parameter fromgetValue().- Parameters:
value- The string to wrap as a JSON value.- Returns:
- A JSON element with the given string as a value.
-
number
Json number(Number value)
Construct and return a JSON number. The resulting value must returntruefromisNumber()and the passed in parameter fromgetValue().- Parameters:
value- The numeric value.- Returns:
- Json instance representing that value.
-
object
Json object()
Construct and return a JSON object. The resulting value must returntruefromisObject()and an implementation ofjava.util.MapfromgetValue().- Returns:
- An empty JSON object.
-
array
Json array()
Construct and return a JSON object. The resulting value must returntruefromisArray()and an implementation ofjava.util.ListfromgetValue().- Returns:
- An empty JSON array.
-
make
Json make(Object anything)
Construct and return a JSON object. The resulting value can be of any JSON type. The method is responsible for examining the type of its argument and performing an appropriate mapping to aJsoninstance.- Parameters:
anything- An arbitray Java object from which to construct aJsonelement.- Returns:
- The newly constructed
Jsoninstance.
-
-