1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License").
5
+ * You may not use this file except in compliance with the License.
6
+ * A copy of the License is located at
7
+ *
8
+ * http://aws.amazon.com/apache2.0
9
+ *
10
+ * or in the "license" file accompanying this file. This file is distributed
11
+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
+ * express or implied. See the License for the specific language governing
13
+ * permissions and limitations under the License.
14
+ */
15
+
16
+ package software .amazon .awssdk .enhanced .dynamodb .internal .converter .attribute ;
17
+
18
+ import java .util .LinkedHashMap ;
19
+ import java .util .List ;
20
+ import java .util .Map ;
21
+ import java .util .stream .Collectors ;
22
+ import software .amazon .awssdk .annotations .Immutable ;
23
+ import software .amazon .awssdk .annotations .SdkInternalApi ;
24
+ import software .amazon .awssdk .annotations .ThreadSafe ;
25
+ import software .amazon .awssdk .core .SdkBytes ;
26
+ import software .amazon .awssdk .enhanced .dynamodb .AttributeConverter ;
27
+ import software .amazon .awssdk .enhanced .dynamodb .AttributeValueType ;
28
+ import software .amazon .awssdk .enhanced .dynamodb .EnhancedType ;
29
+ import software .amazon .awssdk .enhanced .dynamodb .internal .converter .TypeConvertingVisitor ;
30
+ import software .amazon .awssdk .protocols .jsoncore .JsonNode ;
31
+ import software .amazon .awssdk .protocols .jsoncore .internal .ArrayJsonNode ;
32
+ import software .amazon .awssdk .protocols .jsoncore .internal .BooleanJsonNode ;
33
+ import software .amazon .awssdk .protocols .jsoncore .internal .NullJsonNode ;
34
+ import software .amazon .awssdk .protocols .jsoncore .internal .NumberJsonNode ;
35
+ import software .amazon .awssdk .protocols .jsoncore .internal .ObjectJsonNode ;
36
+ import software .amazon .awssdk .protocols .jsoncore .internal .StringJsonNode ;
37
+ import software .amazon .awssdk .services .dynamodb .model .AttributeValue ;
38
+
39
+ /**
40
+ * An Internal converter between JsonNode and {@link AttributeValue}.
41
+ *
42
+ * <p>
43
+ * This converts the Attribute Value read from the DDB to JsonNode.
44
+ */
45
+ @ SdkInternalApi
46
+ @ ThreadSafe
47
+ @ Immutable
48
+ public final class JsonItemAttributeConverter implements AttributeConverter <JsonNode > {
49
+ private static final Visitor VISITOR = new Visitor ();
50
+
51
+ private JsonItemAttributeConverter () {
52
+ }
53
+
54
+ public static JsonItemAttributeConverter create () {
55
+ return new JsonItemAttributeConverter ();
56
+ }
57
+
58
+ @ Override
59
+ public EnhancedType <JsonNode > type () {
60
+ return EnhancedType .of (JsonNode .class );
61
+ }
62
+
63
+ @ Override
64
+ public AttributeValueType attributeValueType () {
65
+ return AttributeValueType .M ;
66
+ }
67
+
68
+ @ Override
69
+ public AttributeValue transformFrom (JsonNode input ) {
70
+ JsonNodeToAttributeValueMapConvertor jsonNodeToAttributeValueMapConvertor = new JsonNodeToAttributeValueMapConvertor ();
71
+ return input .visit (jsonNodeToAttributeValueMapConvertor );
72
+ }
73
+
74
+ @ Override
75
+ public JsonNode transformTo (AttributeValue input ) {
76
+ return EnhancedAttributeValue .fromAttributeValue (input ).convert (VISITOR );
77
+ }
78
+
79
+ private static final class Visitor extends TypeConvertingVisitor <JsonNode > {
80
+ private Visitor () {
81
+ super (JsonNode .class , JsonItemAttributeConverter .class );
82
+ }
83
+
84
+ @ Override
85
+ public JsonNode convertMap (Map <String , AttributeValue > value ) {
86
+ Map <String , JsonNode > jsonNodeMap = new LinkedHashMap <>();
87
+ value .entrySet ().forEach (
88
+ k -> {
89
+ JsonNode jsonNode = this .convert (EnhancedAttributeValue .fromAttributeValue (k .getValue ()));
90
+ jsonNodeMap .put (k .getKey (), jsonNode == null ? NullJsonNode .instance () : jsonNode );
91
+ });
92
+ return new ObjectJsonNode (jsonNodeMap );
93
+ }
94
+
95
+ @ Override
96
+ public JsonNode convertString (String value ) {
97
+ return new StringJsonNode (value );
98
+ }
99
+
100
+ @ Override
101
+ public JsonNode convertNumber (String value ) {
102
+ return new NumberJsonNode (value );
103
+ }
104
+
105
+ @ Override
106
+ public JsonNode convertBytes (SdkBytes value ) {
107
+ return new StringJsonNode (value .asUtf8String ());
108
+ }
109
+
110
+ @ Override
111
+ public JsonNode convertBoolean (Boolean value ) {
112
+ return new BooleanJsonNode (value );
113
+ }
114
+
115
+ @ Override
116
+ public JsonNode convertSetOfStrings (List <String > value ) {
117
+ return new ArrayJsonNode (value .stream ().map (s -> new StringJsonNode (s )).collect (Collectors .toList ()));
118
+ }
119
+
120
+ @ Override
121
+ public JsonNode convertSetOfNumbers (List <String > value ) {
122
+ return new ArrayJsonNode (value .stream ().map (s -> new NumberJsonNode (s )).collect (Collectors .toList ()));
123
+ }
124
+
125
+ @ Override
126
+ public JsonNode convertSetOfBytes (List <SdkBytes > value ) {
127
+ return new ArrayJsonNode (value .stream ().map (sdkByte ->
128
+ new StringJsonNode (sdkByte .asUtf8String ())
129
+ ).collect (Collectors .toList ()));
130
+ }
131
+
132
+ @ Override
133
+ public JsonNode convertListOfAttributeValues (List <AttributeValue > value ) {
134
+ return new ArrayJsonNode (value .stream ().map (
135
+ attributeValue -> EnhancedAttributeValue .fromAttributeValue (
136
+ attributeValue ).convert (VISITOR )).collect (Collectors .toList ()));
137
+
138
+ }
139
+ }
140
+ }
0 commit comments