@@ -31,13 +31,16 @@ public CustomValueBean(int v) {
31
31
}
32
32
33
33
static class CustomValueReader extends ValueReader {
34
- public CustomValueReader () {
34
+ private final int delta ;
35
+
36
+ public CustomValueReader (int d ) {
35
37
super (CustomValue .class );
38
+ delta = d ;
36
39
}
37
40
38
41
@ Override
39
42
public Object read (JSONReader reader , JsonParser p ) throws IOException {
40
- return new CustomValue (p .getIntValue (), true );
43
+ return new CustomValue (p .getIntValue () + delta , true );
41
44
}
42
45
43
46
// Base class impl should be fine, although we'd use this for optimal
@@ -49,23 +52,56 @@ public Object readNext(JSONReader reader, JsonParser p) throws IOException {
49
52
*/
50
53
}
51
54
55
+ static class ABCValueReader extends ValueReader {
56
+ public ABCValueReader () {
57
+ super (ABC .class );
58
+ }
59
+
60
+ @ Override
61
+ public Object read (JSONReader reader , JsonParser p ) throws IOException {
62
+ final String str = p .getText ();
63
+ if ("n/a" .equals (str )) {
64
+ return ABC .DEF ;
65
+ }
66
+ return ABC .valueOf (str );
67
+ }
68
+ }
69
+
52
70
static class CustomReaders extends ReaderWriterProvider {
71
+ final int delta ;
72
+
73
+ public CustomReaders (int d ) {
74
+ delta = d ;
75
+ }
76
+
53
77
@ Override
54
78
public ValueReader findBeanReader (JSONReader readContext , Class <?> type ) {
55
79
if (type .equals (CustomValue .class )) {
56
- return new CustomValueReader ();
80
+ return new CustomValueReader (delta );
81
+ }
82
+ return null ;
83
+ }
84
+
85
+ @ Override
86
+ public ValueReader findEnumReader (JSONReader readContext , Class <?> type ) {
87
+ if (type .equals (ABC .class )) {
88
+ return new ABCValueReader ();
57
89
}
58
90
return null ;
59
91
}
60
92
}
61
93
94
+ enum ABC {
95
+ A , B , C , DEF ;
96
+ }
97
+
62
98
/*
63
99
/**********************************************************************
64
100
/* Test methdods
65
101
/**********************************************************************
66
102
*/
67
103
68
- public void testSimpleCustomReader () throws Exception
104
+ public void testCustomBeanReader () throws Exception
69
105
{
70
106
// First: without handler, will fail to map
71
107
try {
@@ -78,13 +114,44 @@ public void testSimpleCustomReader() throws Exception
78
114
79
115
// then with custom, should be fine
80
116
JSON json = JSON .std
81
- .with (new CustomReaders ());
117
+ .with (new CustomReaders (0 ));
82
118
CustomValue v = json .beanFrom (CustomValue .class , "123" );
83
119
assertEquals (124 , v .value );
84
120
85
121
// similarly with wrapper
86
122
CustomValueBean bean = json .beanFrom (CustomValueBean .class ,
87
123
aposToQuotes ("{ 'custom' : 137 }" ));
88
124
assertEquals (138 , bean .custom .value );
125
+
126
+ // but also ensure we can change registered handler(s)
127
+ JSON json2 = json .with (new CustomReaders (100 ));
128
+ v = json2 .beanFrom (CustomValue .class , "123" );
129
+ assertEquals (224 , v .value );
130
+ }
131
+
132
+ public void testCustomEnumReader () throws Exception
133
+ {
134
+ // First: without handler, will fail to map
135
+ try {
136
+ JSON .std .beanFrom (ABC .class , quote ("n/a" ));
137
+ fail ("Should not pass" );
138
+ } catch (JSONObjectException e ) {
139
+ verifyException (e , "Failed to find Enum of type" );
140
+ }
141
+
142
+ // then with custom, should be fine
143
+ JSON json = JSON .std
144
+ .with (new CustomReaders (0 ));
145
+ ABC v = json .beanFrom (ABC .class , quote ("n/a" ));
146
+ assertEquals (ABC .DEF , v );
147
+
148
+ // but if we remove, again error
149
+ JSON json2 = json .with ((ReaderWriterProvider ) null );
150
+ try {
151
+ json2 .beanFrom (ABC .class , quote ("n/a" ));
152
+ fail ("Should not pass" );
153
+ } catch (JSONObjectException e ) {
154
+ verifyException (e , "Failed to find Enum of type" );
155
+ }
89
156
}
90
157
}
0 commit comments