Interface Json.Factory
-
- All Known Implementing Classes:
Json.DefaultFactory
- Enclosing class:
- Json
public static interface Json.Factory
This interface defines how
Json
instances are constructed. There is a default implementation for each kind ofJson
value, 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 toJson
instances. 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.DefaultFactory
default implementation and simply overwrite the ones you're interested in.The factory implementation used by the
Json
classes 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 Json
array()
Construct and return a JSON object.Json
bool(boolean value)
Construct and return a JSON boolean.Json
make(Object anything)
Construct and return a JSON object.Json
nil()
Construct and return an object representing JSONnull
.Json
number(Number value)
Construct and return a JSON number.Json
object()
Construct and return a JSON object.Json
raw(String value)
Json
string(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 returntrue
fromisNull()
andnull
fromgetValue()
.- Returns:
- The representation of a JSON
null
value.
-
bool
Json bool(boolean value)
Construct and return a JSON boolean. The resulting value must returntrue
fromisBoolean()
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 returntrue
fromisString()
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 returntrue
fromisNumber()
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 returntrue
fromisObject()
and an implementation ofjava.util.Map
fromgetValue()
.- Returns:
- An empty JSON object.
-
array
Json array()
Construct and return a JSON object. The resulting value must returntrue
fromisArray()
and an implementation ofjava.util.List
fromgetValue()
.- 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 aJson
instance.- Parameters:
anything
- An arbitray Java object from which to construct aJson
element.- Returns:
- The newly constructed
Json
instance.
-
-