Skip to content

Commit 052f178

Browse files
authored
Fix iterable maps JSON serialization (#550)
* Add common generator tests for the iterable map and normal map * Make json serialization treat maps as maps, always. Even if they are iterable, like clojure maps. * Fix compilation on Java 7 * only add one map entry to prevent ordering test failure
1 parent 44777a9 commit 052f178

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.json.gson;
16+
17+
import com.google.api.client.json.JsonGenerator;
18+
import com.google.api.client.test.json.AbstractJsonGeneratorTest;
19+
import java.io.IOException;
20+
import java.io.Writer;
21+
22+
public class GsonGeneratorTest extends AbstractJsonGeneratorTest {
23+
24+
private static final GsonFactory FACTORY = new GsonFactory();
25+
26+
@Override
27+
protected JsonGenerator newGenerator(Writer writer) throws IOException {
28+
return FACTORY.createJsonGenerator(writer);
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.json.jackson;
16+
17+
import com.google.api.client.json.JsonGenerator;
18+
import com.google.api.client.test.json.AbstractJsonGeneratorTest;
19+
import java.io.IOException;
20+
import java.io.Writer;
21+
22+
public class JacksonGeneratorTest extends AbstractJsonGeneratorTest {
23+
24+
private static final JacksonFactory FACTORY = new JacksonFactory();
25+
26+
@Override
27+
protected JsonGenerator newGenerator(Writer writer) throws IOException {
28+
return FACTORY.createJsonGenerator(writer);
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.json.jackson2;
16+
17+
import com.google.api.client.json.JsonGenerator;
18+
import com.google.api.client.test.json.AbstractJsonGeneratorTest;
19+
import java.io.IOException;
20+
import java.io.Writer;
21+
22+
public class JacksonGeneratorTest extends AbstractJsonGeneratorTest {
23+
24+
private static final JacksonFactory FACTORY = new JacksonFactory();
25+
26+
@Override
27+
protected JsonGenerator newGenerator(Writer writer) throws IOException {
28+
return FACTORY.createJsonGenerator(writer);
29+
}
30+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.test.json;
16+
17+
import com.google.api.client.json.JsonGenerator;
18+
import java.io.IOException;
19+
import java.io.StringWriter;
20+
import java.io.Writer;
21+
import java.util.HashMap;
22+
import java.util.Iterator;
23+
import java.util.Map;
24+
import junit.framework.TestCase;
25+
26+
public abstract class AbstractJsonGeneratorTest extends TestCase {
27+
28+
protected abstract JsonGenerator newGenerator(Writer writer) throws IOException;
29+
30+
class IterableMap extends HashMap<String, String> implements Iterable<Map.Entry<String, String>> {
31+
@Override
32+
public Iterator<Map.Entry<String, String>> iterator() {
33+
return entrySet().iterator();
34+
}
35+
}
36+
37+
public void testSerialize_simpleMap() throws Exception {
38+
StringWriter writer = new StringWriter();
39+
JsonGenerator generator = newGenerator(writer);
40+
41+
Map m = new HashMap<String, String>();
42+
m.put("a", "b");
43+
44+
generator.serialize(m);
45+
generator.close();
46+
assertEquals("{\"a\":\"b\"}", writer.toString());
47+
}
48+
49+
public void testSerialize_iterableMap() throws Exception {
50+
StringWriter writer = new StringWriter();
51+
JsonGenerator generator = newGenerator(writer);
52+
53+
Map m = new IterableMap();
54+
m.put("a", "b");
55+
56+
generator.serialize(m);
57+
generator.close();
58+
assertEquals("{\"a\":\"b\"}", writer.toString());
59+
}
60+
}

google-http-client/src/main/java/com/google/api/client/json/JsonGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ private void serialize(boolean isJsonString, Object value) throws IOException {
141141
writeBoolean((Boolean) value);
142142
} else if (value instanceof DateTime) {
143143
writeString(((DateTime) value).toStringRfc3339());
144-
} else if (value instanceof Iterable<?> || valueClass.isArray()) {
144+
} else if ((value instanceof Iterable<?> || valueClass.isArray()) &&
145+
!(value instanceof Map<?, ?>) && !(value instanceof GenericData)) {
145146
writeStartArray();
146147
for (Object o : Types.iterableOf(value)) {
147148
serialize(isJsonString, o);

0 commit comments

Comments
 (0)