diff options
author | freva <valerijf@yahoo-inc.com> | 2017-02-15 11:09:16 +0100 |
---|---|---|
committer | freva <valerijf@yahoo-inc.com> | 2017-02-15 11:09:16 +0100 |
commit | 44135dc69675b35f54ef4aff83a404654baa446a (patch) | |
tree | e2c7d1b809169f43ee7a92057fbed2f7f93f741b /document | |
parent | 34f42884c371a9e7936dd07b2e84640b0c2d1290 (diff) |
Updated serializer to new format, removed parsing of old format, fixed tests
Diffstat (limited to 'document')
6 files changed, 31 insertions, 106 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java b/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java index da65ae9c357..02615444037 100644 --- a/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java +++ b/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java @@ -142,7 +142,7 @@ public class DocumentUpdateJsonSerializer } else if (update instanceof RemoveFieldPathUpdate) { } else { - throw new RuntimeException("Unsupported fieldpaths operation: " + update.getClass().getName()); + throw new RuntimeException("Unsupported fieldpath operation: " + update.getClass().getName()); } generator.writeEndObject(); // Ends fieldpath object diff --git a/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java b/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java index 855fabc7a63..c15e61d10f1 100644 --- a/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java +++ b/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java @@ -35,9 +35,6 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; -import static com.yahoo.document.json.readers.MapReader.MAP_KEY; -import static com.yahoo.document.json.readers.MapReader.MAP_VALUE; - /** * @author Steinar Knutsen * @author Vegard Sjonfjell @@ -186,18 +183,14 @@ public class JsonSerializationHelper { public static <K extends FieldValue, V extends FieldValue> void serializeMapField(FieldWriter fieldWriter, JsonGenerator generator, FieldBase field, MapFieldValue<K, V> map) { fieldNameIfNotNull(generator, field); wrapIOException(() -> { - generator.writeStartArray(); + generator.writeStartObject(); for (Map.Entry<K, V> entry : map.entrySet()) { - generator.writeStartObject(); - generator.writeFieldName(MAP_KEY); - entry.getKey().serialize(null, fieldWriter); - generator.writeFieldName(MAP_VALUE); + generator.writeFieldName(entry.getKey().toString()); entry.getValue().serialize(null, fieldWriter); - generator.writeEndObject(); } - generator.writeEndArray(); + generator.writeEndObject(); }); } 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 15fc91c274e..412726b051b 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,7 +17,6 @@ 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; @@ -25,50 +24,11 @@ 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"; - 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) { + public static void fillMap(TokenBuffer buffer, MapFieldValue parent) { JsonToken token = buffer.currentToken(); int initNesting = buffer.nesting(); expectObjectStart(token); diff --git a/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java b/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java index 76f54d5959f..f0251f9b494 100644 --- a/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java +++ b/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java @@ -328,12 +328,12 @@ public class DocumentUpdateJsonSerializerTest { " 'update': 'DOCUMENT_ID',", " 'fields': {", " 'string_map': {", - " 'assign': [", - " { 'key': 'conversion gel', 'value': 'deadly'},", - " { 'key': 'repulsion gel', 'value': 'safe'},", - " { 'key': 'propulsion gel', 'value': 'insufficient data'}", - " ]", - " }", + " 'assign': { ", + " 'conversion gel': 'deadly',", + " 'repulsion gel': 'safe',", + " 'propulsion gel': 'insufficient data'", + " }", + " }", " }", "}" )); @@ -374,12 +374,10 @@ public class DocumentUpdateJsonSerializerTest { " {", " 'assign': {", " 'deep_map{my_field}': {", - " 'value': [", - " {", - " 'key': 'my_key',", - " 'value': 'my_value'", - " }", - " ],", + " 'value': {", + " 'key': 'my_key',", + " 'value': 'my_value'", + " },", " 'createmissingpath': true,", " 'removeifzero': true", " },", @@ -417,12 +415,10 @@ public class DocumentUpdateJsonSerializerTest { " {", " 'assign': {", " 'deep_map{my_field}': {", - " 'value': [", - " {", - " 'key': 'my_key',", - " 'value': 'my_value'", - " }", - " ],", + " 'value': {", + " 'my_key': 'my value',", + " 'my_new_key': 'new value'", + " },", " 'createmissingpath': true,", " 'removeifzero': true", " }", diff --git a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java index 3ad6f270bff..283a65a71ba 100644 --- a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java +++ b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java @@ -477,9 +477,8 @@ public class JsonReaderTestCase { public final void testMap() throws IOException { InputStream rawDoc = new ByteArrayInputStream( Utf8.toBytes("{\"put\": \"id:unittest:testmap::whee\"," - + " \"fields\": { \"actualmap\": [" - + " { \"key\": \"nalle\", \"value\": \"kalle\"}," - + " { \"key\": \"tralle\", \"value\": \"skalle\"} ]}}")); + + " \"fields\": { \"actualmap\": {" + + " \"nalle\": \"kalle\", \"tralle\": \"skalle\"}}}")); JsonReader r = new JsonReader(types, rawDoc, parserFactory); DocumentParseInfo parseInfo = r.parseDocument().get(); DocumentType docType = r.readDocumentType(parseInfo.documentId); @@ -554,9 +553,7 @@ public class JsonReaderTestCase { public final void testMapStringToArrayOfInt() throws IOException { InputStream rawDoc = new ByteArrayInputStream( Utf8.toBytes("{\"put\": \"id:unittest:testMapStringToArrayOfInt::whee\"," - + " \"fields\": { \"actualMapStringToArrayOfInt\": [" - + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" - + "]}}")); + + " \"fields\": { \"actualMapStringToArrayOfInt\": { \"bamse\": [1, 2, 3] }}}")); JsonReader r = new JsonReader(types, rawDoc, parserFactory); DocumentParseInfo parseInfo = r.parseDocument().get(); DocumentType docType = r.readDocumentType(parseInfo.documentId); @@ -595,9 +592,7 @@ public class JsonReaderTestCase { InputStream rawDoc = new ByteArrayInputStream( Utf8.toBytes("{\"update\": \"id:unittest:testMapStringToArrayOfInt::whee\"," + " \"fields\": { \"actualMapStringToArrayOfInt\": {" - + " \"assign\": [" - + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" - + "]}}}")); + + " \"assign\": { \"bamse\": [1, 2, 3] }}}}")); JsonReader r = new JsonReader(types, rawDoc, parserFactory); DocumentParseInfo parseInfo = r.parseDocument().get(); DocumentType docType = r.readDocumentType(parseInfo.documentId); diff --git a/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java index 4928f1be374..f71528d8b24 100644 --- a/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java +++ b/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java @@ -34,12 +34,8 @@ import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.HashMap; -import java.util.List; import java.util.Map; -import static com.yahoo.document.json.readers.MapReader.MAP_KEY; -import static com.yahoo.document.json.readers.MapReader.MAP_VALUE; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; @@ -224,9 +220,7 @@ public class JsonWriterTestCase { @Test public void mapTest() throws IOException { - String fields = "{ \"actualmap\": [" - + " { \"key\": \"nalle\", \"value\": \"kalle\"}," - + " { \"key\": \"tralle\", \"value\": \"skalle\"} ]}"; + String fields = "{ \"actualmap\": { \"nalle\": \"kalle\", \"tralle\": \"skalle\" }}"; String docId = "id:unittest:testmap::whee"; Document doc = readDocumentFromJson(docId, fields); // we have to do everything by hand to check, as maps are unordered, but @@ -238,21 +232,9 @@ public class JsonWriterTestCase { // and from here on down there will be lots of unchecked casting and // other fun. This is OK here, because if the casts fail, the should and // will fail anyway - List<?> inputMap = (List<?>) m.readValue(Utf8.toBytes(fields), Map.class).get("actualmap"); - List<?> generatedMap = (List<?>) ((Map<?, ?>) generated.get("fields")).get("actualmap"); - assertEquals(populateMap(inputMap), populateMap(generatedMap)); - } - - // should very much blow up if the assumptions are incorrect - @SuppressWarnings("rawtypes") - private Map<Object, Object> populateMap(List<?> actualMap) { - Map<Object, Object> m = new HashMap<>(); - for (Object o : actualMap) { - Object key = ((Map) o).get(MAP_KEY); - Object value = ((Map) o).get(MAP_VALUE); - m.put(key, value); - } - return m; + Map<?, ?> inputMap = (Map<?, ?> ) m.readValue(Utf8.toBytes(fields), Map.class).get("actualmap"); + Map<?, ?> generatedMap = (Map<?, ?>) ((Map<?, ?>) generated.get("fields")).get("actualmap"); + assertEquals(inputMap, generatedMap); } @Test @@ -275,8 +257,7 @@ public class JsonWriterTestCase { @Test public final void stringToArrayOfIntMapTest() throws IOException { String docId = "id:unittest:testMapStringToArrayOfInt::whee"; - String fields = "{ \"actualMapStringToArrayOfInt\": [" - + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" + "]}"; + String fields = "{ \"actualMapStringToArrayOfInt\": { \"bamse\": [1, 2, 3] }}"; Document doc = readDocumentFromJson(docId, fields); // we have to do everything by hand to check, as maps are unordered, but // are serialized as an ordered structure @@ -287,9 +268,9 @@ public class JsonWriterTestCase { // and from here on down there will be lots of unchecked casting and // other fun. This is OK here, because if the casts fail, the should and // will fail anyway - List<?> inputMap = (List<?>) m.readValue(Utf8.toBytes(fields), Map.class).get("actualMapStringToArrayOfInt"); - List<?> generatedMap = (List<?>) ((Map<?, ?>) generated.get("fields")).get("actualMapStringToArrayOfInt"); - assertEquals(populateMap(inputMap), populateMap(generatedMap)); + Map<?, ?> inputMap = (Map<?, ?>) m.readValue(Utf8.toBytes(fields), Map.class).get("actualMapStringToArrayOfInt"); + Map<?, ?> generatedMap = (Map<?, ?> ) ((Map<?, ?>) generated.get("fields")).get("actualMapStringToArrayOfInt"); + assertEquals(inputMap, generatedMap); } private Document readDocumentFromJson(String docId, String fields) throws IOException { |