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.ParserNested classes/interfaces inherited from interface io.jstach.ezkv.kvs.KeyValuesServiceProvider
KeyValuesServiceProvider.KeyValuesFilter, KeyValuesServiceProvider.KeyValuesLoaderFinder, KeyValuesServiceProvider.KeyValuesMediaFinder, KeyValuesServiceProvider.KeyValuesProvider -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringWill output array keys using array notation likea[0]instead of the default which is duplicating the keys.static final StringWill duplicate keys for array entries.static final StringThis parameter controls the array key naming scheme and is set withKeyValuesResource.parameters().static final StringThe default JSON path separator.static final StringThe file ext is "json5" and notjsonhowever this media will work forjsonextension.static final StringThe media type is "application/json5" and notapplication/jsonhowever this media will work for normal json.static final StringConfigures 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 StringConfigures 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_URLENCODEDFields inherited from interface io.jstach.ezkv.kvs.KeyValuesServiceProvider
BUILTIN_ORDER_START -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfindByMediaType(String mediaType) Finds aKeyValuesMediabased on a media type string.Returns aKeyValuesMedia.Formatterfor formatting key-value pairs to an appendable target.@Nullable StringReturns 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 otherKeyValuesMediaunlikeKeyValuesMedia.getFileExt().booleanhasFileExt(String filename) Checks if a given filename has the file extension associated with this media type.parser()Returns aKeyValuesMedia.Parserfor 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, waitMethods inherited from interface io.jstach.ezkv.kvs.KeyValuesMedia
findByExt, findByUri, hasFileExtMethods inherited from interface io.jstach.ezkv.kvs.KeyValuesServiceProvider
orderMethods inherited from interface io.jstach.ezkv.kvs.KeyValuesServiceProvider.KeyValuesMediaFinder
findByResource
-
Field Details
-
MEDIA_TYPE
The media type is "application/json5" and notapplication/jsonhowever this media will work for normal json.- See Also:
-
FILE_EXT
The file ext is "json5" and notjsonhowever this media will work forjsonextension.- 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-0xC0FFEEit normally would be converted to an integer.Will yield key values:{ "number" : -0xC0FFEE}Note that exponents in JSON5 work different than in Java so downstream configuration parsing will have to deal with this if this is turned on but otherwise most of JSON5 number format is a compatible subset of Java.# 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:KeyValuesMediaReturns the media type as a string and should ideally be in rfc6838 format, is required and should be unique from otherKeyValuesMediaunlikeKeyValuesMedia.getFileExt(). For implementers if there is not a registered media type a recommendation is to useapplication/x-extwhere "ext" would be replaced with the file extension or some name.- Specified by:
getMediaTypein interfaceKeyValuesMedia- Returns:
- the media type
-
getFileExt
Description copied from interface:KeyValuesMediaReturns the file extension associated with the media type, if any. The first period "." should not be included. For example.tar.gzshould betar.gz.- Specified by:
getFileExtin interfaceKeyValuesMedia- Returns:
- the file extension or
nullif not applicable
-
parser
Description copied from interface:KeyValuesMediaReturns aKeyValuesMedia.Parserfor parsing key-value pairs from an input stream.- Specified by:
parserin interfaceKeyValuesMedia- Returns:
- the parser
-
parser
Description copied from interface:KeyValuesMediaReturns a parser that maybe customized by parameters. By defaultKeyValuesMedia.parser()is called.- Specified by:
parserin interfaceKeyValuesMedia- Parameters:
parameters- parameters that can come fromKeyValuesResource.parameters().- Returns:
- parser
-
formatter
Description copied from interface:KeyValuesMediaReturns aKeyValuesMedia.Formatterfor formatting key-value pairs to an appendable target. By default, this method throwsUnsupportedOperationExceptionif formatting is not supported.- Specified by:
formatterin interfaceKeyValuesMedia- Returns:
- the formatter
-
findByMediaType
Description copied from interface:KeyValuesServiceProvider.KeyValuesMediaFinderFinds aKeyValuesMediabased on a media type string.- Specified by:
findByMediaTypein interfaceKeyValuesMedia- Specified by:
findByMediaTypein interfaceKeyValuesServiceProvider.KeyValuesMediaFinder- Parameters:
mediaType- the media type (e.g., "application/json")- Returns:
- an
Optionalcontaining the media if found, or empty if not
-
hasFileExt
Description copied from interface:KeyValuesMediaChecks if a given filename has the file extension associated with this media type.- Specified by:
hasFileExtin interfaceKeyValuesMedia- Parameters:
filename- the filename to check- Returns:
trueif the filename ends with the media type's file extension, otherwisefalse
-