public static interface Json.Factory
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.
Modifier and Type | Method and 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 JSON
null . |
Json |
number(Number value)
Construct and return a JSON number.
|
Json |
object()
Construct and return a JSON object.
|
Json |
string(String value)
Construct and return a JSON string.
|
Json nil()
null
. Implementations are
free to cache a return the same instance. The resulting value must return
true
from isNull()
and null
from
getValue()
.null
value.Json bool(boolean value)
true
from isBoolean()
and the passed
in parameter from getValue()
.value
- The boolean value.isBoolean() == true
. Implementations
are free to cache and return the same instance for true and false.Json string(String value)
true
from isString()
and the passed
in parameter from getValue()
.value
- The string to wrap as a JSON value.Json number(Number value)
true
from isNumber()
and the passed
in parameter from getValue()
.value
- The numeric value.Json object()
true
from isObject()
and an implementation
of java.util.Map
from getValue()
.Json array()
true
from isArray()
and an implementation
of java.util.List
from getValue()
.Json make(Object anything)
Json
instance.anything
- An arbitray Java object from which to construct a Json
element.Json
instance.Copyright © 2019 JBoss by Red Hat. All rights reserved.