Java Utililty Methods Json Encode

List of utility methods to do Json Encode

Description

The list of methods to do Json Encode are organized into topic(s).

Method

StringjsonEncode(final String val)
Encode a json string according to the statement:

In JSON only the backslash, double quote and ASCII control characters need to be escaped.

if ((val == null) || (val.length() == 0)) {
    return "\"\"";
StringBuilder sb = new StringBuilder();
sb.append('"');
for (int i = 0; i < val.length(); i++) {
    char ch = val.charAt(i);
    switch (ch) {
...
StringjsonEncodeString(String text)
Lets make sure we encode the given string so its a valid JSON value which is wrapped in quotes if its not null
if (text == null) {
    return "null";
StringBuilder buffer = new StringBuilder("\"");
int length = text.length();
for (int i = 0; i < length; i++) {
    char ch = text.charAt(i);
    if (ch == '"') {
...