Class KiwiJacksonDataFormats


  • public class KiwiJacksonDataFormats
    extends Object
    Static utilities for easily performing data format detection on String values. Currently this class supports detection of JSON, XML, and YAML assuming the YAML begins with the explicit document start marker ("---").

    NOTE: This relies entirely on jackson's data format detection!!! You will also need to ensure that the jackson-core, jackson-dataformat-xml, jackson-dataformat-yaml dependencies are present at runtime.

    WARNING: Before using, please make sure you understand how Jackson actually does its detection. Hint: it's not magic, and may not be as robust as you might like. For example the YAML detector will only report YAML if the content starts with the formal YAML preamble "---". Use the "@see" links below for links to the various Jackson classes that perform data format detection. The main class is DataFormatDetector, to which you supply one or more detectors, which are JsonFactory and its subclasses.

    See Also:
    DataFormatDetector, JsonFactory, XmlFactory, YAMLFactory, Charset.defaultCharset()
    • Constructor Detail

      • KiwiJacksonDataFormats

        public KiwiJacksonDataFormats()
    • Method Detail

      • isJson

        public static boolean isJson​(String text)
        Is the given text JSON using the default Charset?
        Parameters:
        text - the String value to check
        Returns:
        true if text is JSON; false otherwise
      • isJsonUtf8

        public static boolean isJsonUtf8​(String text)
        Is the given text JSON using UTF-8 Charset?
        Parameters:
        text - the String value to check
        Returns:
        true if text is JSON; false otherwise
      • isJson

        public static boolean isJson​(String text,
                                     Charset charset)
        Is the given text JSON using the given Charset?
        Parameters:
        text - the String value to check
        charset - the character set to use
        Returns:
        true if text is JSON; false otherwise
      • isXml

        public static boolean isXml​(String text)
        Is the given text XML using the default Charset?
        Parameters:
        text - the String value to check
        Returns:
        true if text is XML; false otherwise
      • isXmlUtf8

        public static boolean isXmlUtf8​(String text)
        Is the given text XML using UTF-8 Charset?
        Parameters:
        text - the String value to check
        Returns:
        true if text is XML; false otherwise
      • isXml

        public static boolean isXml​(String text,
                                    Charset charset)
        Is the given text XML using the given Charset?
        Parameters:
        text - the String value to check
        charset - the character set to use
        Returns:
        true if text is XML; false otherwise
      • isYaml

        public static boolean isYaml​(String text)
        Is the given text YAML using the default Charset?
        Parameters:
        text - the String value to check
        Returns:
        true if text is YAML; false otherwise
        Implementation Note:
        Jackson does NOT consider the content YAML without "---" at the top of the file!
      • isYamlUtf8

        public static boolean isYamlUtf8​(String text)
        Is the given text YAML using UTF-8 Charset?
        Parameters:
        text - the String value to check
        Returns:
        true if text is YAML; false otherwise
        Implementation Note:
        Jackson does NOT consider the content YAML without "---" at the top of the file!
      • isYaml

        public static boolean isYaml​(String text,
                                     Charset charset)
        Is the given text YAML using the given Charset?
        Parameters:
        text - the String value to check
        charset - the character set to use
        Returns:
        true if text is YAML; false otherwise
        Implementation Note:
        Jackson does NOT consider the content YAML without "---" at the top of the file!
      • isFormat

        public static boolean isFormat​(JacksonDataFormat matchFormat,
                                       String text,
                                       Charset charset)
        Does the given text, using the given Charset, have data format matchFormat.
        Parameters:
        matchFormat - the format to match against
        text - the String value to check
        charset - the character set to use
        Returns:
        true if text is equal to matchFormat; false otherwise
      • detectFormat

        public static Optional<JacksonDataFormat> detectFormat​(String text)
        Detect data format of given text using the default Charset.
        Parameters:
        text - the String value to check
        Returns:
        Optional containing detected format, or empty Optional if format was not detected or not supported
      • detectFormatUtf8

        public static Optional<JacksonDataFormat> detectFormatUtf8​(String text)
        Detect data format of given text using UTF-8 Charset.
        Parameters:
        text - the String value to check
        Returns:
        Optional containing detected format, or empty Optional if format was not detected or not supported
      • detectFormat

        public static Optional<JacksonDataFormat> detectFormat​(String text,
                                                               Charset charset)
        Detect data format of given text using the given Charset.
        Parameters:
        text - the String value to check
        charset - the character set to use
        Returns:
        Optional containing detected format, or empty Optional if format was not detected or not supported
      • detectFormat

        public static Optional<String> detectFormat​(String text,
                                                    Charset charset,
                                                    com.fasterxml.jackson.core.format.DataFormatDetector detector)
        Detect data format of given text using given Charset and DataFormatDetector.

        This method is an "escape hatch"; it allows detection of formats not supported in JacksonDataFormat since it accepts a DataFormatDetector and it returns a String. Thus, since the caller supplies its own custom DataFormatDetector, the formats that are supported are dictated entirely by that caller. And because it returns a String, it is not limited to the formats defined in the JacksonDataFormat enum.

        Parameters:
        text - the String value to check
        charset - the character set to use
        detector - the format detector to use
        Returns:
        Optional containing detected format name, or empty Optional if format was not detected or not supported by the supplied DataFormatDetector