1package com.fasterxml.jackson.databind.ser.std;
2
3import java.io.IOException;
4import java.lang.reflect.InvocationTargetException;
5import java.lang.reflect.Type;
6import java.util.LinkedHashSet;
7import java.util.Set;
8
9import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
10import com.fasterxml.jackson.core.*;
11import com.fasterxml.jackson.databind.*;
12import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
13import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
14import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable;
15import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
16import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor;
17import com.fasterxml.jackson.databind.jsonschema.SchemaAware;
18import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
19import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
20import com.fasterxml.jackson.databind.ser.BeanSerializer;
21import com.fasterxml.jackson.databind.ser.ContextualSerializer;
22
23/**
24 * Serializer class that can serialize Object that have a
…lines 25–221 unchanged (197 lines)
222 return;
223 }
224 }
225 // 28-Sep-2016, tatu: As per [databind#1385], we do need to do some juggling
226 // to use different Object for type id (logical type) and actual serialization
227 // (delegat type).
228 TypeSerializerRerouter rr = new TypeSerializerRerouter(typeSer0, bean);
229 ser.serializeWithType(value, gen, provider, rr);
230 } catch (IOException ioe) {
231 throw ioe;
232 } catch (Exception e) {
233 Throwable t = e;
234 // Need to unwrap this specific type, to see infinite recursion...
235 while (t instanceof InvocationTargetException && t.getCause() != null) {
…lines 236–359 unchanged (124 lines)
360
361 /**
362 * Silly little wrapper class we need to re-route type serialization so that we can
363 * override Object to use for type id (logical type) even when asking serialization
364 * of something else (delegate type)
365 */
366 static class TypeSerializerRerouter
367 extends TypeSerializer
368 {
369 protected final TypeSerializer _typeSerializer;
370 protected final Object _forObject;
371
372 public TypeSerializerRerouter(TypeSerializer ts, Object ob) {
373 _typeSerializer = ts;
374 _forObject = ob;
375 }
376
377 @Override
378 public TypeSerializer forProperty(BeanProperty prop) { // should never get called
379 throw new UnsupportedOperationException();
380 }
381
382 @Override
383 public As getTypeInclusion() {
384 return _typeSerializer.getTypeInclusion();
385 }
386
387 @Override
388 public String getPropertyName() {
389 return _typeSerializer.getPropertyName();
390 }
391
392 @Override
393 public TypeIdResolver getTypeIdResolver() {
394 return _typeSerializer.getTypeIdResolver();
395 }
396
397 @Override
398 public void writeTypePrefixForScalar(Object value, JsonGenerator gen) throws IOException {
399 _typeSerializer.writeTypePrefixForScalar(_forObject, gen);
400 }
401
402 @Override
403 public void writeTypePrefixForObject(Object value, JsonGenerator gen) throws IOException {
404 _typeSerializer.writeTypePrefixForObject(_forObject, gen);
405 }
406
407 @Override
408 public void writeTypePrefixForArray(Object value, JsonGenerator gen) throws IOException {
409 _typeSerializer.writeTypePrefixForArray(_forObject, gen);
410 }
411
412 @Override
413 public void writeTypeSuffixForScalar(Object value, JsonGenerator gen) throws IOException {
414 _typeSerializer.writeTypeSuffixForScalar(_forObject, gen);
415 }
416
417 @Override
418 public void writeTypeSuffixForObject(Object value, JsonGenerator gen) throws IOException {
419 _typeSerializer.writeTypeSuffixForObject(_forObject, gen);
420 }
421
422 @Override
423 public void writeTypeSuffixForArray(Object value, JsonGenerator gen) throws IOException {
424 _typeSerializer.writeTypeSuffixForArray(_forObject, gen);
425 }
426
427 public void writeTypePrefixForScalar(Object value, JsonGenerator gen, Class<?> type) throws IOException {
428 _typeSerializer.writeTypePrefixForScalar(_forObject, gen, type);
429 }
430
431 public void writeTypePrefixForObject(Object value, JsonGenerator gen, Class<?> type) throws IOException {
432 _typeSerializer.writeTypePrefixForObject(_forObject, gen, type);
433 }
434
435 public void writeTypePrefixForArray(Object value, JsonGenerator gen, Class<?> type) throws IOException {
436 _typeSerializer.writeTypePrefixForArray(_forObject, gen, type);
437 }
438
439 @Override
440 public void writeCustomTypePrefixForScalar(Object value, JsonGenerator gen, String typeId)
441 throws IOException {
442 _typeSerializer.writeCustomTypePrefixForScalar(_forObject, gen, typeId);
443 }
444
445 @Override
446 public void writeCustomTypePrefixForObject(Object value, JsonGenerator gen, String typeId) throws IOException {
447 _typeSerializer.writeCustomTypePrefixForObject(_forObject, gen, typeId);
448 }
449
450 @Override
451 public void writeCustomTypePrefixForArray(Object value, JsonGenerator gen, String typeId) throws IOException {
452 _typeSerializer.writeCustomTypePrefixForArray(_forObject, gen, typeId);
453 }
454
455 @Override
456 public void writeCustomTypeSuffixForScalar(Object value, JsonGenerator gen, String typeId) throws IOException {
457 _typeSerializer.writeCustomTypeSuffixForScalar(_forObject, gen, typeId);
458 }
459
460 @Override
461 public void writeCustomTypeSuffixForObject(Object value, JsonGenerator gen, String typeId) throws IOException {
462 _typeSerializer.writeCustomTypeSuffixForObject(_forObject, gen, typeId);
463 }
464
465 @Override
466 public void writeCustomTypeSuffixForArray(Object value, JsonGenerator gen, String typeId) throws IOException {
467 _typeSerializer.writeCustomTypeSuffixForArray(_forObject, gen, typeId);
468 }
469 }
470}
471