Fix reading map from old array format
@@ -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);
