Guest User

Untitled

a guest
Jan 12th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.util.HashMap;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. import javax.json.Json;
  8. import javax.json.JsonArray;
  9. import javax.json.JsonArrayBuilder;
  10. import javax.json.JsonNumber;
  11. import javax.json.JsonObject;
  12. import javax.json.JsonObjectBuilder;
  13. import javax.json.JsonString;
  14. import javax.json.JsonValue;
  15.  
  16. import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
  17.  
  18. public class DynamoDBUtil {
  19.  
  20. public static void addList(String key, JsonObjectBuilder objectBuilder, List<JsonObject> items) {
  21. if (!items.isEmpty()) {
  22. JsonArrayBuilder builder = Json.createArrayBuilder();
  23. items.forEach(i -> builder.add(i));
  24. objectBuilder.add(key, builder.build());
  25. }
  26.  
  27. }
  28.  
  29. public static JsonArray toJson(List<AttributeValue> attributeValues) {
  30. if (attributeValues == null) {
  31. return null;
  32. }
  33. JsonArrayBuilder valueBuilder = Json.createArrayBuilder();
  34. for (AttributeValue a : attributeValues) {
  35. add(toJson(a), valueBuilder);
  36. }
  37. return valueBuilder.build();
  38. }
  39.  
  40. public static JsonObject toJson(Map<String, AttributeValue> attributeValues) {
  41. if (attributeValues == null) {
  42. return null;
  43. }
  44. JsonObjectBuilder valueBuilder = Json.createObjectBuilder();
  45. for (Map.Entry<String, AttributeValue> a : attributeValues.entrySet()) {
  46. add(a.getKey(), toJson(a.getValue()), valueBuilder);
  47. }
  48. return valueBuilder.build();
  49. }
  50.  
  51. public static void add(String key, Object value, JsonObjectBuilder object) {
  52. if (value instanceof JsonValue) {
  53. object.add(key, (JsonValue) value);
  54. // with json-p 1.0 can't create JsonString or JsonNumber so simply setting JsonValue not an option.
  55. } else if (value instanceof String) {
  56. object.add(key, (String) value);
  57. } else if (value instanceof BigDecimal) {
  58. object.add(key, (BigDecimal) value);
  59. } else if (value instanceof Boolean) {
  60. object.add(key, (Boolean) value);
  61. } else if (value.equals(JsonValue.NULL)) {
  62. object.addNull(key);
  63. }
  64.  
  65. }
  66.  
  67. public static void add(Object value, JsonArrayBuilder array) {
  68. if (value instanceof JsonValue) {
  69. array.add((JsonValue) value);
  70. } else if (value instanceof String) {
  71. array.add((String) value);
  72. } else if (value instanceof BigDecimal) {
  73. array.add((BigDecimal) value);
  74. } else if (value instanceof Boolean) {
  75. array.add((Boolean) value);
  76. } else if (value.equals(JsonValue.NULL)) {
  77. array.addNull();
  78. }
  79.  
  80. }
  81.  
  82. public static Object toJson(AttributeValue attributeValue) {
  83. // with json-p 1.1 Json.createValue() can be used.
  84. if (attributeValue == null) {
  85. return null;
  86. }
  87. if (attributeValue.s() != null) {
  88. return attributeValue.s();
  89. }
  90. if (attributeValue.m() != null) {
  91. return toJson(attributeValue.m());
  92. }
  93. if (attributeValue.l() != null) {
  94. return toJson(attributeValue.l());
  95. }
  96. if (attributeValue.n() != null) {
  97. return new BigDecimal(attributeValue.n());
  98. }
  99. if (attributeValue.bool() != null) {
  100. // return attributeValue.bool() ? JsonValue.TRUE : JsonValue.FALSE;
  101. return attributeValue.bool();
  102. }
  103. if (attributeValue.nul() != null && attributeValue.nul()) {
  104. return JsonValue.NULL;
  105. }
  106. /* if (attributeValue.b() != null) {
  107. * return Base64.getEncoder().encodeToString(attributeValue.b().array());
  108. * } */
  109. return null;
  110. }
  111.  
  112. public static Map<String, AttributeValue> toAttribute(JsonObject jsonObject) {
  113. Map<String, AttributeValue> attribute = new HashMap<>();
  114. jsonObject.entrySet().forEach(e -> {
  115. attribute.put(e.getKey(), toAttribute(e.getValue()));
  116. });
  117. return attribute;
  118. }
  119.  
  120. public static List<AttributeValue> toAttribute(JsonArray jsonArray) {
  121. List<AttributeValue> attributes = new LinkedList<>();
  122. jsonArray.forEach(e -> {
  123. attributes.add(toAttribute(e));
  124. });
  125. return attributes;
  126. }
  127.  
  128. public static AttributeValue toAttribute(JsonValue jsonValue) {
  129. if (jsonValue == null) {
  130. return null;
  131. }
  132. switch (jsonValue.getValueType()) {
  133. case STRING:
  134. return AttributeValue.builder().s(((JsonString) jsonValue).getString()).build();
  135. case OBJECT:
  136. return AttributeValue.builder().m(toAttribute((JsonObject) jsonValue)).build();
  137. case ARRAY:
  138. return AttributeValue.builder().l(toAttribute((JsonArray) jsonValue)).build();
  139. case NUMBER:
  140. return AttributeValue.builder().n(((JsonNumber) jsonValue).toString()).build();
  141. case TRUE:
  142. return AttributeValue.builder().bool(true).build();
  143. case FALSE:
  144. return AttributeValue.builder().bool(false).build();
  145. case NULL:
  146. return AttributeValue.builder().nul(true).build();
  147. }
  148.  
  149. return null;
  150. }
  151.  
  152. }
Add Comment
Please, Sign In to add comment