1 package com.fasterxml.jackson.databind.convert;
2 
3 import java.util.*;
4 
5 import static org.junit.Assert.*;
6 
7 import com.fasterxml.jackson.core.Base64Variants;
8 import com.fasterxml.jackson.databind.*;
9 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
10 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
11 import com.fasterxml.jackson.databind.util.StdConverter;
12 
13 public class TestStringConversions
14     extends com.fasterxml.jackson.databind.BaseMapTest
15 {
16     static class LCConverter extends StdConverter<String,String>
17     {
convert(String value)18         @Override public String convert(String value) {
19             return value.toLowerCase();
20         }
21     }
22 
23     static class StringWrapperWithConvert
24     {
25         @JsonSerialize(converter=LCConverter.class)
26         @JsonDeserialize(converter=LCConverter.class)
27         public String value;
28 
StringWrapperWithConvert()29         protected StringWrapperWithConvert() { }
StringWrapperWithConvert(String v)30         public StringWrapperWithConvert(String v) { value = v; }
31     }
32 
33     private final ObjectMapper MAPPER = new ObjectMapper();
34 
testSimple()35     public void testSimple()
36     {
37         assertEquals(Boolean.TRUE, MAPPER.convertValue("true", Boolean.class));
38         assertEquals(Integer.valueOf(-3), MAPPER.convertValue("-3", Integer.class));
39         assertEquals(Long.valueOf(77), MAPPER.convertValue("77", Long.class));
40 
41         int[] ints = { 1, 2, 3 };
42         List<Integer> Ints = new ArrayList<Integer>();
43         Ints.add(1);
44         Ints.add(2);
45         Ints.add(3);
46 
47         assertArrayEquals(ints, MAPPER.convertValue(Ints, int[].class));
48     }
49 
testStringsToInts()50     public void testStringsToInts()
51     {
52         // let's verify our "neat trick" actually works...
53         assertArrayEquals(new int[] { 1, 2, 3, 4, -1, 0 },
54                           MAPPER.convertValue("1  2 3    4  -1 0".split("\\s+"), int[].class));
55     }
56 
testBytesToBase64AndBack()57     public void testBytesToBase64AndBack() throws Exception
58     {
59         byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
60         String encoded = MAPPER.convertValue(input, String.class);
61         assertNotNull(encoded);
62 
63         assertEquals("AQIDBAUGBw==", encoded);
64 
65         // plus, ensure this is consistent:
66         assertEquals(Base64Variants.MIME.encode(input), encoded);
67 
68         byte[] result = MAPPER.convertValue(encoded, byte[].class);
69         assertArrayEquals(input, result);
70     }
71 
testBytestoCharArray()72     public void testBytestoCharArray() throws Exception
73     {
74         byte[] input = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
75         // first, do baseline encoding
76         char[] expEncoded = MAPPER.convertValue(input, String.class).toCharArray();
77         // then compare
78         char[] actEncoded = MAPPER.convertValue(input, char[].class);
79         assertArrayEquals(expEncoded, actEncoded);
80     }
81 
testLowerCasingSerializer()82     public void testLowerCasingSerializer() throws Exception
83     {
84         assertEquals("{\"value\":\"abc\"}", MAPPER.writeValueAsString(new StringWrapperWithConvert("ABC")));
85     }
86 
testLowerCasingDeserializer()87     public void testLowerCasingDeserializer() throws Exception
88     {
89         StringWrapperWithConvert value = MAPPER.readValue("{\"value\":\"XyZ\"}", StringWrapperWithConvert.class);
90         assertNotNull(value);
91         assertEquals("xyz", value.value);
92     }
93 }
94