Skip to main content
Kinetic Community

JSON Helper

The Json Helper contains many useful methods for using and manipulating text within a bundle. All of the methods are designed to be “null safe”, which means that they will not throw any NullPointerException errors if null values are passed in as parameters. See the examples for each method to determine the result of passing in a null parameter.

There are two classes that share the same methods:

  • com.kineticdata.bundles.JsonHelper must be initialized in the bundle/initialization.jspf file and can then be used in JSTL expressions.
  • com.kineticdata.bundles.Json is a static class that can be used in JSP scriptlets and declarations (this is primarily useful in custom bundle helper declarations).

Getting Started

Configuration

In order to use the text helper in a bundle, it must be loaded in the bundle/initialization.jspf file as shown below

<% request.setAttribute("json", new com.kineticdata.bundles.JsonHelper()); %>

For a complete bundle/initialization.jspf example, see the default bundle implementation athttps://github.com/kineticdata/request-ce-bundle-default.

Methods

parse(String jsonString)

Parses the specified json string and returns a corresponding Java object. JSON Arrays are represented as java.util.ArrayListinstances and JSON Objetcts are represented as java.util.LinkedHashMap instances. This method will also accept JSON primitives (null, booleans, numbers, and strings; JSON does not have a date or time type.).

json.parse("null");                                       // null
json.parse("true");                                       // true
json.parse("12.3");                                       // 12.3
json.parse("\"abc\"");                                    // "ABC"
json.parse("\"1970-01-01T00:00:00Z\"");                   // "1970-01-01T00:00:00Z"
json.parse("[\"a\", \"b\", \"c\"]");                      // ["a","b","c']
json.parse("{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\"}"); // {"a":"A","b":"B","c":"C"}

toString(Object object)

Converts the specified object into a JSON string. This method will accept Java primitives (null, booleans, numbers, strings, and dates/times) as well as java.util.List and java.util.Map instances.

json.toString(null);                      // null
json.toString(true);                      // true
json.toString(12.3);                      // "12.3"
json.toString('ABC');                     // "\"ABC\""
json.toString(new Date(0));               // "\"1970-01-01T00:00:00Z\""
json.toString(['a','b','c']);             // "[\"a\", \"b\", \"c\"]"
json.toString({'a':'A','b':'B','c':'C'}); // "{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\"}"

Related