@@ -145,7 +145,7 @@ public static String getEntry(Object input) {
145145 /**
146146 * Serialize object to a string
147147 *
148- * @param input can be any object or collection of objects.
148+ * @param input can be any object or collection/map of objects.
149149 * @param useObjectMapper if true, then use Jackson's ObjectMapper to convert from object to string,
150150 * otherwise use toString
151151 * @return serialized object
@@ -154,19 +154,16 @@ public static String getEntry(Object input) {
154154 public static String getEntry (Object input , boolean useObjectMapper ) {
155155 if (input == null ) {
156156 return null ;
157- }
158- if (input instanceof Collection <?>) {
159- StringJoiner joiner = new StringJoiner (", " , "[ " , " ]" );
160- for (Object collectionEntry : (Collection <?>) input ) {
161- joiner .add (getEntry (collectionEntry , useObjectMapper ));
162- }
163- return joiner .toString ();
164- }
165- if (useObjectMapper ) {
157+ } else if (useObjectMapper ) {
166158 return objectMapperWriteValueAsString (input );
167- }
168- if (input instanceof Enum <?>) {
169- return input .toString ();
159+ } else if (input instanceof Collection <?>) {
160+ return serializeCollection ((Collection <?>) input , useObjectMapper );
161+ } else if (input instanceof Map <?, ?>) {
162+ return serializeMap ((Map <?, ?>) input , useObjectMapper );
163+ } else if (input instanceof Map .Entry <?, ?>) {
164+ return serializeMapEntry ((Map .Entry <?, ?>) input , useObjectMapper );
165+ } else if (input instanceof Enum <?>) {
166+ return serializeEnum ((Enum <?>) input );
170167 } else if (input instanceof String ) {
171168 return escapeJsonString (input .toString ());
172169 } else if (input .getClass ().getName ().equals ("scala.Some" )) { // TODO: move to Scala Serializer
@@ -180,13 +177,29 @@ public static String getEntry(Object input, boolean useObjectMapper) {
180177 }
181178 }
182179
180+ public static String serializeCollection (Collection <?> input , boolean useObjectMapper ) {
181+ StringJoiner joiner = new StringJoiner (", " , "[ " , " ]" );
182+ for (Object entry : input ) {
183+ joiner .add (getEntry (entry , useObjectMapper ));
184+ }
185+ return joiner .toString ();
186+ }
183187
184- public static String objectMapperWriteValueAsString (Object input ) {
185- try {
186- return OBJECT_MAPPER .writeValueAsString (input );
187- } catch (JsonProcessingException e ) {
188- throw new UnableToBuildJsonQueryException (e );
188+ public static String serializeMap (Map <?, ?> input , boolean useObjectMapper ) {
189+ StringJoiner joiner = new StringJoiner (", " , "{ " , " }" );
190+ for (Map .Entry <?, ?> entry : input .entrySet ()) {
191+ joiner .add (getEntry (entry , useObjectMapper ));
189192 }
193+ return joiner .toString ();
194+ }
195+
196+ public static String serializeMapEntry (Map .Entry <?, ?> input , boolean useObjectMapper ) {
197+ // no need to quote String key
198+ return input .getKey () + ": " + getEntry (input .getValue (), useObjectMapper );
199+ }
200+
201+ public static String serializeEnum (Enum <?> input ) {
202+ return input .toString ();
190203 }
191204
192205 /**
@@ -231,4 +244,12 @@ public static String escapeJsonString(String stringValue) {
231244 return sb .toString ();
232245 }
233246
247+ public static String objectMapperWriteValueAsString (Object input ) {
248+ try {
249+ return OBJECT_MAPPER .writeValueAsString (input );
250+ } catch (JsonProcessingException e ) {
251+ throw new UnableToBuildJsonQueryException (e );
252+ }
253+ }
254+
234255}
0 commit comments