JsonMarshaller Class — spring-boot Architecture
Architecture documentation for the JsonMarshaller class in JsonMarshaller.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java lines 45–245
public class JsonMarshaller {
public void write(ConfigurationMetadata metadata, OutputStream outputStream) throws IOException {
try {
JSONObject object = new JSONObject();
JsonConverter converter = new JsonConverter();
object.put("groups", converter.toJsonArray(metadata, ItemType.GROUP));
object.put("properties", converter.toJsonArray(metadata, ItemType.PROPERTY));
object.put("hints", converter.toJsonArray(metadata.getHints()));
object.put("ignored", converter.toJsonObject(metadata.getIgnored()));
outputStream.write(object.toString(2).getBytes(StandardCharsets.UTF_8));
}
catch (Exception ex) {
if (ex instanceof IOException ioException) {
throw ioException;
}
if (ex instanceof RuntimeException runtimeException) {
throw runtimeException;
}
throw new IllegalStateException(ex);
}
}
public ConfigurationMetadata read(InputStream inputStream) throws Exception {
ConfigurationMetadata metadata = new ConfigurationMetadata();
JSONObject object = new JSONObject(toString(inputStream));
JsonPath path = JsonPath.root();
checkAllowedKeys(object, path, "groups", "properties", "hints", "ignored");
JSONArray groups = object.optJSONArray("groups");
if (groups != null) {
for (int i = 0; i < groups.length(); i++) {
metadata
.add(toItemMetadata((JSONObject) groups.get(i), path.resolve("groups").index(i), ItemType.GROUP));
}
}
JSONArray properties = object.optJSONArray("properties");
if (properties != null) {
for (int i = 0; i < properties.length(); i++) {
metadata.add(toItemMetadata((JSONObject) properties.get(i), path.resolve("properties").index(i),
ItemType.PROPERTY));
}
}
JSONArray hints = object.optJSONArray("hints");
if (hints != null) {
for (int i = 0; i < hints.length(); i++) {
metadata.add(toItemHint((JSONObject) hints.get(i), path.resolve("hints").index(i)));
}
}
JSONObject ignored = object.optJSONObject("ignored");
if (ignored != null) {
JsonPath ignoredPath = path.resolve("ignored");
checkAllowedKeys(ignored, ignoredPath, "properties");
addIgnoredProperties(metadata, ignored, ignoredPath);
}
return metadata;
}
private void addIgnoredProperties(ConfigurationMetadata metadata, JSONObject ignored, JsonPath path)
throws JSONException {
JSONArray properties = ignored.optJSONArray("properties");
if (properties == null) {
return;
}
for (int i = 0; i < properties.length(); i++) {
JSONObject jsonObject = properties.getJSONObject(i);
checkAllowedKeys(jsonObject, path.resolve("properties").index(i), "name");
metadata.add(ItemIgnore.forProperty(jsonObject.getString("name")));
}
}
private ItemMetadata toItemMetadata(JSONObject object, JsonPath path, ItemType itemType) throws Exception {
switch (itemType) {
case GROUP -> checkAllowedKeys(object, path, "name", "type", "description", "sourceType", "sourceMethod");
case PROPERTY -> checkAllowedKeys(object, path, "name", "type", "description", "sourceType", "defaultValue",
"deprecation", "deprecated");
}
String name = object.getString("name");
String type = object.optString("type", null);
String description = object.optString("description", null);
String sourceType = object.optString("sourceType", null);
String sourceMethod = object.optString("sourceMethod", null);
Object defaultValue = readItemValue(object.opt("defaultValue"));
ItemDeprecation deprecation = toItemDeprecation(object, path);
return new ItemMetadata(itemType, name, null, type, sourceType, sourceMethod, description, defaultValue,
deprecation);
}
private ItemDeprecation toItemDeprecation(JSONObject object, JsonPath path) throws Exception {
if (object.has("deprecation")) {
JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
checkAllowedKeys(deprecationJsonObject, path.resolve("deprecation"), "level", "reason", "replacement",
"since");
ItemDeprecation deprecation = new ItemDeprecation();
deprecation.setLevel(deprecationJsonObject.optString("level", null));
deprecation.setReason(deprecationJsonObject.optString("reason", null));
deprecation.setReplacement(deprecationJsonObject.optString("replacement", null));
deprecation.setSince(deprecationJsonObject.optString("since", null));
return deprecation;
}
return object.optBoolean("deprecated") ? new ItemDeprecation() : null;
}
private ItemHint toItemHint(JSONObject object, JsonPath path) throws Exception {
checkAllowedKeys(object, path, "name", "values", "providers");
String name = object.getString("name");
List<ItemHint.ValueHint> values = new ArrayList<>();
if (object.has("values")) {
JSONArray valuesArray = object.getJSONArray("values");
for (int i = 0; i < valuesArray.length(); i++) {
values.add(toValueHint((JSONObject) valuesArray.get(i), path.resolve("values").index(i)));
}
}
List<ItemHint.ValueProvider> providers = new ArrayList<>();
if (object.has("providers")) {
JSONArray providersObject = object.getJSONArray("providers");
for (int i = 0; i < providersObject.length(); i++) {
providers.add(toValueProvider((JSONObject) providersObject.get(i), path.resolve("providers").index(i)));
}
}
return new ItemHint(name, values, providers);
}
private ItemHint.ValueHint toValueHint(JSONObject object, JsonPath path) throws Exception {
checkAllowedKeys(object, path, "value", "description");
Object value = readItemValue(object.get("value"));
String description = object.optString("description", null);
return new ItemHint.ValueHint(value, description);
}
private ItemHint.ValueProvider toValueProvider(JSONObject object, JsonPath path) throws Exception {
checkAllowedKeys(object, path, "name", "parameters");
String name = object.getString("name");
Map<String, Object> parameters = new HashMap<>();
if (object.has("parameters")) {
JSONObject parametersObject = object.getJSONObject("parameters");
for (Iterator<?> iterator = parametersObject.keys(); iterator.hasNext();) {
String key = (String) iterator.next();
Object value = readItemValue(parametersObject.get(key));
parameters.put(key, value);
}
}
return new ItemHint.ValueProvider(name, parameters);
}
private Object readItemValue(Object value) throws Exception {
if (value instanceof JSONArray array) {
Object[] content = new Object[array.length()];
for (int i = 0; i < array.length(); i++) {
content[i] = array.get(i);
}
return content;
}
return value;
}
private String toString(InputStream inputStream) throws IOException {
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
@SuppressWarnings("unchecked")
private void checkAllowedKeys(JSONObject object, JsonPath path, String... allowedKeys) {
Set<String> availableKeys = new TreeSet<>();
object.keys().forEachRemaining((key) -> availableKeys.add((String) key));
Arrays.stream(allowedKeys).forEach(availableKeys::remove);
if (!availableKeys.isEmpty()) {
throw new IllegalStateException("Expected only keys %s, but found additional keys %s. Path: %s"
.formatted(new TreeSet<>(Arrays.asList(allowedKeys)), availableKeys, path));
}
}
private static final class JsonPath {
private final String path;
private JsonPath(String path) {
this.path = path;
}
JsonPath resolve(String path) {
if (this.path.endsWith(".")) {
return new JsonPath(this.path + path);
}
return new JsonPath(this.path + "." + path);
}
JsonPath index(int index) {
return resolve("[%d]".formatted(index));
}
@Override
public String toString() {
return this.path;
}
static JsonPath root() {
return new JsonPath(".");
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free