With the 2.x SDK, I have not yet identified an equivalent provided by the SDK. As an exercise I decided to solve this with switch expressions
This implementation will marshal maps, records, primitives, byte arrays, collection types and will call toString()
on unknown types. The only datatype I did not implement was a byte set (BS) as it's not as trivial to determine unique byte array values.
You could add support for bean properties, but I figured it would be error prone and decided to just support toString
in default.
Have included unmarshal
as an exercise also.
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
public class AttributeValueConverter {
public static AttributeValue fromCollection(Collection<? extends Object> c) {
if (c instanceof Set s && s.stream().allMatch(n -> n instanceof String)) {
var vals = c.stream()
.map(n -> (String) n)
.collect(toList());
return AttributeValue.fromSs(vals);
}
var vals = c.stream()
.map(x -> marshal(x))
.collect(toList());
return AttributeValue.fromL(vals);
}
public static AttributeValue fromMap(Map<? extends Object, ? extends Object> m) {
var vals = new HashMap<String, AttributeValue>();
m.forEach((k, v) ->
vals.put(k.toString(), marshal(v))
);
return AttributeValue.fromM(vals);
}
public static AttributeValue fromRecord(Record r) {
var vals = new HashMap<String, AttributeValue>();
var components = r.getClass().getRecordComponents();
for (var component : components) {
try {
vals.put(
component.getName(),
marshal(component.getAccessor().invoke(r))
);
} catch (IllegalAccessException | InvocationTargetException ex) {
// will happen if the class is not accessible
}
}
return AttributeValue.fromM(vals);
}
public static AttributeValue marshal(Object obj) {
return switch (obj) {
case null -> AttributeValue.fromNul(true);
case Optional opt -> opt.isPresent()
? marshal(opt.get())
: AttributeValue.fromNul(true);
case Boolean b -> AttributeValue.fromBool(b);
case Number n -> AttributeValue.fromN(n.toString());
case String s -> AttributeValue.fromS(s);
// string representations of temporals are mostly what you want
//case Temporal t -> AttributeValue.fromS(t.toString());
case Collection c -> fromCollection(c);
case Map m -> fromMap(m);
case Record r -> fromRecord(r);
case Object o when o.getClass().isArray() -> {
var ctype = obj.getClass().getComponentType().getTypeName();
if ("byte".equals(ctype)) {
yield AttributeValue.fromB(
SdkBytes.fromByteArray((byte[]) obj)
);
}
yield fromCollection(Arrays.asList((Object[]) obj));
}
default -> AttributeValue.fromS(obj.toString());
};
}
private static final Pattern INT_PATTERN = Pattern.compile("^-?[0-9]+$");
private static Number parseNumber(String n) {
if (INT_PATTERN.matcher(n).matches()) {
if (n.length() < 10) {
return Integer.valueOf(n);
}
return Long.valueOf(n);
}
return Double.valueOf(n);
}
public static Object unmarshal(AttributeValue av) {
return switch (av.type()) {
case B -> av.b().asByteArray();
case BOOL -> av.bool();
case BS -> av.bs().stream()
.map(SdkBytes::asByteArray)
.collect(toList());
case L -> av.l().stream()
.map(AttributeValueConverter::unmarshal)
.collect(toList());
case M -> av.m().entrySet().stream()
.collect(toMap(
Entry::getKey,
n -> Optional.ofNullable(unmarshal(n.getValue()))
));
case N -> parseNumber(av.n());
case NS -> av.ns().stream()
.map(AttributeValueConverter::parseNumber)
.collect(toSet());
case NUL -> null;
case S -> av.s();
case SS -> Set.copyOf(av.ss());
case UNKNOWN_TO_SDK_VERSION ->
throw new UnsupportedOperationException("Type not supported");
};
}
}