Interface Json.Factory
- All Known Implementing Classes:
Json.DefaultFactory
- Enclosing class:
- Json
This interface defines how Json
instances are constructed. There is a default implementation for each
kind of Json
value, but you can provide your own implementation. For example, you might want a
different representation of an object than a regular HashMap
. 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 to
Json
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 the
Json.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 the Json.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
Modifier and TypeMethodDescriptionarray()
Construct and return a JSON object.bool
(boolean value) Construct and return a JSON boolean.Construct and return a JSON object.nil()
Construct and return an object representing JSONnull
.Construct and return a JSON number.object()
Construct and return a JSON object.Construct and return a JSON string.
-
Method Details
-
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
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
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.
-
raw
-
number
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
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.
-