Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> implements Iterable<Map.Entry<String, String>> {
@Override
public Iterator<Map.Entry<String, String>> iterator() {
return entrySet().iterator();
}
}

public void testSerialize_simpleMap() throws Exception {
StringWriter writer = new StringWriter();
JsonGenerator generator = newGenerator(writer);

Map m = new HashMap<String, String>();
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down