diff --git a/google-http-client-gson/src/test/java/com/google/api/client/json/gson/GsonGeneratorTest.java b/google-http-client-gson/src/test/java/com/google/api/client/json/gson/GsonGeneratorTest.java new file mode 100644 index 000000000..caa543836 --- /dev/null +++ b/google-http-client-gson/src/test/java/com/google/api/client/json/gson/GsonGeneratorTest.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.api.client.json.gson; + +import com.google.api.client.json.JsonGenerator; +import com.google.api.client.test.json.AbstractJsonGeneratorTest; +import java.io.IOException; +import java.io.Writer; + +public class GsonGeneratorTest extends AbstractJsonGeneratorTest { + + private static final GsonFactory FACTORY = new GsonFactory(); + + @Override + protected JsonGenerator newGenerator(Writer writer) throws IOException { + return FACTORY.createJsonGenerator(writer); + } +} diff --git a/google-http-client-jackson/src/test/java/com/google/api/client/json/jackson/JacksonGeneratorTest.java b/google-http-client-jackson/src/test/java/com/google/api/client/json/jackson/JacksonGeneratorTest.java new file mode 100644 index 000000000..bfdfbfe28 --- /dev/null +++ b/google-http-client-jackson/src/test/java/com/google/api/client/json/jackson/JacksonGeneratorTest.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.api.client.json.jackson; + +import com.google.api.client.json.JsonGenerator; +import com.google.api.client.test.json.AbstractJsonGeneratorTest; +import java.io.IOException; +import java.io.Writer; + +public class JacksonGeneratorTest extends AbstractJsonGeneratorTest { + + private static final JacksonFactory FACTORY = new JacksonFactory(); + + @Override + protected JsonGenerator newGenerator(Writer writer) throws IOException { + return FACTORY.createJsonGenerator(writer); + } +} diff --git a/google-http-client-jackson2/src/test/java/com/google/api/client/json/jackson2/JacksonGeneratorTest.java b/google-http-client-jackson2/src/test/java/com/google/api/client/json/jackson2/JacksonGeneratorTest.java new file mode 100644 index 000000000..4b0af8478 --- /dev/null +++ b/google-http-client-jackson2/src/test/java/com/google/api/client/json/jackson2/JacksonGeneratorTest.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.api.client.json.jackson2; + +import com.google.api.client.json.JsonGenerator; +import com.google.api.client.test.json.AbstractJsonGeneratorTest; +import java.io.IOException; +import java.io.Writer; + +public class JacksonGeneratorTest extends AbstractJsonGeneratorTest { + + private static final JacksonFactory FACTORY = new JacksonFactory(); + + @Override + protected JsonGenerator newGenerator(Writer writer) throws IOException { + return FACTORY.createJsonGenerator(writer); + } +} diff --git a/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonGeneratorTest.java b/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonGeneratorTest.java new file mode 100644 index 000000000..0f92bc10f --- /dev/null +++ b/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonGeneratorTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.api.client.test.json; + +import com.google.api.client.json.JsonGenerator; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import junit.framework.TestCase; + +public abstract class AbstractJsonGeneratorTest extends TestCase { + + protected abstract JsonGenerator newGenerator(Writer writer) throws IOException; + + class IterableMap extends HashMap implements Iterable> { + @Override + public Iterator> iterator() { + return entrySet().iterator(); + } + } + + public void testSerialize_simpleMap() throws Exception { + StringWriter writer = new StringWriter(); + JsonGenerator generator = newGenerator(writer); + + Map m = new HashMap(); + m.put("a", "b"); + + generator.serialize(m); + generator.close(); + assertEquals("{\"a\":\"b\"}", writer.toString()); + } + + public void testSerialize_iterableMap() throws Exception { + StringWriter writer = new StringWriter(); + JsonGenerator generator = newGenerator(writer); + + Map m = new IterableMap(); + m.put("a", "b"); + + generator.serialize(m); + generator.close(); + assertEquals("{\"a\":\"b\"}", writer.toString()); + } +} diff --git a/google-http-client/src/main/java/com/google/api/client/json/JsonGenerator.java b/google-http-client/src/main/java/com/google/api/client/json/JsonGenerator.java index b37ac6ced..717a1a58d 100644 --- a/google-http-client/src/main/java/com/google/api/client/json/JsonGenerator.java +++ b/google-http-client/src/main/java/com/google/api/client/json/JsonGenerator.java @@ -141,7 +141,8 @@ private void serialize(boolean isJsonString, Object value) throws IOException { writeBoolean((Boolean) value); } else if (value instanceof DateTime) { writeString(((DateTime) value).toStringRfc3339()); - } else if (value instanceof Iterable || valueClass.isArray()) { + } else if ((value instanceof Iterable || valueClass.isArray()) && + !(value instanceof Map) && !(value instanceof GenericData)) { writeStartArray(); for (Object o : Types.iterableOf(value)) { serialize(isJsonString, o);