public class JSONUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
JSONUtil.NodeType
JSON Node types as defined in
http://tools.ietf.org/html/draft-zyp-json-schema-04#section-3.5
|
static class |
JSONUtil.SchemaVersion
Supported JSON Schema Versions
|
Modifier and Type | Field and Description |
---|---|
static String |
$REF |
static String |
$SCHEMA |
static String |
FULL_DOCUMENT_JSON_POINTER |
static com.boomi.util.StreamUtil.Parser<JsonNode> |
JSON_PARSER
A
StreamUtil.Parser implementation which parses a given InputStream into a JsonNode
by calling StreamUtil.Parser |
static String |
POINTER_PATH_DELIMITER |
static char |
POINTER_PATH_DELIMITER_CHAR |
static String |
SCHEMA_ID |
static String |
SCHEMA_PROPERTIES |
static String |
SCHEMA_TITLE |
static String |
SCHEMA_TYPE |
Modifier and Type | Method and Description |
---|---|
static boolean |
asBoolean(JsonNode node,
String fieldName)
Util method to safely get the specified field as a boolean.
|
static String |
asText(JsonNode node,
String fieldName)
Util method to safely get the content of the specified field using
JsonNode.asText() . |
static JsonNode |
buildSchema(Buildable buildable)
Build a JSON schema based on the provided
Buildable . |
static PrettyPrinter |
createPrettyPrinter()
Creates and returns a JSON pretty printer which always uses windows
newlines (the boomi "standard").
|
static String |
decodeSlashesAndtildas(String name) |
static String |
encodeSlashesAndTildas(String name) |
static Iterable<Map.Entry<String,JsonNode>> |
fieldsIterable(JsonNode jsonNode)
Creates an Iterable of the node fields.
|
static JsonPointer |
formatPointer(String pattern,
String... arguments)
Creates a JsonPointer for the supplied pattern.
|
static JsonFactory |
getDefaultJsonFactory()
Obtain the global JsonFactory instance.
|
static ObjectMapper |
getDefaultObjectMapper()
Obtain the global ObjectMapper instance.
|
static boolean |
hasNonNull(JsonNode node,
String fieldName)
Util method to safely determine if the specified field exists with a
non-null value.
|
static JsonNode |
loadSchemaFromResource(String resourceName)
Utility method to load a JSON schema from a resource.
|
static ObjectNode |
newEmptySchema(JSONUtil.SchemaVersion schemaVersion)
Instantiates a new object node to be used as a schema.
|
static JsonParser |
newInitializedParser(InputStream inputStream)
Creates a new JsonParser for the input stream and advances parser to the
first value token.
|
static ObjectDefinition |
newJsonDefinition(ObjectDefinitionRole role,
JsonNode schema)
Factory method to create a new
ObjectDefinition instance with
JSON defaults. |
static ObjectDefinition |
newJsonDefinitionFromResource(ObjectDefinitionRole role,
String resourceName)
Factory method to load a schema resource and use it to create a new
ObjectDefinition instance with JSON defaults. |
static ObjectNode |
newObjectNode()
Instantiates a new ObjectNode with the default node factory.
|
static ObjectNode |
newObjectNode(Map<String,JsonNode> children)
Instantiates a new ObjectNode with the default node factory.
|
static JsonParser |
newParser(InputStream inputStream)
Creates a new JsonParser for the input stream.
|
static JsonPointer |
newPointer(String... arguments)
Creates a JsonPointer for the path arguments.
|
static String |
newPointerString(String... arguments)
Creates a JsonPointer for the path arguments.
|
static ObjectNode |
newSchema(JSONUtil.SchemaVersion schemaVersion,
JSONUtil.NodeType... type)
Creates a new schema with the specified version and ID.
|
static ObjectNode |
newSchema(JSONUtil.SchemaVersion schemaVersion,
String id)
Creates a new schema with the specified version and ID.
|
static ObjectNode |
newSchema(JSONUtil.SchemaVersion schemaVersion,
String title,
JSONUtil.NodeType... types)
Creates a new schema with the specified version and ID.
|
static SchemaBuilder |
newSchemaBuilder()
Factory method to create a new
SchemaBuilder instance. |
static JsonNode |
parseNode(InputStream inputStream)
Parses an JSON input stream into a Java object.
|
static JsonNode |
path(JsonNode node,
String fieldName)
Null safe convenience method for
JsonNode.path(String) . |
static String |
prettyPrintJSON(Object json)
Pretty print JSON nodes.
|
static JsonNode |
remove(JsonNode node,
String fieldName)
Convenience method to remove the provided field name from the the provided json node.
|
static void |
writeNode(JsonNode jsonNode,
OutputStream outputStream)
Writes the given node to the supplied output stream.
|
public static final String FULL_DOCUMENT_JSON_POINTER
public static final com.boomi.util.StreamUtil.Parser<JsonNode> JSON_PARSER
StreamUtil.Parser
implementation which parses a given InputStream
into a JsonNode
by calling StreamUtil.Parser
public static final String $SCHEMA
public static final String $REF
public static final String SCHEMA_ID
public static final String SCHEMA_TITLE
public static final String SCHEMA_TYPE
public static final String SCHEMA_PROPERTIES
public static final char POINTER_PATH_DELIMITER_CHAR
public static final String POINTER_PATH_DELIMITER
public static ObjectMapper getDefaultObjectMapper()
public static JsonFactory getDefaultJsonFactory()
public static JsonParser newParser(InputStream inputStream) throws IOException
inputStream
- java.io.InputStream to parse.IOException
public static JsonParser newInitializedParser(InputStream inputStream) throws IOException
inputStream
- java.io.InputStream to parse.IOException
public static String prettyPrintJSON(Object json) throws JsonProcessingException
json
- Object to be formatted.JsonProcessingException
public static ObjectNode newObjectNode()
public static ObjectNode newObjectNode(Map<String,JsonNode> children)
children
- Children for new node.public static ObjectNode newEmptySchema(JSONUtil.SchemaVersion schemaVersion)
schemaVersion
- The version of the schema.public static ObjectNode newSchema(JSONUtil.SchemaVersion schemaVersion, String id)
schemaVersion
- The version of the schema to set.id
- The ID of the schema.public static ObjectNode newSchema(JSONUtil.SchemaVersion schemaVersion, JSONUtil.NodeType... type)
schemaVersion
- The version of the schema to set.type
- The schema object type.public static ObjectNode newSchema(JSONUtil.SchemaVersion schemaVersion, String title, JSONUtil.NodeType... types)
schemaVersion
- The version of the schema to set.title
- The schema object title.types
- The schema object types.public static String newPointerString(String... arguments)
arguments
- Array of arguments in path.public static JsonPointer newPointer(String... arguments)
arguments
- Array of arguments in path.public static JsonPointer formatPointer(String pattern, String... arguments)
pattern
- MessageFormat pattern to populate.arguments
- Arguments for the template.public static Iterable<Map.Entry<String,JsonNode>> fieldsIterable(JsonNode jsonNode)
jsonNode
- JsonNode whose fields need to be iterated.public static void writeNode(JsonNode jsonNode, OutputStream outputStream) throws IOException
jsonNode
- com.fasterxml.jackson.databind.JsonNode to write.outputStream
- java.io.OutputStream to write to.IOException
- In event of failure.public static JsonNode parseNode(InputStream inputStream) throws JsonProcessingException, IOException
inputStream
- The input stream to parse.JsonProcessingException
- On parsing error.IOException
- On stream error.public static PrettyPrinter createPrettyPrinter()
public static String asText(JsonNode node, String fieldName)
JsonNode.asText()
. Numeric and boolean fields will be converted
to text.node
- the json nodefieldName
- the name of the fieldpublic static boolean asBoolean(JsonNode node, String fieldName)
JsonNode#path(String)#asBoolean(JsonNode, String)
directly. This method only calls
JsonNode.asBoolean()
if the node returned from JsonNode.path(String)
is actually a
BooleanNode
and treats everything else as false. Invoking JsonNode.asBoolean()
on non-boolean
nodes leads to some interesting behavior. e.g., {"x": 9}.path("x").asBoolean() == true
.node
- the json nodefieldName
- the name of the fieldpublic static JsonNode path(JsonNode node, String fieldName)
JsonNode.path(String)
.node
- the json nodefieldName
- the name of the fieldJsonNode.path(String)
if the node is non-null, MissingNode
instance otherwisepublic static boolean hasNonNull(JsonNode node, String fieldName)
node
- the json nodefieldName
- the name of the fieldpublic static JsonNode remove(JsonNode node, String fieldName)
JsonNode
to ObjectNode
.node
- the node to modifyfieldName
- the name of the field to removeJsonNode
containing the field data if the field exists, a MissingNode
instance otherwise.IllegalArgumentException
- if node is null or cannot be cast to an ObjectNode
public static SchemaBuilder newSchemaBuilder()
SchemaBuilder
instance.public static ObjectDefinition newJsonDefinition(ObjectDefinitionRole role, JsonNode schema)
ObjectDefinition
instance with
JSON defaults. Specifically, the input and output content types will be
set to either ContentType.JSON
or ContentType.NONE
depending on the provided ObjectDefinitionRole
, the element name
will be set to JSON Pointer for the full document, and the JSON schema
with the string value of the provided schema.role
- the role for the definitionschema
- the schema for the definitionObjectDefinition
instance with JSON defaultspublic static ObjectDefinition newJsonDefinitionFromResource(ObjectDefinitionRole role, String resourceName) throws IOException
ObjectDefinition
instance with JSON defaults.role
- the role for the definitionresourceName
- the name of the resource containing the schemaObjectDefinition
instance with JSON defaultsIOException
newJsonDefinition(ObjectDefinitionRole, JsonNode)
public static JsonNode loadSchemaFromResource(String resourceName) throws IOException
resourceName
- the name of the resource to loadJsonNode
representing the schemaIOException
public static JsonNode buildSchema(Buildable buildable)
Buildable
. Currently only supports building schemas with root
type of "object".buildable
- the buildableIllegalArgumentException
- if the buildable is nullIllegalArgumentException
- if Buildable.getType()
is not JSONUtil.NodeType.OBJECT