Search in sources :

Example 1 with JSONArray

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

the class MergeMetadataGenerationTests method mergingOfAdditionalMetadata.

@Test
void mergingOfAdditionalMetadata() throws Exception {
    File metaInfDirectory = new File(getCompiler().getOutputLocation(), "META-INF");
    metaInfDirectory.mkdirs();
    File additionalMetadataFile = new File(metaInfDirectory, "additional-spring-configuration-metadata.json");
    additionalMetadataFile.createNewFile();
    JSONObject property = new JSONObject();
    property.put("name", "foo");
    property.put("type", "java.lang.String");
    property.put("sourceType", AdditionalMetadata.class.getName());
    JSONArray properties = new JSONArray();
    properties.put(property);
    JSONObject additionalMetadata = new JSONObject();
    additionalMetadata.put("properties", properties);
    FileWriter writer = new FileWriter(additionalMetadataFile);
    writer.append(additionalMetadata.toString(2));
    writer.flush();
    writer.close();
    ConfigurationMetadata metadata = compile(SimpleProperties.class);
    assertThat(metadata).has(Metadata.withProperty("simple.comparator"));
    assertThat(metadata).has(Metadata.withProperty("foo", String.class).fromSource(AdditionalMetadata.class));
}
Also used : JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject) FileWriter(java.io.FileWriter) JSONArray(org.springframework.boot.configurationprocessor.json.JSONArray) File(java.io.File) ConfigurationMetadata(org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata) Test(org.junit.jupiter.api.Test)

Example 2 with JSONArray

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

the class JsonMarshaller method readItemValue.

private Object readItemValue(Object value) throws Exception {
    if (value instanceof JSONArray) {
        JSONArray array = (JSONArray) value;
        Object[] content = new Object[array.length()];
        for (int i = 0; i < array.length(); i++) {
            content[i] = array.get(i);
        }
        return content;
    }
    return value;
}
Also used : JSONArray(org.springframework.boot.configurationprocessor.json.JSONArray) JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject)

Example 3 with JSONArray

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

the class JsonMarshaller method read.

public ConfigurationMetadata read(InputStream inputStream) throws Exception {
    ConfigurationMetadata metadata = new ConfigurationMetadata();
    JSONObject object = new JSONObject(toString(inputStream));
    JSONArray groups = object.optJSONArray("groups");
    if (groups != null) {
        for (int i = 0; i < groups.length(); i++) {
            metadata.add(toItemMetadata((JSONObject) groups.get(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), 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)));
        }
    }
    return metadata;
}
Also used : JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject) JSONArray(org.springframework.boot.configurationprocessor.json.JSONArray)

Example 4 with JSONArray

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

the class JsonMarshaller method toItemHint.

private ItemHint toItemHint(JSONObject object) throws Exception {
    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)));
        }
    }
    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)));
        }
    }
    return new ItemHint(name, values, providers);
}
Also used : JSONObject(org.springframework.boot.configurationprocessor.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.springframework.boot.configurationprocessor.json.JSONArray)

Example 5 with JSONArray

use of org.springframework.boot.configurationprocessor.json.JSONArray 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)

Aggregations

JSONArray (org.springframework.boot.configurationprocessor.json.JSONArray)8 JSONObject (org.springframework.boot.configurationprocessor.json.JSONObject)7 File (java.io.File)3 ItemMetadata (org.springframework.boot.configurationprocessor.metadata.ItemMetadata)2 FileWriter (java.io.FileWriter)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 ConfigurationMetadata (org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata)1 ItemDeprecation (org.springframework.boot.configurationprocessor.metadata.ItemDeprecation)1 TestJsonConverter (org.springframework.boot.configurationprocessor.metadata.TestJsonConverter)1