org.apache.struts2.json

## Class JSONUtil

- java.lang.Object
- - org.apache.struts2.json.JSONUtil

-     public class JSONUtil
      extends Object

  Wrapper for JSONWriter with some utility methods.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `JSONUtil.ClassVisitor` Realizes the visit(Class) method called by vistInterfaces for all encountered classes/interfaces

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static boolean` | `CACHE_BEAN_INFO_DEFAULT`
    `static String` | `REGEXP_PATTERN`
    `static String` | `RFC3339_FORMAT`
    `static String` | `WILDCARD_PATTERN`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `JSONUtil()`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static Set<String>` | `asSet(String commaDelim)`
    `static Object` | `deserialize(Reader reader)` Deserializes a object from JSON
    `static Object` | `deserialize(String json)` Deserializes a object from JSON
    `static boolean` | `isGzipInRequest(javax.servlet.http.HttpServletRequest request)`
    `static Method[]` | `listSMDMethods(Class clazz, boolean ignoreInterfaces)` List visible methods carrying the @SMDMethod annotation
    `static List<Pattern>` | `processIncludePatterns(Set<String> includePatterns, String type)`
    `String` | `serialize(Object object, boolean cacheBeanInfo)` Serializes an object into JSON.
    `String` | `serialize(Object object, Collection<Pattern> excludeProperties, Collection<Pattern> includeProperties, boolean ignoreHierarchy, boolean excludeNullProperties)` Serializes an object into JSON, excluding any properties matching any of the regular expressions in the given collection.
    `String` | `serialize(Object object, Collection<Pattern> excludeProperties, Collection<Pattern> includeProperties, boolean ignoreHierarchy, boolean excludeNullProperties, boolean cacheBeanInfo)` Serializes an object into JSON, excluding any properties matching any of the regular expressions in the given collection.
    `String` | `serialize(Object object, Collection<Pattern> excludeProperties, Collection<Pattern> includeProperties, boolean ignoreHierarchy, boolean enumAsBean, boolean excludeNullProperties, String defaultDateFormat)` Serializes an object into JSON, excluding any properties matching any of the regular expressions in the given collection.
    `String` | `serialize(Object object, Collection<Pattern> excludeProperties, Collection<Pattern> includeProperties, boolean ignoreHierarchy, boolean enumAsBean, boolean excludeNullProperties, String defaultDateFormat, boolean cacheBeanInfo)` Serializes an object into JSON, excluding any properties matching any of the regular expressions in the given collection.
    `void` | `serialize(Writer writer, Object object)` Serializes an object into JSON to the given writer.
    `void` | `serialize(Writer writer, Object object, boolean cacheBeanInfo)` Serializes an object into JSON to the given writer.
    `void` | `serialize(Writer writer, Object object, Collection<Pattern> excludeProperties, Collection<Pattern> includeProperties, boolean excludeNullProperties)` Serializes an object into JSON to the given writer, excluding any properties matching any of the regular expressions in the given collection.
    `void` | `serialize(Writer writer, Object object, Collection<Pattern> excludeProperties, Collection<Pattern> includeProperties, boolean excludeNullProperties, boolean cacheBeanInfo)` Serializes an object into JSON to the given writer, excluding any properties matching any of the regular expressions in the given collection.
    `void` | `setContainer(Container container)`
    `void` | `setWriter(JSONWriter writer)`
    `static boolean` | `visitInterfaces(Class aClass, JSONUtil.ClassVisitor visitor)` Visit all the interfaces realized by the specified object, its superclasses and its interfaces Visitation is performed in the following order: aClass aClass' interfaces the interface's superclasses (interfaces) aClass' superclass superclass' interfaces superclass' interface's superclasses (interfaces) super-superclass and so on The Object base class is base excluded.
    `static void` | `writeJSONToResponse(SerializationParams serializationParams)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### RFC3339_FORMAT

          public static final String RFC3339_FORMAT

      See Also:
        Constant Field Values

    - #### CACHE_BEAN_INFO_DEFAULT

          public static final boolean CACHE_BEAN_INFO_DEFAULT

      See Also:
        Constant Field Values

    - #### REGEXP_PATTERN

          public static final String REGEXP_PATTERN

      See Also:
        Constant Field Values

    - #### WILDCARD_PATTERN

          public static final String WILDCARD_PATTERN

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### JSONUtil

          public JSONUtil()

  - ### Method Detail

    - #### setWriter

          public void setWriter(JSONWriter writer)

    - #### setContainer

          public void setContainer(Container container)

    - #### serialize

          public String serialize(Object object,
                                  boolean cacheBeanInfo)
                           throws JSONException

      Serializes an object into JSON.
      Parameters:
        `object` - to be serialized
        `cacheBeanInfo` - Specifies whether to cache bean info in the JSONWriter
      Returns:
        JSON string
      Throws:
        `JSONException` - in case of error during serialize

    - #### serialize

          public String serialize(Object object,
                                  Collection<Pattern> excludeProperties,
                                  Collection<Pattern> includeProperties,
                                  boolean ignoreHierarchy,
                                  boolean excludeNullProperties)
                           throws JSONException

      Serializes an object into JSON, excluding any properties matching any of the regular expressions in the given collection.
      Parameters:
        `object` - to be serialized
        `excludeProperties` - Patterns matching properties to exclude
        `includeProperties` - Patterns matching properties to include
        `ignoreHierarchy` - whether to ignore properties defined on base classes of the root object
        `excludeNullProperties` - enable/disable excluding of null properties
      Returns:
        JSON string
      Throws:
        `JSONException` - in case of error during serialize

    - #### serialize

          public String serialize(Object object,
                                  Collection<Pattern> excludeProperties,
                                  Collection<Pattern> includeProperties,
                                  boolean ignoreHierarchy,
                                  boolean excludeNullProperties,
                                  boolean cacheBeanInfo)
                           throws JSONException

      Serializes an object into JSON, excluding any properties matching any of the regular expressions in the given collection.
      Parameters:
        `object` - to be serialized
        `excludeProperties` - Patterns matching properties to exclude
        `includeProperties` - Patterns matching properties to include
        `ignoreHierarchy` - whether to ignore properties defined on base classes of the root object
        `excludeNullProperties` - enable/disable excluding of null properties
        `cacheBeanInfo` - Specifies whether to cache bean info in the JSONWriter
      Returns:
        JSON string
      Throws:
        `JSONException` - in case of error during serialize

    - #### serialize

          public String serialize(Object object,
                                  Collection<Pattern> excludeProperties,
                                  Collection<Pattern> includeProperties,
                                  boolean ignoreHierarchy,
                                  boolean enumAsBean,
                                  boolean excludeNullProperties,
                                  String defaultDateFormat)
                           throws JSONException

      Serializes an object into JSON, excluding any properties matching any of the regular expressions in the given collection.
      Parameters:
        `object` - to be serialized
        `excludeProperties` - Patterns matching properties to exclude
        `includeProperties` - Patterns matching properties to include
        `ignoreHierarchy` - whether to ignore properties defined on base classes of the root object
        `enumAsBean` - whether to serialized enums a Bean or name=value pair
        `excludeNullProperties` - enable/disable excluding of null properties
        `defaultDateFormat` - date format used to serialize dates
      Returns:
        JSON string
      Throws:
        `JSONException` - in case of error during serialize

    - #### serialize

          public String serialize(Object object,
                                  Collection<Pattern> excludeProperties,
                                  Collection<Pattern> includeProperties,
                                  boolean ignoreHierarchy,
                                  boolean enumAsBean,
                                  boolean excludeNullProperties,
                                  String defaultDateFormat,
                                  boolean cacheBeanInfo)
                           throws JSONException

      Serializes an object into JSON, excluding any properties matching any of the regular expressions in the given collection.
      Parameters:
        `object` - to be serialized
        `excludeProperties` - Patterns matching properties to exclude
        `includeProperties` - Patterns matching properties to include
        `ignoreHierarchy` - whether to ignore properties defined on base classes of the root object
        `enumAsBean` - whether to serialized enums a Bean or name=value pair
        `excludeNullProperties` - enable/disable excluding of null properties
        `defaultDateFormat` - date format used to serialize dates
        `cacheBeanInfo` - Specifies whether to cache bean info in the JSONWriter
      Returns:
        JSON string
      Throws:
        `JSONException` - in case of error during serialize

    - #### serialize

          public void serialize(Writer writer,
                                Object object)
                         throws IOException,
                                JSONException

      Serializes an object into JSON to the given writer.
      Parameters:
        `writer` - Writer to serialize the object to
        `object` - object to be serialized
      Throws:
        `IOException` - in case of IO errors
        `JSONException` - in case of error during serialize

    - #### serialize

          public void serialize(Writer writer,
                                Object object,
                                boolean cacheBeanInfo)
                         throws IOException,
                                JSONException

      Serializes an object into JSON to the given writer.
      Parameters:
        `writer` - Writer to serialize the object to
        `object` - object to be serialized
        `cacheBeanInfo` - Specifies whether to cache bean info in the JSONWriter
      Throws:
        `IOException` - in case of IO errors
        `JSONException` - in case of error during serialize

    - #### serialize

          public void serialize(Writer writer,
                                Object object,
                                Collection<Pattern> excludeProperties,
                                Collection<Pattern> includeProperties,
                                boolean excludeNullProperties)
                         throws IOException,
                                JSONException

      Serializes an object into JSON to the given writer, excluding any properties matching any of the regular expressions in the given collection.
      Parameters:
        `writer` - Writer to serialize the object to
        `object` - object to be serialized
        `excludeProperties` - Patterns matching properties to ignore
        `includeProperties` - Patterns matching properties to include
        `excludeNullProperties` - enable/disable excluding of null properties
      Throws:
        `IOException` - in case of IO errors
        `JSONException` - in case of error during serialize

    - #### serialize

          public void serialize(Writer writer,
                                Object object,
                                Collection<Pattern> excludeProperties,
                                Collection<Pattern> includeProperties,
                                boolean excludeNullProperties,
                                boolean cacheBeanInfo)
                         throws IOException,
                                JSONException

      Serializes an object into JSON to the given writer, excluding any properties matching any of the regular expressions in the given collection.
      Parameters:
        `writer` - Writer to serialize the object to
        `object` - object to be serialized
        `excludeProperties` - Patterns matching properties to ignore
        `includeProperties` - Patterns matching properties to include
        `excludeNullProperties` - enable/disable excluding of null properties
        `cacheBeanInfo` - Specifies whether to cache bean info in the JSONWriter
      Throws:
        `IOException` - in case of IO errors
        `JSONException` - in case of error during serialize

    - #### deserialize

          public static Object deserialize(String json)
                                    throws JSONException

      Deserializes a object from JSON
      Parameters:
        `json` - string in JSON
      Returns:
        desrialized object
      Throws:
        `JSONException` - in case of error during serialize

    - #### deserialize

          public static Object deserialize(Reader reader)
                                    throws JSONException

      Deserializes a object from JSON
      Parameters:
        `reader` - Reader to read a JSON string from
      Returns:
        deserialized object
      Throws:
        `JSONException` - when IOException happens

    - #### writeJSONToResponse

          public static void writeJSONToResponse(SerializationParams serializationParams)
                                          throws IOException

      Throws:
        `IOException`

    - #### asSet

          public static Set<String> asSet(String commaDelim)

    - #### listSMDMethods

          public static Method[] listSMDMethods(Class clazz,
                                                boolean ignoreInterfaces)

      List visible methods carrying the @SMDMethod annotation
      Parameters:
        `clazz` - class
        `ignoreInterfaces` - if true, only the methods of the class are examined. If false, annotations on every interfaces' methods are examined.
      Returns:
        array of SMD methods

    - #### visitInterfaces

          public static boolean visitInterfaces(Class aClass,
                                                JSONUtil.ClassVisitor visitor)

      Visit all the interfaces realized by the specified object, its superclasses and its interfaces
      Visitation is performed in the following order: aClass aClass' interfaces the interface's superclasses (interfaces) aClass' superclass superclass' interfaces superclass' interface's superclasses (interfaces) super-superclass and so on
      The Object base class is base excluded. Classes/interfaces are only visited once each
      Parameters:
        `aClass` - the class to start recursing upwards from
        `visitor` - this vistor is called for each class/interface encountered
      Returns:
        true if all classes/interfaces were visited, false if it was exited early as specified by a ClassVisitor result

    - #### isGzipInRequest

          public static boolean isGzipInRequest(javax.servlet.http.HttpServletRequest request)

    - #### processIncludePatterns

          public static List<Pattern> processIncludePatterns(Set<String> includePatterns,
                                                             String type)

Copyright © 2000–2025 Apache Software Foundation. All rights reserved.