|
| 1 | +/* |
| 2 | + * Copyright (C) 2007-2017, GoodData(R) Corporation. All rights reserved. |
| 3 | + * This source code is licensed under the BSD-style license found in the |
| 4 | + * LICENSE.txt file in the root directory of this source tree. |
| 5 | + */ |
| 6 | +package com.gooddata.executeafm.result; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.annotation.JsonValue; |
| 9 | +import com.fasterxml.jackson.core.JsonParser; |
| 10 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 11 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 12 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 13 | +import com.fasterxml.jackson.databind.JsonNode; |
| 14 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.UncheckedIOException; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Spliterator; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +import static java.util.Spliterators.spliteratorUnknownSize; |
| 23 | +import static java.util.stream.StreamSupport.stream; |
| 24 | + |
| 25 | +/** |
| 26 | + * Data of {@link ExecutionResult}, can be of three basic kinds - {@link #NULL}, list and simple value. |
| 27 | + */ |
| 28 | +@JsonDeserialize(using = Data.DataDeserializer.class) |
| 29 | +public interface Data { |
| 30 | + |
| 31 | + Data NULL = new Data() { |
| 32 | + @Override |
| 33 | + public boolean isList() { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public boolean isValue() { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + @JsonValue |
| 44 | + public String textValue() { |
| 45 | + return null; |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + /** |
| 50 | + * @return true if this instance is of kind list, false otherwise |
| 51 | + */ |
| 52 | + boolean isList(); |
| 53 | + |
| 54 | + /** |
| 55 | + * @return true if this instance is of kind value, false otherwise |
| 56 | + */ |
| 57 | + boolean isValue(); |
| 58 | + |
| 59 | + /** |
| 60 | + * @return true if this instance is the same as {@link #NULL}, false otherwise |
| 61 | + */ |
| 62 | + default boolean isNull() { |
| 63 | + return this == NULL; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @return text data value, throws exception for data which can't be represented as text |
| 68 | + */ |
| 69 | + String textValue(); |
| 70 | + |
| 71 | + /** |
| 72 | + * @return this instance cast to List, may throw exception if this instance is not of kind list. |
| 73 | + */ |
| 74 | + default List<Data> asList() { |
| 75 | + throw new UnsupportedOperationException("This is not a list"); |
| 76 | + } |
| 77 | + |
| 78 | + class DataDeserializer extends JsonDeserializer<Data> { |
| 79 | + @Override |
| 80 | + public Data deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException { |
| 81 | + final JsonNode root = jp.readValueAsTree(); |
| 82 | + if (root.isArray()) { |
| 83 | + final List<Data> list = stream(spliteratorUnknownSize(root.elements(), Spliterator.ORDERED), false) |
| 84 | + .map(elem -> { |
| 85 | + try { |
| 86 | + return ctxt.readValue(elem.traverse(jp.getCodec()), Data.class); |
| 87 | + } catch (IOException e) { |
| 88 | + throw new UncheckedIOException(e); |
| 89 | + } |
| 90 | + }).collect(Collectors.toList()); |
| 91 | + return new DataList(list); |
| 92 | + } else if (root.isTextual()) { |
| 93 | + return new DataValue(root.textValue()); |
| 94 | + } else if (root.isNull()) { |
| 95 | + return NULL; |
| 96 | + } else { |
| 97 | + throw JsonMappingException.from(jp, "Unknown value of type: " + root.getNodeType()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Data getNullValue(final DeserializationContext ctxt) throws JsonMappingException { |
| 103 | + return NULL; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments