22
33import com .fasterxml .jackson .databind .JsonNode ;
44import com .fasterxml .jackson .databind .ObjectMapper ;
5+ import com .fasterxml .jackson .databind .node .ArrayNode ;
56import com .fasterxml .jackson .databind .node .ObjectNode ;
67import com .fasterxml .jackson .databind .node .ValueNode ;
78import edu .berkeley .cs .succinct .DataType ;
@@ -67,28 +68,48 @@ private void flattenJsonTree(String currentPath, JsonNode jsonNode, ByteArrayOut
6768 flattenJsonTree (pathPrefix + entry .getKey (), entry .getValue (), out );
6869 }
6970 } else if (jsonNode .isArray ()) {
70- throw new SerializationException ("Arrays in JSON are not supported yet." );
71+ ArrayNode arrayNode = (ArrayNode ) jsonNode ;
72+ DataType primitiveArrayType = getNodeType (arrayNode .get (0 ));
73+ DataType jsonArrayType = getArrayNodeType (primitiveArrayType );
74+ for (int i = 0 ; i < arrayNode .size (); i ++) {
75+ if (getNodeType (arrayNode .get (i )) != primitiveArrayType )
76+ throw new SerializationException ("Multi Type Arrays in JSON are not supported yet." );
77+ else if (!arrayNode .get (i ).isValueNode ())
78+ throw new SerializationException ("Non primitive types in Arrays in JSON are not supported yet." );
79+ }
80+ for (int i = 0 ; i < arrayNode .size (); i ++) {
81+ ValueNode valueNode = (ValueNode ) arrayNode .get (i );
82+ writeJsonTree (currentPath , valueNode , jsonArrayType , out , true );
83+ }
7184 } else if (jsonNode .isValueNode ()) {
7285 ValueNode valueNode = (ValueNode ) jsonNode ;
73- if (!fieldMapping .containsField (currentPath )) {
74- fieldMapping .put (currentPath , delimiters [currentDelimiterIdx ++], getNodeType (jsonNode ));
75- } else {
76- DataType existingType = fieldMapping .getDataType (currentPath );
77- DataType newType = getNodeType (valueNode );
78- if (existingType != newType ) {
79- DataType encapsulatingType = DataType .encapsulatingType (existingType , newType );
86+ writeJsonTree (currentPath , valueNode , getNodeType (jsonNode ), out , false );
87+ }
88+ }
89+
90+ private void writeJsonTree (String currentPath , ValueNode valueNode , DataType fieldMappingType , ByteArrayOutputStream out ,
91+ boolean isArray ) throws SerializationException {
92+ if (!fieldMapping .containsField (currentPath )) {
93+ fieldMapping .put (currentPath , delimiters [currentDelimiterIdx ++], fieldMappingType );
94+ } else {
95+ DataType existingType = fieldMapping .getDataType (currentPath );
96+ DataType newType = getNodeType (valueNode );
97+ if (existingType != newType ) {
98+ DataType encapsulatingType = DataType .encapsulatingType (existingType , newType );
99+ if (isArray )
100+ fieldMapping .updateType (currentPath , getArrayNodeType (encapsulatingType ));
101+ else
80102 fieldMapping .updateType (currentPath , encapsulatingType );
81- }
82- }
83- try {
84- byte fieldByte = fieldMapping .getDelimiter (currentPath );
85- out .write (fieldByte );
86- out .write (valueNode .asText ().getBytes ());
87- out .write (fieldByte );
88- } catch (IOException e ) {
89- throw new SerializationException (e .getMessage ());
90103 }
91104 }
105+ try {
106+ byte fieldByte = fieldMapping .getDelimiter (currentPath );
107+ out .write (fieldByte );
108+ out .write (valueNode .asText ().getBytes ());
109+ out .write (fieldByte );
110+ } catch (IOException e ) {
111+ throw new SerializationException (e .getMessage ());
112+ }
92113 }
93114
94115 private DataType getNodeType (JsonNode node ) {
@@ -108,4 +129,12 @@ private DataType getNodeType(JsonNode node) {
108129 throw new UnsupportedOperationException ("JSON DataType not supported." );
109130 }
110131 }
132+
133+ private DataType getArrayNodeType (DataType fieldType ) {
134+ if (fieldType == DataType .STRING ) return DataType .STRINGARRAY ;
135+ else if (fieldType == DataType .LONG ) return DataType .LONGARRAY ;
136+ else if (fieldType == DataType .BYTE ) return DataType .BYTEARRAY ;
137+ else if (fieldType == DataType .BOOLEAN ) return DataType .BOOLARRAY ;
138+ else throw new UnsupportedOperationException ("JSON DataType not supported." );
139+ }
111140}
0 commit comments