Gson.toJson incorrectly serializes collection of objects with some circumstances (see unit test)
- Dominant language
- Java
- Stars
- 24.2k
- Forks
- 4.5k
- Avg merge
- 6d 4h
- Merged PRs (30d)
- 12
Description
I guess it is something wrong with TypeAdapter cache mechanism for collections.
My Java version:
> java version "1.7.0_80"
> Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
> Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
Gson dependency:
``` xml
com.google.code.gson
gson
2.5
```
Just run this unit test:
``` java
package pkravtsov;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.google.gson.Gson;
public class GsonTest {
class R {
List blist;
}
class B {
int b = 1;
List rlist;
}
class C extends B {
int c = 2;
}
class A {
List a;
}
@Test
public void testToJsonObject() {
Gson gson = new Gson();
List source = new ArrayList();
A a = new A();
a.a = new ArrayList();
a.a.add(new B());
a.a.add(new C());
source.add(a);
String expected = "[{\"a\":[{\"b\":1},{\"c\":2,\"b\":1}]}]";
String result = gson.toJson(source);
assertEquals(expected, result);
gson = new Gson();// This line is essential to reproduce the bug! Do not use previous successful gson instance!
source = new ArrayList();
source.add(new B());
a = new A();
a.a = new ArrayList();
a.a.add(new B());
a.a.add(new C());
source.add(a);
expected = "[{\"b\":1},{\"a\":[{\"b\":1},{\"c\":2,\"b\":1}]}]";
String wrong = "[{\"b\":1},{\"a\":[{\"b\":1},{\"b\":1}]}]";
result = gson.toJson(source);
assertEquals(wrong, result);
assertEquals(expected, result);
}
}
```
This unit test will be successful if you do any of the following:
1. Move line "source.add(new B());" after "source.add(a);" (do not forget to update "expected" value appropriately).
2. Remove R.blist (do not forget to update "expected" value appropriately).
Contributor guide
Assessment
This issue has not been assessed yet.