1
1
package com .fasterxml .jackson .databind .objectid ;
2
2
3
- import com .fasterxml .jackson .annotation .JsonIdentityInfo ;
4
- import com .fasterxml .jackson .annotation .JsonProperty ;
5
- import com .fasterxml .jackson .annotation .JsonTypeInfo ;
3
+ import com .fasterxml .jackson .annotation .*;
6
4
import com .fasterxml .jackson .annotation .JsonTypeInfo .As ;
7
5
import com .fasterxml .jackson .annotation .JsonTypeInfo .Id ;
8
- import com . fasterxml . jackson . annotation . ObjectIdGenerator ;
6
+
9
7
import com .fasterxml .jackson .core .JsonParser ;
10
- import com .fasterxml .jackson .databind .BaseMapTest ;
11
- import com .fasterxml .jackson .databind .DeserializationContext ;
12
- import com .fasterxml .jackson .databind .JsonDeserializer ;
13
- import com .fasterxml .jackson .databind .JsonMappingException ;
14
- import com .fasterxml .jackson .databind .JsonNode ;
15
- import com .fasterxml .jackson .databind .ObjectMapper ;
8
+
9
+ import com .fasterxml .jackson .databind .*;
16
10
import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
17
11
18
12
import java .io .IOException ;
@@ -27,88 +21,81 @@ public class JSOGDeserialize622Test extends BaseMapTest
27
21
public static final String REF_KEY = "@ref" ;
28
22
29
23
/**
30
- * JSON input
31
- */
32
- private static final String EXP_EXAMPLE_JSOG = aposToQuotes (
33
- "{'@id':'1','foo':66,'next':{'" +REF_KEY +"':'1'}}" );
34
-
35
- /**
36
- * Customer IdGenerator
24
+ * Custom IdGenerator
37
25
*/
38
26
static class JSOGGenerator extends ObjectIdGenerator <JSOGRef > {
27
+ private static final long serialVersionUID = 1L ;
28
+ protected transient int _nextValue ;
29
+ protected final Class <?> _scope ;
39
30
40
- private static final long serialVersionUID = 1L ;
41
- protected transient int _nextValue ;
42
- protected final Class <?> _scope ;
31
+ protected JSOGGenerator () { this (null , -1 ); }
43
32
44
- protected JSOGGenerator () { this (null , -1 ); }
45
-
46
- protected JSOGGenerator (Class <?> scope , int nextValue ) {
47
- _scope = scope ;
48
- _nextValue = nextValue ;
49
- }
33
+ protected JSOGGenerator (Class <?> scope , int nextValue ) {
34
+ _scope = scope ;
35
+ _nextValue = nextValue ;
36
+ }
50
37
51
- @ Override
52
- public Class <?> getScope () {
53
- return _scope ;
54
- }
38
+ @ Override
39
+ public Class <?> getScope () {
40
+ return _scope ;
41
+ }
55
42
56
- @ Override
57
- public boolean canUseFor (ObjectIdGenerator <?> gen ) {
58
- return (gen .getClass () == getClass ()) && (gen .getScope () == _scope );
59
- }
43
+ @ Override
44
+ public boolean canUseFor (ObjectIdGenerator <?> gen ) {
45
+ return (gen .getClass () == getClass ()) && (gen .getScope () == _scope );
46
+ }
60
47
61
- @ Override
62
- public ObjectIdGenerator <JSOGRef > forScope (Class <?> scope ) {
63
- return (_scope == scope ) ? this : new JSOGGenerator (scope , _nextValue );
64
- }
48
+ @ Override
49
+ public ObjectIdGenerator <JSOGRef > forScope (Class <?> scope ) {
50
+ return (_scope == scope ) ? this : new JSOGGenerator (scope , _nextValue );
51
+ }
65
52
66
- @ Override
67
- public ObjectIdGenerator <JSOGRef > newForSerialization (Object context ) {
68
- return new JSOGGenerator (_scope , 1 );
69
- }
53
+ @ Override
54
+ public ObjectIdGenerator <JSOGRef > newForSerialization (Object context ) {
55
+ return new JSOGGenerator (_scope , 1 );
56
+ }
70
57
71
- @ Override
72
- public com .fasterxml .jackson .annotation .ObjectIdGenerator .IdKey key (Object key ) {
73
- return new IdKey (getClass (), _scope , key );
74
- }
58
+ @ Override
59
+ public com .fasterxml .jackson .annotation .ObjectIdGenerator .IdKey key (Object key ) {
60
+ return new IdKey (getClass (), _scope , key );
61
+ }
75
62
76
- // important: otherwise won't get proper handling
77
- @ Override
78
- public boolean maySerializeAsObject () { return true ; }
63
+ // important: otherwise won't get proper handling
64
+ @ Override
65
+ public boolean maySerializeAsObject () { return true ; }
79
66
80
- // ditto: needed for handling Object-valued Object references
81
- @ Override
82
- public boolean isValidReferencePropertyName (String name , Object parser ) {
83
- return REF_KEY .equals (name );
84
- }
67
+ // ditto: needed for handling Object-valued Object references
68
+ @ Override
69
+ public boolean isValidReferencePropertyName (String name , Object parser ) {
70
+ return REF_KEY .equals (name );
71
+ }
85
72
86
- @ Override
87
- public JSOGRef generateId (Object forPojo ) {
88
- int id = _nextValue ;
89
- ++_nextValue ;
90
- return new JSOGRef (id );
91
- }
73
+ @ Override
74
+ public JSOGRef generateId (Object forPojo ) {
75
+ int id = _nextValue ;
76
+ ++_nextValue ;
77
+ return new JSOGRef (id );
78
+ }
92
79
}
93
80
94
81
/**
95
82
* The reference deserializer
96
83
*/
97
84
static class JSOGRefDeserializer extends JsonDeserializer <JSOGRef >
98
85
{
99
- @ Override
100
- public JSOGRef deserialize (JsonParser p , DeserializationContext ctx ) throws IOException {
101
- JsonNode node = p .readValueAsTree ();
102
- if (node .isTextual ()) {
103
- return new JSOGRef (node .asInt ());
104
- }
105
- JsonNode n = node .get (REF_KEY );
106
- if (n == null ) {
107
- throw new JsonMappingException (p , "Could not find key '" +REF_KEY
108
- +"' from (" +node .getClass ().getName ()+"): " +node );
109
- }
110
- return new JSOGRef (n .asInt ());
111
- }
86
+ @ Override
87
+ public JSOGRef deserialize (JsonParser p , DeserializationContext ctx ) throws IOException {
88
+ JsonNode node = p .readValueAsTree ();
89
+ if (node .isTextual ()) {
90
+ return new JSOGRef (node .asInt ());
91
+ }
92
+ JsonNode n = node .get (REF_KEY );
93
+ if (n == null ) {
94
+ throw new JsonMappingException (p , "Could not find key '" +REF_KEY
95
+ +"' from (" +node .getClass ().getName ()+"): " +node );
96
+ }
97
+ return new JSOGRef (n .asInt ());
98
+ }
112
99
}
113
100
114
101
/**
@@ -204,6 +191,9 @@ public static class Outer {
204
191
// Basic for [databind#622]
205
192
public void testStructJSOGRef () throws Exception
206
193
{
194
+ final String EXP_EXAMPLE_JSOG = aposToQuotes (
195
+ "{'@id':'1','foo':66,'next':{'" +REF_KEY +"':'1'}}" );
196
+
207
197
IdentifiableExampleJSOG result = MAPPER .readValue (EXP_EXAMPLE_JSOG ,
208
198
IdentifiableExampleJSOG .class );
209
199
assertEquals (66 , result .foo );
0 commit comments