{#========================================== Spincast Jackson Json plugin ==========================================#} {% extends "../../layout.html" %} {% block sectionClasses %}plugins plugins-spincast-jackson-json{% endblock %} {% block meta_title %}Plugins - Spincast Jackson Json{% endblock %} {% block meta_description %}Spincast Jackson Json plugin provides Json functionalities using Jackson.{% endblock %} {% block scripts %} {% endblock %} {% block body %}

Overview

The Spincast Jackson Json plugin provides Json functionalities using Jackson. It contains an implementation of the IJsonManager interface.

Most of the time, the IJsonManager interface is used directly from the request context via the json() method or indirectly via some methods on the response() add-on.

For example:

{% verbatim %}

public class AppController {

    public void myHandler(IDefaultRequestContext context) {

        // Create a Json object, using the "json()" add-on
        IJsonObject jsonObj = context.json().create();

        // Send an object as Json, using the "response()" add-on
        context.response().sendJson(jsonObj);
    }
}
{% endverbatim %}

You can also directly inject the IJsonManager instance where you need it in your application.

Installation

If you use the spincast-default artifact, this plugin is already installed so you have nothing more to do!

If you start from scratch using the spincast-core artifact, you can use the plugin by adding this artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-jackson-json</artifactId>
    <version>{{spincastLatestStableVersion}}</version>
</dependency>

You then install the plugin's Guice module, by passing it to the Guice.createInjector(...) method:

Injector guice = Guice.createInjector(
        new SpincastCoreGuiceModule(args),
        new SpincastJacksonJsonPluginGuiceModule(IAppRequestContext.class)
        // other modules...
        );

... or by using the install(...) method from your custom Guice module:

public class AppModule extends SpincastCoreGuiceModule {

    @Override
    protected void configure() {
        super.configure();
        install(new SpincastJacksonJsonPluginGuiceModule(getRequestContextType()));
        // other modules...
    }
    
    // ...
}

The IJsonManager interface

Methods :

  • IJsonObject create()
    Creates an empty JsonObject
  • IJsonObject create(String jsonString)
    Creates a JsonObject from a Json
    String.
  • IJsonObject create(InputStream inputStream)
    Creates a JsonObject from an inputStream.
  • IJsonArray createArray()
    Creates an empty JsonArray.
  • String toJsonString(Object obj)
    Gets the Json String representation of the specified object.
  • String toJsonString(Object obj, boolean pretty)
    Gets the Json String representation of the specified object.
    @param pretty if true, the generated String will be formatted.
  • Map<String, Object> fromJsonStringToMap(String jsonString)
    Creates a Map<String, Object> from a Json
    String.
  • Map<String, Object> fromJsonInputStreamToMap(InputStream inputStream)
    Creates a Map<String, Object> from a Json inputStream.
  • <T> T fromJsonString(String jsonString, Class<T> clazz)
    Creates an instance of the specified T type from a Json String.
  • <T> T fromJsonInputStream(InputStream inputStream, Class<T> clazz)
    Creates an instance of the specified T type from a Json inputStream.
  • Date parseDateFromJson(String str)
    Converts a Json date to a Java UTC date.

{% endblock %}