java.lang.Object
io.jstach.ezkv.json5.JSON5KeyValuesMedia
- All Implemented Interfaces:
KeyValuesMedia
,KeyValuesServiceProvider
,KeyValuesServiceProvider.KeyValuesMediaFinder
Parses JSON5 to KeyValues. The
Here is the corresponding key values (notice duplicates):
We will get flattened key values that look like:
Because both formats may not be what you want you might want to use a
filter to clean up the results.
parser()
can
also be used for strict JSON as JSON5 is a true superset.
The constants in this class are important and describe parameters that can be set on the
KeyValuesResource
.
JSON is not flat and is more of a tree where as EZKV KeyValues are and only support string values. Thus the parser flattens the JSON based on some heuristics. JSON objects are easy to flatten but JSON arrays need special handling.
Here is an example of the default flattening: {
"a": {
"b": [1, 2.0, {"c": 3}],
"d": "value"
}
}
a.b=1
a.b=2.0
a.b.c=3
a.d=value
Because EZKV supports duplicates and order matters the data is not lost but this might be confusing. Another option is to generate array indices in the keys.
Assume we load a resource with the previous JSON like:
_load_a=classpath:/a.json5?_param_json5_arraykey=array
a.b[0]=1
a.b[1]=2.0
a.b[2].c=3
a.d=value
This module does have a service loader registration. If you do not wish to use the
Service Loader you can add an instance of this class to
KeyValuesSystem.Builder
.
- See Also:
- API Note
- This is not included with the core ezkv module because JSON5 is still evolving and likely to keep changing.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.jstach.ezkv.kvs.KeyValuesMedia
KeyValuesMedia.Formatter, KeyValuesMedia.Parser
Nested classes/interfaces inherited from interface io.jstach.ezkv.kvs.KeyValuesServiceProvider
KeyValuesServiceProvider.KeyValuesFilter, KeyValuesServiceProvider.KeyValuesLoaderFinder, KeyValuesServiceProvider.KeyValuesMediaFinder, KeyValuesServiceProvider.KeyValuesProvider
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Will output array keys using array notation likea[0]
instead of the default which is duplicating the keys.static final String
Will duplicate keys for array entries.static final String
This parameter controls the array key naming scheme and is set withKeyValuesResource.parameters()
.static final String
The default JSON path separator.static final String
The file ext is "json5" and notjson
however this media will work forjson
extension.static final String
The media type is "application/json5" and notapplication/json
however this media will work for normal json.static final String
Configures whether or not to use the raw JSON5 number found instead of the Java converted number inKeyValuesResource.parameters()
with the key "json5_number_raw" and is by defaultfalse
.static final String
Configures the separator byKeyValuesResource.parameters()
with the key "json5_separator" and is by default ".".Fields inherited from interface io.jstach.ezkv.kvs.KeyValuesMedia
FILE_EXT_PROPERTIES, MEDIA_TYPE_PROPERTIES, MEDIA_TYPE_URLENCODED
Fields inherited from interface io.jstach.ezkv.kvs.KeyValuesServiceProvider
BUILTIN_ORDER_START
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfindByMediaType
(String mediaType) Finds aKeyValuesMedia
based on a media type string.Returns aKeyValuesMedia.Formatter
for formatting key-value pairs to an appendable target.@Nullable String
Returns the file extension associated with the media type, if any.Returns the media type as a string and should ideally be in rfc6838 format, is required and should be unique from otherKeyValuesMedia
unlikeKeyValuesMedia.getFileExt()
.boolean
hasFileExt
(String filename) Checks if a given filename has the file extension associated with this media type.parser()
Returns aKeyValuesMedia.Parser
for parsing key-value pairs from an input stream.Returns a parser that maybe customized by parameters.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.jstach.ezkv.kvs.KeyValuesMedia
findByExt, findByUri, hasFileExt
Methods inherited from interface io.jstach.ezkv.kvs.KeyValuesServiceProvider
order
Methods inherited from interface io.jstach.ezkv.kvs.KeyValuesServiceProvider.KeyValuesMediaFinder
findByResource
-
Field Details
-
MEDIA_TYPE
The media type is "application/json5" and notapplication/json
however this media will work for normal json.- See Also:
-
FILE_EXT
The file ext is "json5" and notjson
however this media will work forjson
extension.- See Also:
-
ARRAY_KEY_PARAM
This parameter controls the array key naming scheme and is set withKeyValuesResource.parameters()
. The two values are "duplicate" (default) and "array". An example is:_load_a=classpath:/a.json5?_param_json5_arraykey=array
- See Also:
-
ARRAY_KEY_ARRAY_VALUE
Will output array keys using array notation likea[0]
instead of the default which is duplicating the keys.- See Also:
-
ARRAY_KEY_DUPLICATE_VALUE
Will duplicate keys for array entries. For example{ "a" : [ 1, 2 ] }
will bea=1 a=2
- See Also:
-
SEPARATOR_PARAM
Configures the separator byKeyValuesResource.parameters()
with the key "json5_separator" and is by default ".".- See Also:
-
DEFAULT_SEPARATOR
The default JSON path separator.- See Also:
-
NUMBER_RAW_PARAM
Configures whether or not to use the raw JSON5 number found instead of the Java converted number inKeyValuesResource.parameters()
with the key "json5_number_raw" and is by defaultfalse
. An example is if one used hexadecimal-0xC0FFEE
it normally would be converted to an integer.{ "number" : -0xC0FFEE}
# Normally the below would happen # number=-12648430 # But will now be number=-0xC0FFEE
- See Also:
-
-
Constructor Details
-
JSON5KeyValuesMedia
public JSON5KeyValuesMedia()For service loader.
-
-
Method Details
-
getMediaType
Description copied from interface:KeyValuesMedia
Returns the media type as a string and should ideally be in rfc6838 format, is required and should be unique from otherKeyValuesMedia
unlikeKeyValuesMedia.getFileExt()
. For implementers if there is not a registered media type a recommendation is to useapplication/x-ext
where "ext" would be replaced with the file extension or some name.- Specified by:
getMediaType
in interfaceKeyValuesMedia
- Returns:
- the media type
-
getFileExt
Description copied from interface:KeyValuesMedia
Returns the file extension associated with the media type, if any. The first period ".
" should not be included. For example.tar.gz
should betar.gz
.- Specified by:
getFileExt
in interfaceKeyValuesMedia
- Returns:
- the file extension or
null
if not applicable
-
parser
Description copied from interface:KeyValuesMedia
Returns aKeyValuesMedia.Parser
for parsing key-value pairs from an input stream.- Specified by:
parser
in interfaceKeyValuesMedia
- Returns:
- the parser
-
parser
Description copied from interface:KeyValuesMedia
Returns a parser that maybe customized by parameters. By defaultKeyValuesMedia.parser()
is called.- Specified by:
parser
in interfaceKeyValuesMedia
- Parameters:
parameters
- parameters that can come fromKeyValuesResource.parameters()
.- Returns:
- parser
-
formatter
Description copied from interface:KeyValuesMedia
Returns aKeyValuesMedia.Formatter
for formatting key-value pairs to an appendable target. By default, this method throwsUnsupportedOperationException
if formatting is not supported.- Specified by:
formatter
in interfaceKeyValuesMedia
- Returns:
- the formatter
-
findByMediaType
Description copied from interface:KeyValuesServiceProvider.KeyValuesMediaFinder
Finds aKeyValuesMedia
based on a media type string.- Specified by:
findByMediaType
in interfaceKeyValuesMedia
- Specified by:
findByMediaType
in interfaceKeyValuesServiceProvider.KeyValuesMediaFinder
- Parameters:
mediaType
- the media type (e.g., "application/json")- Returns:
- an
Optional
containing the media if found, or empty if not
-
hasFileExt
Description copied from interface:KeyValuesMedia
Checks if a given filename has the file extension associated with this media type.- Specified by:
hasFileExt
in interfaceKeyValuesMedia
- Parameters:
filename
- the filename to check- Returns:
true
if the filename ends with the media type's file extension, otherwisefalse
-