• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.ser;
2 
3 import java.io.IOException;
4 import java.util.*;
5 
6 import com.fasterxml.jackson.annotation.JsonFilter;
7 import com.fasterxml.jackson.core.JsonGenerator;
8 
9 import com.fasterxml.jackson.databind.*;
10 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
11 
12 public class TestIterable extends BaseMapTest
13 {
14     final static class IterableWrapper
15         implements Iterable<Integer>
16     {
17         List<Integer> _ints = new ArrayList<Integer>();
18 
IterableWrapper(int[] values)19         public IterableWrapper(int[] values) {
20             for (int i : values) {
21                 _ints.add(Integer.valueOf(i));
22             }
23         }
24 
25         @Override
iterator()26         public Iterator<Integer> iterator() {
27             return _ints.iterator();
28         }
29     }
30 
31     @JsonSerialize(typing=JsonSerialize.Typing.STATIC)
32     static class BeanWithIterable {
33         private final ArrayList<String> values = new ArrayList<String>();
34         {
35             values.add("value");
36         }
37 
getValues()38         public Iterable<String> getValues() { return values; }
39     }
40 
41     static class BeanWithIterator {
42         private final ArrayList<String> values = new ArrayList<String>();
43         {
44             values.add("itValue");
45         }
46 
getValues()47         public Iterator<String> getValues() { return values.iterator(); }
48     }
49 
50     static class IntIterable implements Iterable<Integer>
51     {
52         @Override
iterator()53         public Iterator<Integer> iterator() {
54             return new IntIterator(1, 3);
55         }
56     }
57 
58     static class IntIterator implements Iterator<Integer> {
59         int i;
60         final int last;
61 
IntIterator(int first, int last)62         public IntIterator(int first, int last) {
63             i = first;
64             this.last = last;
65         }
66 
67         @Override
hasNext()68         public boolean hasNext() {
69             return i <= last;
70         }
71 
72         @Override
next()73         public Integer next() {
74             return i++;
75         }
76 
77         @Override
remove()78         public void remove() { }
79 
getX()80         public int getX() { return 13; }
81     }
82 
83     // [databind#358]
84     static class A {
85         public String unexpected = "Bye.";
86     }
87 
88     static class B {
89         @JsonSerialize(as = Iterable.class,
90                 contentUsing = ASerializer.class)
91         public List<A> list = Arrays.asList(new A());
92     }
93 
94     static class ASerializer extends JsonSerializer<A> {
95         @Override
serialize(A a, JsonGenerator jsonGenerator, SerializerProvider provider)96         public void serialize(A a, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
97             jsonGenerator.writeStartArray();
98             jsonGenerator.writeString("Hello world.");
99             jsonGenerator.writeEndArray();
100         }
101     }
102 
103     // [databind#2390]
104     @JsonFilter("default")
105     static class IntIterable2390 extends IntIterable { }
106 
107     /*
108     /**********************************************************
109     /* Test methods
110     /**********************************************************
111      */
112 
113     private final ObjectMapper MAPPER = new ObjectMapper();
114 
115     private final ObjectMapper STATIC_MAPPER = jsonMapperBuilder()
116             .enable(MapperFeature.USE_STATIC_TYPING)
117             .build();
118 
testIterator()119     public void testIterator() throws IOException
120     {
121         ArrayList<Integer> l = new ArrayList<Integer>();
122         l.add(1);
123         l.add(null);
124         l.add(-9);
125         l.add(0);
126 
127         assertEquals("[1,null,-9,0]", MAPPER.writeValueAsString(l.iterator()));
128         l.clear();
129         assertEquals("[]", MAPPER.writeValueAsString(l.iterator()));
130     }
131 
testIterable()132     public void testIterable() throws IOException
133     {
134         assertEquals("[1,2,3]",
135                 MAPPER.writeValueAsString(new IterableWrapper(new int[] { 1, 2, 3 })));
136     }
137 
testWithIterable()138     public void testWithIterable() throws IOException
139     {
140         assertEquals("{\"values\":[\"value\"]}",
141                 STATIC_MAPPER.writeValueAsString(new BeanWithIterable()));
142         assertEquals("[1,2,3]",
143                 STATIC_MAPPER.writeValueAsString(new IntIterable()));
144     }
145 
testWithIterator()146     public void testWithIterator() throws IOException
147     {
148         assertEquals("{\"values\":[\"itValue\"]}",
149                 STATIC_MAPPER.writeValueAsString(new BeanWithIterator()));
150 
151         // [databind#1977]
152         ArrayList<Number> numbersList = new ArrayList<>();
153         numbersList.add(1);
154         numbersList.add(0.25);
155         String json = MAPPER.writeValueAsString(numbersList.iterator());
156         assertEquals("[1,0.25]", json);
157     }
158 
159     // [databind#358]
testIterable358()160     public void testIterable358() throws Exception {
161         String json = MAPPER.writeValueAsString(new B());
162         assertEquals("{\"list\":[[\"Hello world.\"]]}", json);
163     }
164 
165     // [databind#2390]
testIterableWithAnnotation()166     public void testIterableWithAnnotation() throws Exception
167     {
168         assertEquals("[1,2,3]",
169                 STATIC_MAPPER.writeValueAsString(new IntIterable2390()));
170     }
171 }
172