step.artefacts.handlers.UserFriendlyJsonObject Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (C) 2020, exense GmbH
*
* This file is part of STEP
*
* STEP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* STEP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with STEP. If not, see .
******************************************************************************/
package step.artefacts.handlers;
import jakarta.json.*;
import java.util.*;
public class UserFriendlyJsonObject implements OutputJsonObject {
private final JsonObject wrapped;
private final Map unwrappedValues;
public UserFriendlyJsonObject(JsonObject wrapped) {
this.wrapped = wrapped;
this.unwrappedValues = unwrapJsonObject(wrapped);
}
protected Map unwrapJsonObject(JsonObject jsonObject) {
Map simpleValues = new HashMap<>();
for (Entry nestedField : jsonObject.entrySet()) {
Object simpleValue = unwrapSimpleValue(nestedField.getValue());
simpleValues.put(nestedField.getKey(), simpleValue);
}
return simpleValues;
}
private Object unwrapSimpleValue(JsonValue fieldValue) {
Object simpleValue;
if (fieldValue.getValueType() == ValueType.OBJECT) {
simpleValue = unwrapJsonObject(fieldValue.asJsonObject());
} else if (fieldValue.getValueType() == ValueType.STRING) {
simpleValue = ((JsonString) fieldValue).getString();
} else if (fieldValue.getValueType() == ValueType.NULL) {
simpleValue = null;
} else if (fieldValue.getValueType() == ValueType.NUMBER) {
JsonNumber numberValue = (JsonNumber) fieldValue;
if (numberValue.isIntegral()) {
simpleValue = numberValue.longValue();
} else {
simpleValue = numberValue.doubleValue();
}
} else if (fieldValue.getValueType() == ValueType.FALSE) {
simpleValue = false;
} else if (fieldValue.getValueType() == ValueType.TRUE) {
simpleValue = true;
} else if (fieldValue.getValueType() == ValueType.ARRAY) {
JsonArray jsonArray = fieldValue.asJsonArray();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy