Class KiwiJacksonDataFormats
- java.lang.Object
-
- org.kiwiproject.jackson.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 areJsonFactoryand its subclasses.- See Also:
DataFormatDetector,JsonFactory,XmlFactory,YAMLFactory,Charset.defaultCharset()
-
-
Constructor Summary
Constructors Constructor Description KiwiJacksonDataFormats()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Optional<JacksonDataFormat>detectFormat(String text)Detect data format of given text using the defaultCharset.static Optional<JacksonDataFormat>detectFormat(String text, Charset charset)Detect data format of given text using the givenCharset.static Optional<String>detectFormat(String text, Charset charset, com.fasterxml.jackson.core.format.DataFormatDetector detector)Detect data format of given text using givenCharsetandDataFormatDetector.static Optional<JacksonDataFormat>detectFormatUtf8(String text)Detect data format of given text using UTF-8Charset.static booleanisFormat(JacksonDataFormat matchFormat, String text, Charset charset)Does the given text, using the givenCharset, have data formatmatchFormat.static booleanisJson(String text)Is the given text JSON using the defaultCharset?static booleanisJson(String text, Charset charset)Is the given text JSON using the givenCharset?static booleanisJsonUtf8(String text)Is the given text JSON using UTF-8Charset?static booleanisXml(String text)Is the given text XML using the defaultCharset?static booleanisXml(String text, Charset charset)Is the given text XML using the givenCharset?static booleanisXmlUtf8(String text)Is the given text XML using UTF-8Charset?static booleanisYaml(String text)Is the given text YAML using the defaultCharset?static booleanisYaml(String text, Charset charset)Is the given text YAML using the givenCharset?static booleanisYamlUtf8(String text)Is the given text YAML using UTF-8Charset?
-
-
-
Method Detail
-
isJson
public static boolean isJson(String text)
Is the given text JSON using the defaultCharset?- Parameters:
text- the String value to check- Returns:
trueif text is JSON;falseotherwise
-
isJsonUtf8
public static boolean isJsonUtf8(String text)
Is the given text JSON using UTF-8Charset?- Parameters:
text- the String value to check- Returns:
trueif text is JSON;falseotherwise
-
isJson
public static boolean isJson(String text, Charset charset)
Is the given text JSON using the givenCharset?- Parameters:
text- the String value to checkcharset- the character set to use- Returns:
trueif text is JSON;falseotherwise
-
isXml
public static boolean isXml(String text)
Is the given text XML using the defaultCharset?- Parameters:
text- the String value to check- Returns:
trueif text is XML;falseotherwise
-
isXmlUtf8
public static boolean isXmlUtf8(String text)
Is the given text XML using UTF-8Charset?- Parameters:
text- the String value to check- Returns:
trueif text is XML;falseotherwise
-
isXml
public static boolean isXml(String text, Charset charset)
Is the given text XML using the givenCharset?- Parameters:
text- the String value to checkcharset- the character set to use- Returns:
trueif text is XML;falseotherwise
-
isYaml
public static boolean isYaml(String text)
Is the given text YAML using the defaultCharset?- Parameters:
text- the String value to check- Returns:
trueif text is YAML;falseotherwise- 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-8Charset?- Parameters:
text- the String value to check- Returns:
trueif text is YAML;falseotherwise- 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 givenCharset?- Parameters:
text- the String value to checkcharset- the character set to use- Returns:
trueif text is YAML;falseotherwise- 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 givenCharset, have data formatmatchFormat.- Parameters:
matchFormat- the format to match againsttext- the String value to checkcharset- the character set to use- Returns:
trueif text is equal tomatchFormat;falseotherwise
-
detectFormat
public static Optional<JacksonDataFormat> detectFormat(String text)
Detect data format of given text using the defaultCharset.- 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-8Charset.- 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 givenCharset.- Parameters:
text- the String value to checkcharset- 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 givenCharsetandDataFormatDetector.This method is an "escape hatch"; it allows detection of formats not supported in
JacksonDataFormatsince it accepts aDataFormatDetectorand it returns a String. Thus, since the caller supplies its own customDataFormatDetector, 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 theJacksonDataFormatenum.- Parameters:
text- the String value to checkcharset- the character set to usedetector- 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
-
-