|
| 1 | +package com.alibaba.json.bvt.parser; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import junit.framework.TestCase; |
| 6 | + |
| 7 | +import org.junit.Assert; |
| 8 | + |
| 9 | +import com.alibaba.fastjson.JSON; |
| 10 | +import com.alibaba.fastjson.TypeReference; |
| 11 | + |
| 12 | +public class GenericTest2 extends TestCase { |
| 13 | + public void test_for_bingyang() throws Exception { |
| 14 | + String text = "{\"count\":123,\"index\":7,\"items\":[{\"id\":234,\"latitude\":2.5,\"longtitude\":3.7}]}"; |
| 15 | + PageBean<ActiveBase> pageBean = JSON.parseObject(text, new TypeReference<PageBean<ActiveBase>>() {}); |
| 16 | + Assert.assertNotNull(pageBean); |
| 17 | + Assert.assertEquals(123, pageBean.getCount()); |
| 18 | + Assert.assertEquals(7, pageBean.getIndex()); |
| 19 | + Assert.assertNotNull(pageBean.getItems()); |
| 20 | + Assert.assertEquals(1, pageBean.getItems().size()); |
| 21 | + ActiveBase active = pageBean.getItems().get(0); |
| 22 | + Assert.assertEquals(new Integer(234), active.getId()); |
| 23 | + Assert.assertTrue(3.7D == active.getLongtitude()); |
| 24 | + Assert.assertTrue(2.5D == active.getLatitude()); |
| 25 | + } |
| 26 | + |
| 27 | + public static class ActiveBase extends BaseModel { |
| 28 | + private double latitude; |
| 29 | + private double longtitude; |
| 30 | + public double getLatitude() { |
| 31 | + return latitude; |
| 32 | + } |
| 33 | + public void setLatitude(double latitude) { |
| 34 | + this.latitude = latitude; |
| 35 | + } |
| 36 | + public double getLongtitude() { |
| 37 | + return longtitude; |
| 38 | + } |
| 39 | + public void setLongtitude(double longtitude) { |
| 40 | + this.longtitude = longtitude; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static class BaseModel { |
| 45 | + private Integer id; |
| 46 | + |
| 47 | + public Integer getId() { |
| 48 | + return id; |
| 49 | + } |
| 50 | + |
| 51 | + public void setId(Integer id) { |
| 52 | + this.id = id; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static class PageBean<T> { |
| 57 | + private int count; |
| 58 | + private int index; |
| 59 | + private List<T> items; |
| 60 | + |
| 61 | + public int getCount() { |
| 62 | + return count; |
| 63 | + } |
| 64 | + |
| 65 | + public void setCount(int count) { |
| 66 | + this.count = count; |
| 67 | + } |
| 68 | + |
| 69 | + public int getIndex() { |
| 70 | + return index; |
| 71 | + } |
| 72 | + |
| 73 | + public void setIndex(int index) { |
| 74 | + this.index = index; |
| 75 | + } |
| 76 | + |
| 77 | + public List<T> getItems() { |
| 78 | + return items; |
| 79 | + } |
| 80 | + |
| 81 | + public void setItems(List<T> items) { |
| 82 | + this.items = items; |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | +} |
0 commit comments