Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
guava multimap support. issue #992
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 15, 2017
1 parent e4b6be8 commit 6c01068
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/alibaba/fastjson/serializer/GuavaCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alibaba.fastjson.serializer;

import com.google.common.collect.Multimap;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;

/**
* Created by wenshao on 15/01/2017.
*/
public class GuavaCodec implements ObjectSerializer {
public static GuavaCodec instance = new GuavaCodec();

public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.out;
if (object instanceof Multimap) {
Multimap multimap = (Multimap) object;
serializer.write(multimap.asMap());
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class SerializeConfig {
private static boolean jdk8Error = false;
private static boolean oracleJdbcError = false;
private static boolean springfoxError = false;
private static boolean guavaError = false;

private boolean asm = !ASMUtils.IS_ANDROID;
private ASMSerializerFactory asmFactory;
protected String typeKey = JSON.DEFAULT_TYPE_KEY;
Expand Down Expand Up @@ -531,6 +533,28 @@ private ObjectSerializer getObjectWriter(Class<?> clazz, boolean create) {
}
}

if ((!guavaError) //
&& className.startsWith("com.google.common.collect.")) {
try {
put(Class.forName("com.google.common.collect.HashMultimap"), //
GuavaCodec.instance);
put(Class.forName("com.google.common.collect.LinkedListMultimap"), //
GuavaCodec.instance);
put(Class.forName("com.google.common.collect.ArrayListMultimap"), //
GuavaCodec.instance);
put(Class.forName("com.google.common.collect.TreeMultimap"), //
GuavaCodec.instance);

writer = serializers.get(clazz);
if (writer != null) {
return writer;
}
} catch (ClassNotFoundException e) {
// skip
guavaError = true;
}
}

if (TypeUtils.isProxy(clazz)) {
Class<?> superClazz = clazz.getSuperclass();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.alibaba.json.bvt.guava;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.TreeMultimap;
import com.google.common.primitives.Ints;
import junit.framework.TestCase;

/**
* Created by wenshao on 15/01/2017.
*/
public class ArrayListMultimapTest extends TestCase {
public void test_for_multimap() throws Exception {
ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
multimap.putAll("b", Ints.asList(2, 4, 6));
multimap.putAll("a", Ints.asList(4, 2, 1));
multimap.putAll("c", Ints.asList(2, 5, 3));


String json = JSON.toJSONString(multimap);
assertEquals("{\"a\":[4,2,1],\"b\":[2,4,6],\"c\":[2,5,3]}", json);

TreeMultimap treeMultimap = TreeMultimap.create(multimap);
String json2 = JSON.toJSONString(treeMultimap);
assertEquals("{\"a\":[1,2,4],\"b\":[2,4,6],\"c\":[2,3,5]}", json2);
}
}
21 changes: 21 additions & 0 deletions src/test/java/com/alibaba/json/bvt/guava/HashMultimapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alibaba.json.bvt.guava;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.HashMultimap;
import com.google.gson.Gson;
import junit.framework.TestCase;

/**
* Created by wenshao on 15/01/2017.
*/
public class HashMultimapTest extends TestCase {
public void test_for_multimap() throws Exception {
HashMultimap map = HashMultimap.create();
map.put("name", "a");
map.put("name", "b");

String json = JSON.toJSONString(map);
assertEquals("{\"name\":[\"a\",\"b\"]}", json);
}
}
18 changes: 18 additions & 0 deletions src/test/java/com/alibaba/json/bvt/guava/ImmutableMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alibaba.json.bvt.guava;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import junit.framework.TestCase;

import java.util.Map;

/**
* Created by wenshao on 15/01/2017.
*/
public class ImmutableMapTest extends TestCase {
public void test_immutableMap() throws Exception {
Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 2, "c", 3);
String json = JSON.toJSONString(map);
assertEquals("{\"a\":1,\"b\":2,\"c\":3}", json);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.alibaba.json.bvt.guava;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.LinkedListMultimap;
import junit.framework.TestCase;

/**
* Created by wenshao on 15/01/2017.
*/
public class LinkedListMultimapTest extends TestCase {
public void test_for_multimap() throws Exception {
LinkedListMultimap map = LinkedListMultimap.create();
map.put("name", "a");
map.put("name", "b");

String json = JSON.toJSONString(map);
assertEquals("{\"name\":[\"a\",\"b\"]}", json);
}
}
21 changes: 21 additions & 0 deletions src/test/java/com/alibaba/json/bvt/guava/MultiMapTes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alibaba.json.bvt.guava;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.*;
import junit.framework.TestCase;

import java.util.Map;

/**
* Created by wenshao on 15/01/2017.
*/
public class MultiMapTes extends TestCase {

public void test_multimap() throws Exception {
Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 1, "c", 2);
SetMultimap<String, Integer> multimap = Multimaps.forMap(map);
Multimap<Integer, String> inverse = Multimaps.invertFrom(multimap, HashMultimap.<Integer, String>create());
String json = JSON.toJSONString(inverse);
assertEquals("{1:[\"a\",\"b\"],2:[\"c\"]}",json);
}
}
8 changes: 8 additions & 0 deletions src/test/java/com/alibaba/json/test/FNV32_CollisionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@ static long fnv_hash(char[] chars) {
}
return hash;
}

static long hash(char[] chars) {
long hash = 0;
for (int i = 0; i < chars.length; ++i) {
hash = 31 * hash + chars[i];
}
return hash;
}
}

0 comments on commit 6c01068

Please sign in to comment.