Skip to content

Commit 377aab0

Browse files
committed
Some work wrt #588; minor refactoring to support custom ObjectWriter sub-classes
1 parent 83f1be0 commit 377aab0

12 files changed

+169
-96
lines changed

src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java

+26-7
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ public JsonSerializer<T> replaceDelegatee(JsonSerializer<?> delegatee) {
9797
* values of type this serializer handles.
9898
*
9999
* @param value Value to serialize; can <b>not</b> be null.
100-
* @param jgen Generator used to output resulting Json content
100+
* @param gen Generator used to output resulting Json content
101101
* @param provider Provider that can be used to get serializers for
102102
* serializing Objects value contains, if any.
103103
*/
104-
public abstract void serialize(T value, JsonGenerator jgen, SerializerProvider provider)
104+
public abstract void serialize(T value, JsonGenerator gen, SerializerProvider provider)
105105
throws IOException, JsonProcessingException;
106106

107107
/**
@@ -116,22 +116,22 @@ public abstract void serialize(T value, JsonGenerator jgen, SerializerProvider p
116116
* implementation would look like:
117117
*<pre>
118118
* // note: method to call depends on whether this type is serialized as JSON scalar, object or Array!
119-
* typeSer.writeTypePrefixForScalar(value, jgen);
120-
* serialize(value, jgen, provider);
121-
* typeSer.writeTypeSuffixForScalar(value, jgen);
119+
* typeSer.writeTypePrefixForScalar(value, gen);
120+
* serialize(value, gen, provider);
121+
* typeSer.writeTypeSuffixForScalar(value, gen);
122122
*</pre>
123123
* and implementations for type serialized as JSON Arrays or Objects would differ slightly,
124124
* as <code>START-ARRAY>/<code>END-ARRAY</code> and
125125
* <code>START-OBJECT>/<code>END-OBJECT</code> pairs
126126
* need to be properly handled with respect to serializing of contents.
127127
*
128128
* @param value Value to serialize; can <b>not</b> be null.
129-
* @param jgen Generator used to output resulting Json content
129+
* @param gen Generator used to output resulting Json content
130130
* @param provider Provider that can be used to get serializers for
131131
* serializing Objects value contains, if any.
132132
* @param typeSer Type serializer to use for including type information
133133
*/
134-
public void serializeWithType(T value, JsonGenerator jgen, SerializerProvider provider,
134+
public void serializeWithType(T value, JsonGenerator gen, SerializerProvider provider,
135135
TypeSerializer typeSer)
136136
throws IOException, JsonProcessingException
137137
{
@@ -169,11 +169,30 @@ public void serializeWithType(T value, JsonGenerator jgen, SerializerProvider pr
169169
* Default implementation will consider only null values to be empty.
170170
*
171171
* @since 2.0
172+
*
173+
* @deprecated Since 2.5 Use {@link #isEmpty(SerializerProvider, Object)} instead
172174
*/
175+
@Deprecated
173176
public boolean isEmpty(T value) {
174177
return (value == null);
175178
}
176179

180+
/**
181+
* Method called to check whether given serializable value is
182+
* considered "empty" value (for purposes of suppressing serialization
183+
* of empty values).
184+
*<p>
185+
* Default implementation will consider only null values to be empty.
186+
*<p>
187+
* NOTE: replaces {@link #isEmpty(Object)}, deprecated in 2.5
188+
*
189+
* @since 2.5
190+
*/
191+
public boolean isEmpty(SerializerProvider provider, T value) {
192+
// replace with implementation in 2.6 or later
193+
return isEmpty(value);
194+
}
195+
177196
/**
178197
* Method that can be called to see whether this serializer instance
179198
* will use Object Id to handle cyclic references.

src/main/java/com/fasterxml/jackson/databind/ObjectReader.java

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ public Version version() {
267267
/**********************************************************
268268
/* Methods sub-classes MUST override, used for constructing
269269
/* reader instances, (re)configuring parser instances
270+
/* Added in 2.5
270271
/**********************************************************
271272
*/
272273

src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java

+64-28
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.fasterxml.jackson.core.io.SegmentedStringWriter;
1111
import com.fasterxml.jackson.core.type.TypeReference;
1212
import com.fasterxml.jackson.core.util.*;
13-
1413
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
1514
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
1615
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
@@ -233,7 +232,44 @@ protected ObjectWriter(ObjectWriter base, JsonFactory f)
233232
public Version version() {
234233
return com.fasterxml.jackson.databind.cfg.PackageVersion.VERSION;
235234
}
236-
235+
236+
/*
237+
/**********************************************************
238+
/* Methods sub-classes MUST override, used for constructing
239+
/* reader instances, (re)configuring parser instances.
240+
/* Added in 2.5
241+
/**********************************************************
242+
*/
243+
244+
/**
245+
* Overridable factory method called by various "withXxx()" methods
246+
*
247+
* @since 2.5
248+
*/
249+
protected ObjectWriter _new(ObjectWriter base, JsonFactory f) {
250+
return new ObjectWriter(base, f);
251+
}
252+
253+
/**
254+
* Overridable factory method called by various "withXxx()" methods
255+
*
256+
* @since 2.5
257+
*/
258+
protected ObjectWriter _new(ObjectWriter base, SerializationConfig config) {
259+
return new ObjectWriter(base, config);
260+
}
261+
262+
/**
263+
* Overridable factory method called by various "withXxx()" methods
264+
*
265+
* @since 2.5
266+
*/
267+
protected ObjectWriter _new(ObjectWriter base, SerializationConfig config,
268+
JavaType rootType, JsonSerializer<Object> rootSer,
269+
PrettyPrinter pp, FormatSchema s, CharacterEscapes escapes) {
270+
return new ObjectWriter(base, config, rootType, rootSer, pp, s, escapes);
271+
}
272+
237273
/*
238274
/**********************************************************
239275
/* Life-cycle, fluent factories for SerializationFeature
@@ -246,7 +282,7 @@ public Version version() {
246282
*/
247283
public ObjectWriter with(SerializationFeature feature) {
248284
SerializationConfig newConfig = _config.with(feature);
249-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
285+
return (newConfig == _config) ? this : _new(this, newConfig);
250286
}
251287

252288
/**
@@ -255,7 +291,7 @@ public ObjectWriter with(SerializationFeature feature) {
255291
*/
256292
public ObjectWriter with(SerializationFeature first, SerializationFeature... other) {
257293
SerializationConfig newConfig = _config.with(first, other);
258-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
294+
return (newConfig == _config) ? this : _new(this, newConfig);
259295
}
260296

261297
/**
@@ -264,7 +300,7 @@ public ObjectWriter with(SerializationFeature first, SerializationFeature... oth
264300
*/
265301
public ObjectWriter withFeatures(SerializationFeature... features) {
266302
SerializationConfig newConfig = _config.withFeatures(features);
267-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
303+
return (newConfig == _config) ? this : _new(this, newConfig);
268304
}
269305

270306
/**
@@ -273,7 +309,7 @@ public ObjectWriter withFeatures(SerializationFeature... features) {
273309
*/
274310
public ObjectWriter without(SerializationFeature feature) {
275311
SerializationConfig newConfig = _config.without(feature);
276-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
312+
return (newConfig == _config) ? this : _new(this, newConfig);
277313
}
278314

279315
/**
@@ -282,7 +318,7 @@ public ObjectWriter without(SerializationFeature feature) {
282318
*/
283319
public ObjectWriter without(SerializationFeature first, SerializationFeature... other) {
284320
SerializationConfig newConfig = _config.without(first, other);
285-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
321+
return (newConfig == _config) ? this : _new(this, newConfig);
286322
}
287323

288324
/**
@@ -291,7 +327,7 @@ public ObjectWriter without(SerializationFeature first, SerializationFeature...
291327
*/
292328
public ObjectWriter withoutFeatures(SerializationFeature... features) {
293329
SerializationConfig newConfig = _config.withoutFeatures(features);
294-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
330+
return (newConfig == _config) ? this : _new(this, newConfig);
295331
}
296332

297333
/*
@@ -305,31 +341,31 @@ public ObjectWriter withoutFeatures(SerializationFeature... features) {
305341
*/
306342
public ObjectWriter with(JsonGenerator.Feature feature) {
307343
SerializationConfig newConfig = _config.with(feature);
308-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
344+
return (newConfig == _config) ? this : _new(this, newConfig);
309345
}
310346

311347
/**
312348
* @since 2.5
313349
*/
314350
public ObjectWriter withFeatures(JsonGenerator.Feature... features) {
315351
SerializationConfig newConfig = _config.withFeatures(features);
316-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
352+
return (newConfig == _config) ? this : _new(this, newConfig);
317353
}
318354

319355
/**
320356
* @since 2.5
321357
*/
322358
public ObjectWriter without(JsonGenerator.Feature feature) {
323359
SerializationConfig newConfig = _config.without(feature);
324-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
360+
return (newConfig == _config) ? this : _new(this, newConfig);
325361
}
326362

327363
/**
328364
* @since 2.5
329365
*/
330366
public ObjectWriter withoutFeatures(JsonGenerator.Feature... features) {
331367
SerializationConfig newConfig = _config.withoutFeatures(features);
332-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
368+
return (newConfig == _config) ? this : _new(this, newConfig);
333369
}
334370

335371
/*
@@ -348,7 +384,7 @@ public ObjectWriter withoutFeatures(JsonGenerator.Feature... features) {
348384
*/
349385
public ObjectWriter with(DateFormat df) {
350386
SerializationConfig newConfig = _config.with(df);
351-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
387+
return (newConfig == _config) ? this : _new(this, newConfig);
352388
}
353389

354390
/**
@@ -365,7 +401,7 @@ public ObjectWriter withDefaultPrettyPrinter() {
365401
*/
366402
public ObjectWriter with(FilterProvider filterProvider) {
367403
return (filterProvider == _config.getFilterProvider()) ? this
368-
: new ObjectWriter(this, _config.withFilters(filterProvider));
404+
: _new(this, _config.withFilters(filterProvider));
369405
}
370406

371407
/**
@@ -380,7 +416,7 @@ public ObjectWriter with(PrettyPrinter pp) {
380416
if (pp == null) {
381417
pp = NULL_PRETTY_PRINTER;
382418
}
383-
return new ObjectWriter(this, _config, _rootType, _rootSerializer,
419+
return _new(this, _config, _rootType, _rootSerializer,
384420
pp, _schema, _characterEscapes);
385421
}
386422

@@ -394,7 +430,7 @@ public ObjectWriter with(PrettyPrinter pp) {
394430
*/
395431
public ObjectWriter withRootName(String rootName) {
396432
SerializationConfig newConfig = _config.withRootName(rootName);
397-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
433+
return (newConfig == _config) ? this : _new(this, newConfig);
398434
}
399435

400436
/**
@@ -409,7 +445,7 @@ public ObjectWriter with(FormatSchema schema) {
409445
return this;
410446
}
411447
_verifySchemaType(schema);
412-
return new ObjectWriter(this, _config, _rootType, _rootSerializer,
448+
return _new(this, _config, _rootType, _rootSerializer,
413449
_prettyPrinter, schema, _characterEscapes);
414450
}
415451

@@ -442,7 +478,7 @@ public ObjectWriter forType(JavaType rootType)
442478
rootType = rootType.withStaticTyping();
443479
rootSer = _prefetchRootSerializer(_config, rootType);
444480
}
445-
return new ObjectWriter(this, _config, rootType, rootSer,
481+
return _new(this, _config, rootType, rootSer,
446482
_prettyPrinter, _schema, _characterEscapes);
447483
}
448484

@@ -498,17 +534,17 @@ public ObjectWriter withType(TypeReference<?> rootType) {
498534
*/
499535
public ObjectWriter withView(Class<?> view) {
500536
SerializationConfig newConfig = _config.withView(view);
501-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
537+
return (newConfig == _config) ? this : _new(this, newConfig);
502538
}
503539

504540
public ObjectWriter with(Locale l) {
505541
SerializationConfig newConfig = _config.with(l);
506-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
542+
return (newConfig == _config) ? this : _new(this, newConfig);
507543
}
508544

509545
public ObjectWriter with(TimeZone tz) {
510546
SerializationConfig newConfig = _config.with(tz);
511-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
547+
return (newConfig == _config) ? this : _new(this, newConfig);
512548
}
513549

514550
/**
@@ -519,7 +555,7 @@ public ObjectWriter with(TimeZone tz) {
519555
*/
520556
public ObjectWriter with(Base64Variant b64variant) {
521557
SerializationConfig newConfig = _config.with(b64variant);
522-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
558+
return (newConfig == _config) ? this : _new(this, newConfig);
523559
}
524560

525561
/**
@@ -529,47 +565,47 @@ public ObjectWriter with(CharacterEscapes escapes) {
529565
if (_characterEscapes == escapes) {
530566
return this;
531567
}
532-
return new ObjectWriter(this, _config, _rootType, _rootSerializer,
568+
return _new(this, _config, _rootType, _rootSerializer,
533569
_prettyPrinter, _schema, escapes);
534570
}
535571

536572
/**
537573
* @since 2.3
538574
*/
539575
public ObjectWriter with(JsonFactory f) {
540-
return (f == _generatorFactory) ? this : new ObjectWriter(this, f);
576+
return (f == _generatorFactory) ? this : _new(this, f);
541577
}
542578

543579
/**
544580
* @since 2.3
545581
*/
546582
public ObjectWriter with(ContextAttributes attrs) {
547583
SerializationConfig newConfig = _config.with(attrs);
548-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
584+
return (newConfig == _config) ? this : _new(this, newConfig);
549585
}
550586

551587
/**
552588
* @since 2.3
553589
*/
554590
public ObjectWriter withAttributes(Map<Object,Object> attrs) {
555591
SerializationConfig newConfig = _config.withAttributes(attrs);
556-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
592+
return (newConfig == _config) ? this : _new(this, newConfig);
557593
}
558594

559595
/**
560596
* @since 2.3
561597
*/
562598
public ObjectWriter withAttribute(Object key, Object value) {
563599
SerializationConfig newConfig = _config.withAttribute(key, value);
564-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
600+
return (newConfig == _config) ? this : _new(this, newConfig);
565601
}
566602

567603
/**
568604
* @since 2.3
569605
*/
570606
public ObjectWriter withoutAttribute(Object key) {
571607
SerializationConfig newConfig = _config.withoutAttribute(key);
572-
return (newConfig == _config) ? this : new ObjectWriter(this, newConfig);
608+
return (newConfig == _config) ? this : _new(this, newConfig);
573609
}
574610

575611
/*

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,15 @@ private final Object vanillaDeserialize(JsonParser p,
241241
final Object bean = _valueInstantiator.createUsingDefault(ctxt);
242242
// [databind#631]: Assign current value, to be accessible by custom serializers
243243
p.setCurrentValue(bean);
244+
244245
for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
245246
String propName = p.getCurrentName();
246247
p.nextToken();
247248
if (!_beanProperties.findDeserializeAndSet(p, ctxt, bean, propName)) {
248249
handleUnknownVanilla(p, ctxt, bean, propName);
249250
}
250251
}
251-
252+
252253
// 13-Dec-2014, tatu: For 2.6, we'll do:
253254
/*
254255
if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) {
@@ -261,7 +262,6 @@ private final Object vanillaDeserialize(JsonParser p,
261262
} while ((propName = p.nextFieldName()) != null);
262263
}
263264
*/
264-
265265
return bean;
266266
}
267267

0 commit comments

Comments
 (0)