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
+ JsonNodeToAttributeValueMapConverter attributeValueMapConverter = JsonNodeToAttributeValueMapConverter .instance ();
71
+ return input .visit (attributeValueMapConverter );
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
+ if (value == null ) {
87
+ return null ;
88
+ }
89
+ Map <String , JsonNode > jsonNodeMap = new LinkedHashMap <>();
90
+ value .entrySet ().forEach (
91
+ k -> {
92
+ JsonNode jsonNode = this .convert (EnhancedAttributeValue .fromAttributeValue (k .getValue ()));
93
+ jsonNodeMap .put (k .getKey (), jsonNode == null ? NullJsonNode .instance () : jsonNode );
94
+ });
95
+ return new ObjectJsonNode (jsonNodeMap );
96
+ }
97
+
98
+ @ Override
99
+ public JsonNode convertString (String value ) {
100
+ if (value == null ) {
101
+ return null ;
102
+ }
103
+ return new StringJsonNode (value );
104
+ }
105
+
106
+ @ Override
107
+ public JsonNode convertNumber (String value ) {
108
+ if (value == null ) {
109
+ return null ;
110
+ }
111
+ return new NumberJsonNode (value );
112
+ }
113
+
114
+ @ Override
115
+ public JsonNode convertBytes (SdkBytes value ) {
116
+ if (value == null ) {
117
+ return null ;
118
+ }
119
+ return new StringJsonNode (value .asUtf8String ());
120
+ }
121
+
122
+ @ Override
123
+ public JsonNode convertBoolean (Boolean value ) {
124
+ if (value == null ) {
125
+ return null ;
126
+ }
127
+ return new BooleanJsonNode (value );
128
+ }
129
+
130
+ @ Override
131
+ public JsonNode convertSetOfStrings (List <String > value ) {
132
+ if (value == null ) {
133
+ return null ;
134
+ }
135
+ return new ArrayJsonNode (value .stream ().map (s -> new StringJsonNode (s )).collect (Collectors .toList ()));
136
+ }
137
+
138
+ @ Override
139
+ public JsonNode convertSetOfNumbers (List <String > value ) {
140
+ if (value == null ) {
141
+ return null ;
142
+ }
143
+ return new ArrayJsonNode (value .stream ().map (s -> new NumberJsonNode (s )).collect (Collectors .toList ()));
144
+ }
145
+
146
+ @ Override
147
+ public JsonNode convertSetOfBytes (List <SdkBytes > value ) {
148
+ if (value == null ) {
149
+ return null ;
150
+ }
151
+ return new ArrayJsonNode (value .stream ().map (sdkByte ->
152
+ new StringJsonNode (sdkByte .asUtf8String ())
153
+ ).collect (Collectors .toList ()));
154
+ }
155
+
156
+ @ Override
157
+ public JsonNode convertListOfAttributeValues (List <AttributeValue > value ) {
158
+ if (value == null ) {
159
+ return null ;
160
+ }
161
+ return new ArrayJsonNode (value .stream ().map (
162
+ attributeValue -> {
163
+ EnhancedAttributeValue enhancedAttributeValue = EnhancedAttributeValue .fromAttributeValue (attributeValue );
164
+ return enhancedAttributeValue .isNull () ? NullJsonNode .instance () : enhancedAttributeValue .convert (VISITOR );
165
+ }).collect (Collectors .toList ()));
166
+ }
167
+ }
168
+ }
0 commit comments