summaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorfreva <valerijf@yahoo-inc.com>2017-02-20 08:55:48 +0100
committerfreva <valerijf@yahoo-inc.com>2017-02-20 08:55:48 +0100
commit1af437c88da001d9ea12aac47a56ea9e22c910d5 (patch)
tree064e8b5fe0400f552370e5b56a4cf05e631aad2c /document
parent401dd08db643cefb25ef1cc891bca1c3e2013f1d (diff)
Fix reading map from old array format
Diffstat (limited to 'document')
-rw-r--r--document/src/main/java/com/yahoo/document/json/readers/MapReader.java42
1 files changed, 41 insertions, 1 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/readers/MapReader.java b/document/src/main/java/com/yahoo/document/json/readers/MapReader.java
index 412726b051b..1e7e2353fab 100644
--- a/document/src/main/java/com/yahoo/document/json/readers/MapReader.java
+++ b/document/src/main/java/com/yahoo/document/json/readers/MapReader.java
@@ -17,6 +17,7 @@ import com.yahoo.document.json.TokenBuffer;
import com.yahoo.document.update.MapValueUpdate;
import com.yahoo.document.update.ValueUpdate;
+import static com.yahoo.document.json.readers.JsonParserHelpers.expectArrayStart;
import static com.yahoo.document.json.readers.JsonParserHelpers.expectObjectEnd;
import static com.yahoo.document.json.readers.JsonParserHelpers.expectObjectStart;
import static com.yahoo.document.json.readers.SingleValueReader.readAtomic;
@@ -24,11 +25,50 @@ import static com.yahoo.document.json.readers.SingleValueReader.readSingleUpdate
import static com.yahoo.document.json.readers.SingleValueReader.readSingleValue;
public class MapReader {
+ public static final String MAP_KEY = "key";
+ public static final String MAP_VALUE = "value";
public static final String UPDATE_ELEMENT = "element";
public static final String UPDATE_MATCH = "match";
- @SuppressWarnings({ "rawtypes", "cast", "unchecked" })
public static void fillMap(TokenBuffer buffer, MapFieldValue parent) {
+ if (buffer.currentToken() == JsonToken.START_ARRAY) {
+ MapReader.fillMapFromArray(buffer, parent);
+ } else {
+ MapReader.fillMapFromObject(buffer, parent);
+ }
+ }
+
+ @SuppressWarnings({ "rawtypes", "cast", "unchecked" })
+ public static void fillMapFromArray(TokenBuffer buffer, MapFieldValue parent) {
+ JsonToken token = buffer.currentToken();
+ int initNesting = buffer.nesting();
+ expectArrayStart(token);
+ token = buffer.next();
+ DataType keyType = parent.getDataType().getKeyType();
+ DataType valueType = parent.getDataType().getValueType();
+ while (buffer.nesting() >= initNesting) {
+ FieldValue key = null;
+ FieldValue value = null;
+ expectObjectStart(token);
+ token = buffer.next();
+ for (int i = 0; i < 2; ++i) {
+ if (MAP_KEY.equals(buffer.currentName())) {
+ key = readSingleValue(buffer, keyType);
+ } else if (MAP_VALUE.equals(buffer.currentName())) {
+ value = readSingleValue(buffer, valueType);
+ }
+ token = buffer.next();
+ }
+ Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
+ parent.put(key, value);
+
+ expectObjectEnd(token);
+ token = buffer.next(); // array end or next entry
+ }
+ }
+
+ @SuppressWarnings({ "rawtypes", "cast", "unchecked" })
+ public static void fillMapFromObject(TokenBuffer buffer, MapFieldValue parent) {
JsonToken token = buffer.currentToken();
int initNesting = buffer.nesting();
expectObjectStart(token);