This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/com/alibaba/fastjson/serializer/GuavaCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/test/java/com/alibaba/json/bvt/guava/ArrayListMultimapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/test/java/com/alibaba/json/bvt/guava/HashMultimapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/test/java/com/alibaba/json/bvt/guava/ImmutableMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/alibaba/json/bvt/guava/LinkedListMultimapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters