Created
January 6, 2016 03:44
-
-
Save greenlaw110/bd773f1a283ec079102f to your computer and use it in GitHub Desktop.
FastJson doesn't support serialize Iterable stuff
This file contains hidden or 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
public class FastJsonSerializeIterableTest { | |
@Test | |
public void testWithIterable() { | |
class Person { | |
private String name; | |
public Person(String s) { | |
this.name = s; | |
} | |
public String getName() { | |
return name; | |
} | |
} | |
final Person s1 = new Person("fast"); | |
final Person s2 = new Person("fast"); | |
Iterable<Person> iterable = new Iterable<Person>() { | |
@Override | |
public Iterator<Person> iterator() { | |
return new Iterator<Person>() { | |
int cursor = 0; | |
@Override | |
public boolean hasNext() { | |
return cursor < 2; | |
} | |
@Override | |
public Person next() { | |
switch (cursor++) { | |
case 0: | |
return s1; | |
case 1: | |
return s2; | |
default: | |
throw new NoSuchElementException(); | |
} | |
} | |
@Override | |
public void remove() { | |
throw new UnsupportedOperationException(); | |
} | |
}; | |
} | |
}; | |
List<Person> list = new ArrayList<>(); | |
for (Person p : iterable) { | |
list.add(p); | |
} | |
assertEquals("[{\"name\":\"fast\"},{\"name\":\"fast\"}]", JSON.toJSONString(list)); | |
assertEquals("[{\"name\":\"fast\"},{\"name\":\"fast\"}]", JSON.toJSONString(iterable)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
嗯,这应该支持的,等下个版本