Search in sources :

Example 11 with JSONObject

use of org.springframework.boot.configurationprocessor.json.JSONObject in project spring-boot by spring-projects.

the class JsonMarshaller method write.

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()));
        outputStream.write(object.toString(2).getBytes(StandardCharsets.UTF_8));
    } catch (Exception ex) {
        if (ex instanceof IOException) {
            throw (IOException) ex;
        }
        if (ex instanceof RuntimeException) {
            throw (RuntimeException) ex;
        }
        throw new IllegalStateException(ex);
    }
}
Also used : JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject) IOException(java.io.IOException) IOException(java.io.IOException)

Example 12 with JSONObject

use of org.springframework.boot.configurationprocessor.json.JSONObject in project spring-boot by spring-projects.

the class JsonMarshaller method toValueProvider.

private ItemHint.ValueProvider toValueProvider(JSONObject object) throws Exception {
    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);
}
Also used : JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject) HashMap(java.util.HashMap) JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject)

Example 13 with JSONObject

use of org.springframework.boot.configurationprocessor.json.JSONObject in project spring-boot by spring-projects.

the class JsonMarshaller method toItemDeprecation.

private ItemDeprecation toItemDeprecation(JSONObject object) throws Exception {
    if (object.has("deprecation")) {
        JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
        ItemDeprecation deprecation = new ItemDeprecation();
        deprecation.setLevel(deprecationJsonObject.optString("level", null));
        deprecation.setReason(deprecationJsonObject.optString("reason", null));
        deprecation.setReplacement(deprecationJsonObject.optString("replacement", null));
        return deprecation;
    }
    return object.optBoolean("deprecated") ? new ItemDeprecation() : null;
}
Also used : JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject)

Example 14 with JSONObject

use of org.springframework.boot.configurationprocessor.json.JSONObject in project spring-boot by spring-projects.

the class MergeMetadataGenerationTests method writePropertyDeprecation.

private void writePropertyDeprecation(ItemMetadata... items) throws Exception {
    File additionalMetadataFile = createAdditionalMetadataFile();
    JSONArray propertiesArray = new JSONArray();
    for (ItemMetadata item : items) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", item.getName());
        if (item.getType() != null) {
            jsonObject.put("type", item.getType());
        }
        ItemDeprecation deprecation = item.getDeprecation();
        if (deprecation != null) {
            JSONObject deprecationJson = new JSONObject();
            if (deprecation.getReason() != null) {
                deprecationJson.put("reason", deprecation.getReason());
            }
            if (deprecation.getReplacement() != null) {
                deprecationJson.put("replacement", deprecation.getReplacement());
            }
            jsonObject.put("deprecation", deprecationJson);
        }
        propertiesArray.put(jsonObject);
    }
    JSONObject additionalMetadata = new JSONObject();
    additionalMetadata.put("properties", propertiesArray);
    writeMetadata(additionalMetadataFile, additionalMetadata);
}
Also used : JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject) ItemDeprecation(org.springframework.boot.configurationprocessor.metadata.ItemDeprecation) JSONArray(org.springframework.boot.configurationprocessor.json.JSONArray) File(java.io.File) ItemMetadata(org.springframework.boot.configurationprocessor.metadata.ItemMetadata)

Example 15 with JSONObject

use of org.springframework.boot.configurationprocessor.json.JSONObject in project spring-boot by spring-projects.

the class MergeMetadataGenerationTests method writeAdditionalMetadata.

private void writeAdditionalMetadata(ItemMetadata... metadata) throws Exception {
    TestJsonConverter converter = new TestJsonConverter();
    File additionalMetadataFile = createAdditionalMetadataFile();
    JSONObject additionalMetadata = new JSONObject();
    JSONArray properties = new JSONArray();
    for (ItemMetadata itemMetadata : metadata) {
        properties.put(converter.toJsonObject(itemMetadata));
    }
    additionalMetadata.put("properties", properties);
    writeMetadata(additionalMetadataFile, additionalMetadata);
}
Also used : JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject) JSONArray(org.springframework.boot.configurationprocessor.json.JSONArray) File(java.io.File) TestJsonConverter(org.springframework.boot.configurationprocessor.metadata.TestJsonConverter) ItemMetadata(org.springframework.boot.configurationprocessor.metadata.ItemMetadata)

Aggregations

JSONObject (org.springframework.boot.configurationprocessor.json.JSONObject)17 JSONArray (org.springframework.boot.configurationprocessor.json.JSONArray)5 File (java.io.File)4 ItemMetadata (org.springframework.boot.configurationprocessor.metadata.ItemMetadata)2 TestJsonConverter (org.springframework.boot.configurationprocessor.metadata.TestJsonConverter)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1 ConfigurationMetadata (org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata)1 ItemDeprecation (org.springframework.boot.configurationprocessor.metadata.ItemDeprecation)1