30
30
31
31
final class JsonValueObjectEncoderContext implements ObjectEncoderContext , ValueEncoderContext {
32
32
33
+ private JsonValueObjectEncoderContext childContext = null ;
34
+ private boolean active = true ;
33
35
private final JsonWriter jsonWriter ;
34
36
private final Map <Class <?>, ObjectEncoder <?>> objectEncoders ;
35
37
private final Map <Class <?>, ValueEncoder <?>> valueEncoders ;
@@ -43,10 +45,17 @@ final class JsonValueObjectEncoderContext implements ObjectEncoderContext, Value
43
45
this .valueEncoders = valueEncoders ;
44
46
}
45
47
48
+ private JsonValueObjectEncoderContext (JsonValueObjectEncoderContext anotherContext ) {
49
+ this .jsonWriter = anotherContext .jsonWriter ;
50
+ this .objectEncoders = anotherContext .objectEncoders ;
51
+ this .valueEncoders = anotherContext .valueEncoders ;
52
+ }
53
+
46
54
@ NonNull
47
55
@ Override
48
56
public JsonValueObjectEncoderContext add (@ NonNull String name , @ Nullable Object o )
49
57
throws IOException , EncodingException {
58
+ maybeUnNest ();
50
59
jsonWriter .name (name );
51
60
if (o == null ) {
52
61
jsonWriter .nullValue ();
@@ -59,6 +68,7 @@ public JsonValueObjectEncoderContext add(@NonNull String name, @Nullable Object
59
68
@ Override
60
69
public JsonValueObjectEncoderContext add (@ NonNull String name , double value )
61
70
throws IOException , EncodingException {
71
+ maybeUnNest ();
62
72
jsonWriter .name (name );
63
73
return add (value );
64
74
}
@@ -67,6 +77,7 @@ public JsonValueObjectEncoderContext add(@NonNull String name, double value)
67
77
@ Override
68
78
public JsonValueObjectEncoderContext add (@ NonNull String name , int value )
69
79
throws IOException , EncodingException {
80
+ maybeUnNest ();
70
81
jsonWriter .name (name );
71
82
return add (value );
72
83
}
@@ -75,6 +86,7 @@ public JsonValueObjectEncoderContext add(@NonNull String name, int value)
75
86
@ Override
76
87
public JsonValueObjectEncoderContext add (@ NonNull String name , long value )
77
88
throws IOException , EncodingException {
89
+ maybeUnNest ();
78
90
jsonWriter .name (name );
79
91
return add (value );
80
92
}
@@ -83,42 +95,58 @@ public JsonValueObjectEncoderContext add(@NonNull String name, long value)
83
95
@ Override
84
96
public JsonValueObjectEncoderContext add (@ NonNull String name , boolean value )
85
97
throws IOException , EncodingException {
98
+ maybeUnNest ();
86
99
jsonWriter .name (name );
87
100
return add (value );
88
101
}
89
102
103
+ @ NonNull
104
+ @ Override
105
+ public ObjectEncoderContext nested (@ NonNull String name ) throws IOException {
106
+ maybeUnNest ();
107
+ childContext = new JsonValueObjectEncoderContext (this );
108
+ jsonWriter .name (name );
109
+ jsonWriter .beginObject ();
110
+ return childContext ;
111
+ }
112
+
90
113
@ NonNull
91
114
@ Override
92
115
public JsonValueObjectEncoderContext add (@ Nullable String value )
93
116
throws IOException , EncodingException {
117
+ maybeUnNest ();
94
118
jsonWriter .value (value );
95
119
return this ;
96
120
}
97
121
98
122
@ NonNull
99
123
@ Override
100
124
public JsonValueObjectEncoderContext add (double value ) throws IOException , EncodingException {
125
+ maybeUnNest ();
101
126
jsonWriter .value (value );
102
127
return this ;
103
128
}
104
129
105
130
@ NonNull
106
131
@ Override
107
132
public JsonValueObjectEncoderContext add (int value ) throws IOException , EncodingException {
133
+ maybeUnNest ();
108
134
jsonWriter .value (value );
109
135
return this ;
110
136
}
111
137
112
138
@ NonNull
113
139
@ Override
114
140
public JsonValueObjectEncoderContext add (long value ) throws IOException , EncodingException {
141
+ maybeUnNest ();
115
142
jsonWriter .value (value );
116
143
return this ;
117
144
}
118
145
119
146
@ NonNull
120
147
@ Override
121
148
public JsonValueObjectEncoderContext add (boolean value ) throws IOException , EncodingException {
149
+ maybeUnNest ();
122
150
jsonWriter .value (value );
123
151
return this ;
124
152
}
@@ -127,6 +155,7 @@ public JsonValueObjectEncoderContext add(boolean value) throws IOException, Enco
127
155
@ Override
128
156
public JsonValueObjectEncoderContext add (@ Nullable byte [] bytes )
129
157
throws IOException , EncodingException {
158
+ maybeUnNest ();
130
159
if (bytes == null ) {
131
160
jsonWriter .nullValue ();
132
161
} else {
@@ -237,6 +266,20 @@ JsonValueObjectEncoderContext add(@Nullable Object o) throws IOException, Encodi
237
266
}
238
267
239
268
void close () throws IOException {
269
+ maybeUnNest ();
240
270
jsonWriter .flush ();
241
271
}
272
+
273
+ private void maybeUnNest () throws IOException {
274
+ if (!active ) {
275
+ throw new IllegalStateException (
276
+ "Parent context used since this context was created. Cannot use this context anymore." );
277
+ }
278
+ if (childContext != null ) {
279
+ childContext .maybeUnNest ();
280
+ childContext .active = false ;
281
+ childContext = null ;
282
+ jsonWriter .endObject ();
283
+ }
284
+ }
242
285
}
0 commit comments