Java Utililty Methods Json

List of utility methods to do Json

Description

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

Method

JsonObjectBuilderaddAll(JsonObjectBuilder a, JsonObjectBuilder b)
add All
Objects.requireNonNull(a);
Objects.requireNonNull(b);
return addAll(a, b.build());
JsonObjectasJsonObject(JsonValue value)
as Json Object
return JsonObject.class.cast(value);
StringasString(JsonValue value)
as String
if (JsonValue.ValueType.STRING.equals(value.getValueType())) {
    return ((JsonString) value).getString();
return value.toString();
booleancompare(JsonValue expected, JsonValue actual, boolean strict)
compare
switch (expected.getValueType()) {
case OBJECT:
    if (actual.getValueType() == ValueType.OBJECT) {
        return jsonEquals((JsonObject) expected, (JsonObject) actual, strict);
    break;
case ARRAY:
    if (actual.getValueType() == ValueType.ARRAY) {
...
ObjectconvertJsonValue(Object jsonValue, Class desiredType)
convert Json Value
if (jsonValue instanceof JsonNumber) {
    JsonNumber number = (JsonNumber) jsonValue;
    if (desiredType == null || desiredType == Long.class || desiredType == Long.TYPE) {
        return number.longValue();
    } else if (desiredType == Integer.class || desiredType == Integer.TYPE) {
        return number.intValue();
    } else if (desiredType == Double.class || desiredType == Double.TYPE) {
        return number.doubleValue();
...
Stringescape(String text)
Escape a string.
if (text == null)
    return null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
    char ch = text.charAt(i);
    switch (ch) {
    case '"':
        sb.append("\\\"");
...
voidfill_dictionary(Map dictionary, JsonObject jsonObject)
fildictionary
if (dictionary == null || jsonObject == null) {
    return;
for (String key : jsonObject.keySet()) {
    JsonValue valueObj = (JsonValue) jsonObject.get(key);
    JsonValue.ValueType type = valueObj.getValueType();
    if (type == JsonValue.ValueType.ARRAY) {
        JsonArray arrayObj = jsonObject.getJsonArray(key);
...
voidfill_key_value(JsonObjectBuilder jsonObject, String key, Object value)
filkevalue
if (jsonObject == null || key == null) {
    return;
if (value == null) {
    jsonObject.addNull(key);
} else if (value instanceof String) {
    jsonObject.add(key, (String) value);
} else if (value instanceof Integer) {
...
voidfill_list(JsonObjectBuilder jsonObject, String key, List list)
fillist
if (jsonObject == null || key == null) {
    return;
JsonArrayBuilder arrayBuilder = jsonFactory.createArrayBuilder();
for (Object entry : list) {
    if (entry instanceof Map) {
        JsonObjectBuilder entryObj = jsonFactory.createObjectBuilder();
        fill_map(entryObj, (Map<String, Object>) entry);
...
StringfromDictionary(Map dictionary)
from Dictionary
if (dictionary == null) {
    return null;
JsonObjectBuilder root = jsonFactory.createObjectBuilder();
fill_map(root, dictionary);
return root.build().toString();