Functions for use in JSON parsing and formatting
These functions are used by other libraries to perform common functions related to JSON parsing and formatting.
The output functions take two alternative forms:
- functions that append to an
Appendable
(usually aStringBuilder
, but possibly aWriter
or some other class implementing that interface) - functions that output characters to an
IntConsumer
(theIntConsumer
is always the final parameter, allowing Kotlin users to provide a lambda in the Kotlin idiomatic form)
The functions that use Appendable
are all declared as throwing IOException
, but they only do so if the Appendable
implementation class does so.
If the functions are used with a StringBuilder
, the exception may safely be ignored.
There is also an escapeString
function that returns the string in its escaped form, along with a version of this
function that omits the enclosing quotes.
The parsing functions make use of the textmatcher
library.
The TextMatcher
class holds a string to be parsed, with indexes to represent the start and end of the current parsed
element.
The functions are static members of the JSONFunctions
class.
This function appends a JSON string to an Appendable
, enclosing it in quotes and escaping special characters
according to the JSON specification.
The parameters are:
- the
Appendable
- the string to be output (actually a
CharSequence
) - a boolean indicator – if set to
true
, characters above the base ASCII set will be output unencoded; if set tofalse
, any such characters will be output as Unicode escape sequences
Example:
JSONFunctions.appendString(stringBuilder, "Müller Straße", false);
will output:
"M\u00fcller Stra\u00dfe"
This function outputs a string to an IntConsumer
character at a time, preceded and followed by quote characters, and
with special characters escaped according to the
JSON specification.
The parameters are:
- the string to be output (actually a
CharSequence
) - a boolean indicator (see the description above under
appendString
) - the
IntConsumer
output function
Example:
JSONFunctions.outputString("Müller Straße", false, ch -> outputStream.write(ch));
will output:
"M\u00fcller Stra\u00dfe"
This function converts a JSON string to another string, enclosing it in quotes and escaping special characters according to the JSON specification. The parameters are:
- the string to be output (actually a
CharSequence
) - a boolean indicator – if set to
true
, characters above the base ASCII set will be output unencoded; if set tofalse
, any such characters will be output as Unicode escape sequences
The function returns the converted string.
Example:
String escaped = JSONFunctions.escapeString("Müller Straße", false);
will return:
"M\u00fcller Stra\u00dfe"
Or, if the option is set to return characters above the base ASCII set unencoded is selected::
String escaped = JSONFunctions.escapeString("Müller Straße", true);
will return:
"Müller Straße"
This function converts a JSON string to an escaped form of the string like the above function, but without the enclosing quotes. The parameters are:
- the string to be output (actually a
CharSequence
) - a boolean indicator – if set to
true
, characters above the base ASCII set will be output unencoded; if set tofalse
, any such characters will be output as Unicode escape sequences
The function returns the converted string, or if no characters in the string require conversion, it returns the original string unmodified (avoiding the need for object allocations).
Example:
String escaped = JSONFunctions.escapeStringUnquoted("Müller Straße", false);
will return:
M\u00fcller Stra\u00dfe
This appends a single character to an Appendable
with the appropriate escaping.
The parameters are:
- the
Appendable
- the character to be output
- a boolean indicator (see the description above under
appendString
)
Example:
JSONFunctions.appendChar(stringBuilder, '£', false);
will output:
\u00a3
This outputs a single character to an IntConsumer
with the appropriate escaping.
The parameters are:
- the character to be output
- a boolean indicator (see the description above under
appendString
) - the
IntConsumer
output function
Example:
JSONFunctions.outputChar('£', false, ch -> outputStream.write(ch));
will output:
\u00a3
There is often a requirement to display a JSON string in an error message, limiting the output to a maximum number of
characters.
This function converts a string a form suitable for display, abbreviating it if necessary by splicing the characters
“...
” into the middle.
The parameters are:
- the string
- the maximum number of characters
Example:
String message = JSONFunctions.displayString("the quick brown fox jumps over the lazy dog", 21);
will return:
"the quic ... lazy dog"
The parseString
function parses an input string, decoding JSON escape sequences and terminating on an unescaped double
quote character.
The function makes use of the textmatcher
library; the TextMatcher
class
allows a parse position index to be maintained along with the text, and this is used by the parseString
function to
determine where to start decoding, and to indicate where parsing completed.
The single parameter to the function is:
- the
TextMatcher
, with theindex
positioned after the opening double quote character
The function returns the decoded string, and the index
of the TextMatcher
is left positioned after the closing
double quote.
This function simply tests whether a character is a whitespace character according to the JSON specification.
The latest version of the library is 1.9, and it may be obtained from the Maven Central repository.
<dependency>
<groupId>net.pwall.json</groupId>
<artifactId>json-functions</artifactId>
<version>1.9</version>
</dependency>
implementation 'net.pwall.json:json-functions:1.9'
implementation("net.pwall.json:json-functions:1.9")
Peter Wall
2023-12-02