Class GroovyJSON


  • public final class GroovyJSON
    extends Object
    Since:
    Intrexx 7.0.
    • Constructor Detail

      • GroovyJSON

        public GroovyJSON()
    • Method Detail

      • createJSONObject

        public GroovyJSONObject createJSONObject()
        Create a new JSON object.

        Note: Since Groovy maps can replace such JSON objects in most cases, this method is rarely required.

        Returns:
        The new JSON object.
      • createJSONObject

        public GroovyJSONObject createJSONObject​(Map<?,​?> p_json)
        Create a new JSON object from the given map.

        Note: Since Groovy maps can replace such JSON objects in most cases, this method is rarely required.

        Parameters:
        p_json - The map to construct the JSON object.
        Returns:
        The new JSON object.
      • parse

        public Object parse​(CharSequence p_chseq)
        Parse the given character sequence as JSON.
        Parameters:
        p_chseq - The character sequence.
        See Also:
        parse(Reader)
      • parse

        public Object parse​(Reader p_reader)
        Parse the given input as JSON.

        Note: Some of the objects returned might be implementing methods like toJSONString(), or writeJSONString(Writer). Do not call these methods in scripts. They will be removed in future versions of this API.

        Parameters:
        p_reader - The input stream.
        Returns:
        An instance of the following:
        • GroovyJSONObject,
        • java.util.List,
        • java.lang.String,
        • java.lang.Number,
        • java.lang.Boolean, or
        • null.
      • httpPost

        public Object httpPost​(Map<String,​Object> p_params)
                        throws IOException,
                               URISyntaxException
        Send an HTTP or HTTPS request and parse the response as JSON.

        Valid parameters:

        • url (required) - The URL, either a java.net.URL, or a java.net.URI or any object that can be converted to a valid URL string.
        • multipartFormData a map containing the multipart content bodies,
        • optional properties as defined for GroovyHttpRequestWrapper.
        Parameters:
        p_params - The parameters.
        Throws:
        IOException
        URISyntaxException
      • stringify

        public String stringify​(Object p_obj)
        Convert an object to its JSON representation.

        The method mimics the JSON.stringify function as defined in the ECMAScript Language Scecification, 5.1 edition.

        Parameters:
        p_obj - The object.
        Returns:
        The object's JSON representation.
      • createJSONContentBody

        public IGroovyJSONContentBody createJSONContentBody​(Object p_obj)
        Create a JSON content bodyto be used as part of a multipart/form-data request.
        Parameters:
        p_obj - The object to create the JSON body from.
        Returns:
        A JSON content body containing the given data.
      • createTextContentBody

        public IGroovyJSONContentBody createTextContentBody​(CharSequence p_chseq)
        Create a text content body to be used as part of a multipart/form-data request.
        Parameters:
        p_chseq - The text data.
        Returns:
        The content body.
      • createTextContentBody

        public IGroovyJSONContentBody createTextContentBody​(CharSequence p_chseq,
                                                            String p_strMimeType)
        Create a text content body to be used as part of a multipart/form-data request.
        Parameters:
        p_chseq - The text data.
        p_strMimeType - The MIME type of the content body.
        Returns:
        The content body.
      • createTextContentBody

        public IGroovyJSONContentBody createTextContentBody​(CharSequence p_chseq,
                                                            String p_strMimeType,
                                                            String p_strCharset)
        Create a text content body to be used as part of a multipart/form-data request.
        Parameters:
        p_chseq - The text data.
        p_strMimeType - The MIME type of the content body.
        p_strCharset - The charset to be used.
        Returns:
        The content body.
      • createFileContentBody

        public IGroovyJSONContentBody createFileContentBody​(Object p_obj)
        Create a file content body to be used as part of a multipart/form-data request.

        If an Intrexx file descriptor is given, the MIME type and the file name are taken from the descriptor, in all other cases the original file name is used and the MIME type is derived rom the file extension.

        Parameters:
        p_obj - The object that represents the file, either, a string, or a java.io.File, or a java.nio.file.Path, or an Intrexx file descriptor.
        Returns:
        The content body.
      • createFileContentBody

        public IGroovyJSONContentBody createFileContentBody​(Object p_obj,
                                                            String p_strFileName,
                                                            String p_strMimeType)
        Create a file content body to be used as part of a multipart/form-data request.
        Parameters:
        p_obj - The object that represents the file, either, a string, or a java.io.File, or a java.nio.file.Path, or an Intrexx file descriptor.
        p_strFileName - The file name to be used.
        p_strMimeType - The MIME type of the content body.
        Returns:
        The content body.
      • createFileContentBody

        public IGroovyJSONContentBody createFileContentBody​(Object p_obj,
                                                            String p_strFileName,
                                                            String p_strMimeType,
                                                            String p_strCharset)
        Create a file content body to be used as part of a multipart/form-data request.
        Parameters:
        p_obj - The object that represents the file, either, a string, or a java.io.File, or a java.nio.file.Path, or an Intrexx file descriptor.
        p_strFileName - The file name to be used.
        p_strMimeType - The MIME type of the content body.
        p_strCharset - The charset to be declared in the Content-Type header.
        Returns:
        The content body.
      • writeJSON

        public Appendable writeJSON​(Appendable p_appendable,
                                    Object p_obj)
                             throws IOException
        Convert an object to its JSON representation.

        The method mimics the JSON.stringify function as defined in the ECMAScript Language Scecification, 5.1 edition.

        Parameters:
        p_obj - The object.
        Returns:
        The object's JSON representation.
        Throws:
        IOException
      • escape

        public String escape​(CharSequence p_chseq)
        Escape a character sequence according to the JSON rules.
        Parameters:
        p_chseq - The character sequence to be escaped.
        Returns:
        The escaped character sequence.