blob: 6211d71281b5772eec9862e11363b31b4592dc3a [file] [log] [blame]
Tatu Salorantae4f23bb2011-12-23 00:31:35 -08001package com.fasterxml.jackson.databind.ser.std;
2
3import java.io.IOException;
Tatu Saloranta64fd1812014-10-11 18:48:34 -07004import java.util.*;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -08005
Tatu Saloranta1b253d32011-12-23 00:44:25 -08006import com.fasterxml.jackson.core.*;
Tatu Salorantad10146c2012-01-31 20:07:28 -08007import com.fasterxml.jackson.databind.*;
Tatu Salorantadf6302f2011-12-23 20:05:35 -08008import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -08009
Cowtowncoder257ae1c2014-12-23 15:05:46 -080010@SuppressWarnings("serial")
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080011public class EnumSetSerializer
12 extends AsArraySerializerBase<EnumSet<? extends Enum<?>>>
13{
Tatu Saloranta9ed3abf2015-06-19 10:51:11 -070014 /**
15 * @since 2.6
16 */
17 public EnumSetSerializer(JavaType elemType) {
18 super(EnumSet.class, elemType, true, null, null);
19 }
20
Tatua57eca72012-02-01 15:26:14 -080021 public EnumSetSerializer(EnumSetSerializer src,
Tatu Saloranta9ed3abf2015-06-19 10:51:11 -070022 BeanProperty property, TypeSerializer vts, JsonSerializer<?> valueSerializer,
23 Boolean unwrapSingle) {
24 super(src, property, vts, valueSerializer, unwrapSingle);
Tatu Salorantad10146c2012-01-31 20:07:28 -080025 }
26
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080027 @Override
Tatu Salorantad10146c2012-01-31 20:07:28 -080028 public EnumSetSerializer _withValueTypeSerializer(TypeSerializer vts) {
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080029 // no typing for enums (always "hard" type)
30 return this;
31 }
Tatu56e8e6c2012-01-12 16:09:41 -080032
33 @Override
Tatu Salorantad10146c2012-01-31 20:07:28 -080034 public EnumSetSerializer withResolved(BeanProperty property,
Tatu Saloranta9ed3abf2015-06-19 10:51:11 -070035 TypeSerializer vts, JsonSerializer<?> elementSerializer,
36 Boolean unwrapSingle) {
37 return new EnumSetSerializer(this, property, vts, elementSerializer, unwrapSingle);
Tatu Salorantad10146c2012-01-31 20:07:28 -080038 }
39
40 @Override
Cowtowncoderdfa1b442014-12-29 15:55:05 -080041 public boolean isEmpty(SerializerProvider prov, EnumSet<? extends Enum<?>> value) {
Tatu Salorantafaf68c52016-10-30 22:58:08 -070042 return value.isEmpty();
Tatu56e8e6c2012-01-12 16:09:41 -080043 }
Tatu Saloranta1421af62012-03-09 21:46:44 -080044
45 @Override
46 public boolean hasSingleElement(EnumSet<? extends Enum<?>> value) {
47 return value.size() == 1;
48 }
Tatu Saloranta64fd1812014-10-11 18:48:34 -070049
50 @Override
Tatu Saloranta9ed3abf2015-06-19 10:51:11 -070051 public final void serialize(EnumSet<? extends Enum<?>> value, JsonGenerator gen,
52 SerializerProvider provider) throws IOException
Tatu Saloranta64fd1812014-10-11 18:48:34 -070053 {
Tatu Saloranta53bd8da2016-10-19 15:58:39 -070054 final int len = value.size();
Tatu Saloranta9ed3abf2015-06-19 10:51:11 -070055 if (len == 1) {
56 if (((_unwrapSingle == null)
57 && provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
58 || (_unwrapSingle == Boolean.TRUE)) {
59 serializeContents(value, gen, provider);
60 return;
61 }
Tatu Saloranta64fd1812014-10-11 18:48:34 -070062 }
Tatu Saloranta9ed3abf2015-06-19 10:51:11 -070063 gen.writeStartArray(len);
64 serializeContents(value, gen, provider);
65 gen.writeEndArray();
Tatu Saloranta64fd1812014-10-11 18:48:34 -070066 }
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080067
68 @Override
Tatu Saloranta9ed3abf2015-06-19 10:51:11 -070069 public void serializeContents(EnumSet<? extends Enum<?>> value, JsonGenerator gen,
70 SerializerProvider provider)
71 throws IOException
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080072 {
73 JsonSerializer<Object> enumSer = _elementSerializer;
74 /* Need to dynamically find instance serializer; unfortunately
75 * that seems to be the only way to figure out type (no accessors
76 * to the enum class that set knows)
77 */
78 for (Enum<?> en : value) {
79 if (enumSer == null) {
Oliver Kopp5e3bf082017-06-27 14:12:19 +030080 /* 12-Jan-2010, tatu: Since enums cannot be polymorphic, let's
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080081 * not bother with typed serializer variant here
82 */
83 enumSer = provider.findValueSerializer(en.getDeclaringClass(), _property);
84 }
Tatu Saloranta9ed3abf2015-06-19 10:51:11 -070085 enumSer.serialize(en, gen, provider);
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080086 }
87 }
88}