Example usage for com.amazonaws.services.dynamodbv2.document Item toJSON

List of usage examples for com.amazonaws.services.dynamodbv2.document Item toJSON

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document Item toJSON.

Prototype

public String toJSON() 

Source Link

Document

Returns this item as a JSON string.

Usage

From source file:io.helixservice.feature.configuration.dynamo.DynamoConfigResourceLocator.java

License:Open Source License

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private Optional<InputStream> getPropertiesFromDynamoDB(String resourcePath, Optional<InputStream> pResult) {
    Optional<InputStream> result = pResult;

    String environmentName = resourcePath.substring(0, resourcePath.lastIndexOf(APPLICATION_YAML_FILE));
    try {/*w  ww .java2s  .  c o m*/
        Item item = configTable.getItem("environment", environmentName, "service", serviceName);
        if (item == null) {
            LOG.info("No configuration found for environment=" + environmentName
                    + "; creating default empty configuration");
            createEmptyConfiguration(environmentName);
        } else {
            String json = item.toJSON();
            JSONObject jsonObject = new JSONObject(json);
            JSONObject config = jsonObject.getJSONObject("config");

            ObjectMapper objectMapper = new ObjectMapper();
            Map mapOfValues = objectMapper.readValue(config.toString(), Map.class);

            ObjectMapper yamlObjectMapper = new ObjectMapper(new YAMLFactory());
            String yamlResult = yamlObjectMapper.writeValueAsString(mapOfValues);

            result = Optional.of(new StringInputStream(yamlResult));
        }
    } catch (Throwable t) {
        LOG.error("Unable to load configuration from DynamoDB for environment=" + environmentName, t);
    }

    return result;
}

From source file:jp.classmethod.aws.dynamodb.DynamoDbRepository.java

License:Open Source License

<S extends E> S convertItemToDomain(Item item, Class<? extends S> crass) {
    try {// ww w.ja va  2s .co m
        if (item == null) {
            return null;
        }
        String json = item.toJSON();
        return objectMapper.readValue(json, crass);
    } catch (IOException e) {
        throw new IllegalStateException("unable to convert orders JSON to domain object", e);
    }
}