com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConvertedJson Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-java-sdk-dynamodb Show documentation
Show all versions of aws-java-sdk-dynamodb Show documentation
The AWS Java SDK for Amazon DynamoDB module holds the client classes that are used for communicating with Amazon DynamoDB Service
/*
* Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://aws.amazon.com/apache2.0
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and
* limitations under the License.
*/
package com.amazonaws.services.dynamodbv2.datamodeling;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
/**
* A simple JSON converter that uses the Jackson JSON processor.
*
* It shares all limitations of that library. For more information about
* Jackson, see: http://wiki.fasterxml.com/JacksonHome
*
*
* @DynamoDBTypeConvertedJson
* public Currency getCurrency()
*
*
* Where,
*
* public class Currency {
* private Double amount;
* private String unit;
*
* public Double getAmount() { return amount; }
* public void setAmount(Double amount) { this.amount = amount; }
*
* public String getUnit() { return unit; }
* public void setUnit(String unit) { this.unit = unit; }
* }
*
*
* Would write the following value to DynamoDB given,
*
* Currency(79.99,"USD")
= "{\"amount\":79.99,\"unit\":\"USD\"}"
*
*
* @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted
*/
@DynamoDBTypeConverted(converter=DynamoDBTypeConvertedJson.Converter.class)
@DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.S)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
public @interface DynamoDBTypeConvertedJson {
/**
* The value type to use when calling the JSON mapper's {@code readValue};
* a value of {@code Void.class} indicates to use the getter's type.
*/
Class targetType() default void.class;
/**
* JSON type converter.
*/
final class Converter implements DynamoDBTypeConverter {
private static final ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
private final Class targetType;
public Converter(Class targetType, DynamoDBTypeConvertedJson annotation) {
this.targetType = annotation.targetType() == void.class ? targetType : (Class)annotation.targetType();
}
@Override
public final String convert(final T object) {
try {
return mapper.writeValueAsString(object);
} catch (final Exception e) {
throw new DynamoDBMappingException("Unable to write object to JSON", e);
}
}
@Override
public final T unconvert(final String object) {
try {
return mapper.readValue(object, targetType);
} catch (final Exception e) {
throw new DynamoDBMappingException("Unable to read JSON string", e);
}
}
}
}