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 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 Detail

      • nil

        Json nil()
        Construct and return an object representing JSON null. Implementations are free to cache a return the same instance. The resulting value must return true from isNull() and null from getValue().
        Returns:
        The representation of a JSON null value.
      • bool

        Json bool​(boolean value)
        Construct and return a JSON boolean. The resulting value must return true from isBoolean() and the passed in parameter from getValue().
        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 return true from isString() and the passed in parameter from getValue().
        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 return true from isNumber() and the passed in parameter from getValue().
        Parameters:
        value - The numeric value.
        Returns:
        Json instance representing that value.
      • object

        Json object()
        Construct and return a JSON object. The resulting value must return true from isObject() and an implementation of java.util.Map from getValue().
        Returns:
        An empty JSON object.
      • array

        Json array()
        Construct and return a JSON object. The resulting value must return true from isArray() and an implementation of java.util.List from getValue().
        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 a Json instance.
        Parameters:
        anything - An arbitray Java object from which to construct a Json element.
        Returns:
        The newly constructed Json instance.