1 package com.fasterxml.jackson.databind.struct;
2 
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.List;
7 
8 import com.fasterxml.jackson.annotation.JsonCreator;
9 import com.fasterxml.jackson.annotation.JsonProperty;
10 
11 import com.fasterxml.jackson.core.type.TypeReference;
12 
13 import com.fasterxml.jackson.databind.*;
14 
15 public class SingleValueAsArrayTest extends BaseMapTest
16 {
17     private static final String JSON = "[{\"message\":\"messageHere\"}]";
18 
19     static class Bean1421A
20     {
21         List<Messages> bs = Collections.emptyList();
22 
23         @JsonCreator
Bean1421A(final List<Messages> bs)24         Bean1421A(final List<Messages> bs)
25         {
26             this.bs = bs;
27         }
28     }
29 
30     static class Messages
31     {
32         List<MessageWrapper> cs = Collections.emptyList();
33 
34         @JsonCreator
Messages(final List<MessageWrapper> cs)35         Messages(final List<MessageWrapper> cs)
36         {
37             this.cs = cs;
38         }
39     }
40 
41     static class MessageWrapper
42     {
43         String message;
44 
45         @JsonCreator
MessageWrapper(@sonProperty"message") String message)46         MessageWrapper(@JsonProperty("message") String message)
47         {
48             this.message = message;
49         }
50     }
51 
52     static class Bean1421B<T> {
53         T value;
54 
55         @JsonCreator
Bean1421B(T value)56         public Bean1421B(T value) {
57             this.value = value;
58         }
59     }
60 
61     /*
62     /**********************************************************
63     /* Unit tests
64     /**********************************************************
65      */
66 
67     private final ObjectMapper MAPPER = new ObjectMapper();
68     {
69         MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
70     }
71 
testSuccessfulDeserializationOfObjectWithChainedArrayCreators()72     public void testSuccessfulDeserializationOfObjectWithChainedArrayCreators() throws IOException
73     {
74         Bean1421A result = MAPPER.readValue(JSON, Bean1421A.class);
75         assertNotNull(result);
76     }
77 
testWithSingleString()78     public void testWithSingleString() throws Exception {
79         Bean1421B<List<String>> a = MAPPER.readValue(quote("test2"),
80                 new TypeReference<Bean1421B<List<String>>>() {});
81         List<String> expected = new ArrayList<>();
82         expected.add("test2");
83         assertEquals(expected, a.value);
84     }
85 
testPrimitives()86     public void testPrimitives() throws Exception {
87         int[] i = MAPPER.readValue("16", int[].class);
88         assertEquals(1, i.length);
89         assertEquals(16, i[0]);
90 
91         long[] l = MAPPER.readValue("1234", long[].class);
92         assertEquals(1, l.length);
93         assertEquals(1234L, l[0]);
94 
95         double[] d = MAPPER.readValue("12.5", double[].class);
96         assertEquals(1, d.length);
97         assertEquals(12.5, d[0]);
98 
99         boolean[] b = MAPPER.readValue("true", boolean[].class);
100         assertEquals(1, d.length);
101         assertEquals(true, b[0]);
102     }
103 }
104