Skip to content

Commit 60ce114

Browse files
committed
code reuse
1 parent 366a1f8 commit 60ce114

File tree

2 files changed

+34
-87
lines changed

2 files changed

+34
-87
lines changed

src/main/scala/tools/jackson/module/scala/ser/ScalaIterableSerializer.scala

Lines changed: 20 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,115 +6,59 @@ import tools.jackson.databind.ser.std.{AsArraySerializerBase, StdContainerSerial
66
import tools.jackson.databind._
77

88
import java.{lang => jl}
9-
import scala.util.control.NonFatal
109

1110
private case class ScalaIterableSerializer(elemType: JavaType, staticTyping: Boolean, vts: TypeSerializer,
1211
property: BeanProperty, valueSerializer: ValueSerializer[Object],
1312
unwrapSingle: jl.Boolean, suppressableValue: Any, suppressNulls: Boolean)
1413
extends AsArraySerializerBase[collection.Iterable[Any]](collection.Iterable.getClass, elemType, staticTyping, vts, valueSerializer,
1514
unwrapSingle, property, suppressableValue, suppressNulls) {
1615

16+
private val iteratorSerializer = ScalaIteratorSerializer(elemType, staticTyping = staticTyping, vts,
17+
property, valueSerializer, unwrapSingle = unwrapSingle,
18+
suppressableValue = suppressableValue, suppressNulls = suppressNulls)
19+
1720
@deprecated(since = "3.1.0")
1821
def this(elemType: JavaType, staticTyping: Boolean, vts: TypeSerializer,
1922
property: BeanProperty, valueSerializer: ValueSerializer[Object], unwrapSingle: jl.Boolean) = {
20-
this(elemType, staticTyping, vts, property, valueSerializer, unwrapSingle, None.orNull, false)
23+
this(elemType, staticTyping, vts, property, valueSerializer, unwrapSingle = unwrapSingle,
24+
suppressableValue = None.orNull, suppressNulls = false)
2125
}
2226

2327
def this(elemType: JavaType, staticTyping: Boolean, vts: TypeSerializer, valueSerializer: ValueSerializer[Object]) = {
24-
this(elemType, staticTyping, vts, None.orNull, valueSerializer, None.orNull, None.orNull, false)
28+
this(elemType, staticTyping, vts, property = None.orNull, valueSerializer, unwrapSingle = None.orNull,
29+
suppressableValue = None.orNull, suppressNulls = false)
2530
}
2631

2732
@deprecated(since = "3.1.0")
2833
def this(src: ScalaIterableSerializer, property: BeanProperty, vts: TypeSerializer, valueSerializer: ValueSerializer[_],
2934
unwrapSingle: jl.Boolean) = {
3035
this(src.elemType, src.staticTyping, vts, property, valueSerializer.asInstanceOf[ValueSerializer[Object]],
31-
unwrapSingle, None.orNull, false)
36+
unwrapSingle = unwrapSingle, suppressableValue = None.orNull, suppressNulls = false)
3237
}
3338

3439
def this(src: ScalaIterableSerializer, property: BeanProperty, vts: TypeSerializer, valueSerializer: ValueSerializer[_],
3540
unwrapSingle: jl.Boolean, suppressableValue: Any, suppressNulls: Boolean) = {
3641
this(src.elemType, src.staticTyping, vts, property, valueSerializer.asInstanceOf[ValueSerializer[Object]],
37-
unwrapSingle, suppressableValue, suppressNulls)
42+
unwrapSingle = unwrapSingle, suppressableValue = suppressableValue, suppressNulls = suppressNulls)
3843
}
3944

4045
override def isEmpty(prov: SerializationContext, value: Iterable[Any]): Boolean = value.isEmpty
4146

4247
override def hasSingleElement(value: Iterable[Any]): Boolean = value.size == 1
4348

44-
override def serialize(value: Iterable[Any], g: JsonGenerator, serializationContext: SerializationContext): Unit = {
45-
if (((_unwrapSingle == null && serializationContext.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
46-
|| _unwrapSingle) && hasSingleElement(value)) {
47-
serializeContents(value, g, serializationContext)
48-
} else {
49-
g.writeStartArray(value)
50-
serializeContents(value, g, serializationContext)
51-
g.writeEndArray()
52-
}
53-
}
49+
override def serialize(value: Iterable[Any], g: JsonGenerator, serializationContext: SerializationContext): Unit =
50+
iteratorSerializer.serialize(value.iterator, g, serializationContext)
5451

55-
override def serializeContents(value: Iterable[Any], g: JsonGenerator, serializationContext: SerializationContext): Unit = {
56-
g.assignCurrentValue(value)
57-
if (_elementSerializer != null) {
58-
serializeContentsUsing(value, g, serializationContext, _elementSerializer)
59-
} else {
60-
val it = value.iterator
61-
if (it.hasNext) {
62-
val typeSer = _valueTypeSerializer
63-
var serializers = _dynamicValueSerializers
64-
var i = 0
65-
try while (it.hasNext) {
66-
val elem = it.next()
67-
if (elem == null) serializationContext.defaultSerializeNullValue(g)
68-
else {
69-
val cc = elem.getClass
70-
var serializer = serializers.serializerFor(cc)
71-
if (serializer == null) {
72-
if (_elementType.hasGenericTypes)
73-
serializer = _findAndAddDynamic(serializationContext, serializationContext.constructSpecializedType(_elementType, cc))
74-
else
75-
serializer = _findAndAddDynamic(serializationContext, cc)
76-
serializers = _dynamicValueSerializers
77-
}
78-
if (typeSer == null) serializer.serialize(elem.asInstanceOf[Object], g, serializationContext)
79-
else serializer.serializeWithType(elem.asInstanceOf[Object], g, serializationContext, typeSer)
80-
}
81-
i += 1
82-
}
83-
catch {
84-
case NonFatal(e) =>
85-
wrapAndThrow(serializationContext, e, value, i)
86-
}
87-
}
88-
}
89-
}
52+
override def serializeContents(value: Iterable[Any], g: JsonGenerator, serializationContext: SerializationContext): Unit =
53+
iteratorSerializer.serializeContents(value.iterator, g, serializationContext)
9054

9155
override def withResolved(property: BeanProperty, vts: TypeSerializer, elementSerializer: ValueSerializer[_],
9256
unwrapSingle: jl.Boolean, suppressableValue: Any,
93-
suppressNulls: Boolean): AsArraySerializerBase[Iterable[Any]] = {
94-
new ScalaIterableSerializer(this, property, vts, elementSerializer, unwrapSingle, suppressableValue, suppressNulls)
95-
}
57+
suppressNulls: Boolean): AsArraySerializerBase[Iterable[Any]] =
58+
new ScalaIterableSerializer(this, property, vts, elementSerializer, unwrapSingle = unwrapSingle,
59+
suppressableValue = suppressableValue, suppressNulls = suppressNulls)
9660

97-
override def _withValueTypeSerializer(vts: TypeSerializer): StdContainerSerializer[_] = {
98-
new ScalaIterableSerializer(this, _property, vts, _elementSerializer, _unwrapSingle, _suppressableValue, _suppressNulls)
99-
}
100-
101-
private def serializeContentsUsing(value: Iterable[Any], g: JsonGenerator, serializationContext: SerializationContext, ser: ValueSerializer[AnyRef]): Unit = {
102-
val it = value.iterator
103-
if (it.hasNext) {
104-
val typeSer = _valueTypeSerializer
105-
var i = 0
106-
while (it.hasNext) {
107-
val elem = it.next()
108-
try {
109-
if (elem == null) serializationContext.defaultSerializeNullValue(g)
110-
else if (typeSer == null) ser.serialize(elem.asInstanceOf[Object], g, serializationContext)
111-
else ser.serializeWithType(elem.asInstanceOf[Object], g, serializationContext, typeSer)
112-
i += 1
113-
} catch {
114-
case NonFatal(e) =>
115-
wrapAndThrow(serializationContext, e, value, i)
116-
}
117-
}
118-
}
119-
}
61+
override def _withValueTypeSerializer(vts: TypeSerializer): StdContainerSerializer[_] =
62+
new ScalaIterableSerializer(this, _property, vts, _elementSerializer, unwrapSingle = _unwrapSingle,
63+
suppressableValue = _suppressableValue, suppressNulls = _suppressNulls)
12064
}

src/main/scala/tools/jackson/module/scala/ser/ScalaIteratorSerializer.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,31 @@ private case class ScalaIteratorSerializer(elemType: JavaType, staticTyping: Boo
1717
@deprecated(since = "3.1.0")
1818
def this(elemType: JavaType, staticTyping: Boolean, vts: TypeSerializer,
1919
property: BeanProperty, valueSerializer: ValueSerializer[Object], unwrapSingle: jl.Boolean) = {
20-
this(elemType, staticTyping, vts, property, valueSerializer, unwrapSingle, None.orNull, false)
20+
this(elemType, staticTyping, vts, property, valueSerializer, unwrapSingle = unwrapSingle,
21+
suppressableValue = None.orNull, suppressNulls = false)
2122
}
2223

2324
def this(elemType: JavaType, staticTyping: Boolean, vts: TypeSerializer) = {
24-
this(elemType, staticTyping, vts, None.orNull, None.orNull, None.orNull, None.orNull, false)
25+
this(elemType, staticTyping, vts, property = None.orNull, valueSerializer = None.orNull,
26+
unwrapSingle = None.orNull, suppressableValue = None.orNull, suppressNulls = false)
2527
}
2628

2729
def this(elemType: JavaType, staticTyping: Boolean, vts: TypeSerializer, valueSerializer: ValueSerializer[Object]) = {
28-
this(elemType, staticTyping, vts, None.orNull, valueSerializer, None.orNull, None.orNull, false)
30+
this(elemType, staticTyping, vts, property = None.orNull, valueSerializer, unwrapSingle = None.orNull,
31+
suppressableValue = None.orNull, suppressNulls = false)
2932
}
3033

3134
@deprecated(since = "3.1.0")
3235
def this(src: ScalaIteratorSerializer, property: BeanProperty, vts: TypeSerializer, valueSerializer: ValueSerializer[_],
3336
unwrapSingle: jl.Boolean) = {
3437
this(src.elemType, src.staticTyping, vts, property, valueSerializer.asInstanceOf[ValueSerializer[Object]],
35-
unwrapSingle, None.orNull, false)
38+
unwrapSingle, suppressableValue = None.orNull, suppressNulls = false)
3639
}
3740

3841
def this(src: ScalaIteratorSerializer, property: BeanProperty, vts: TypeSerializer, valueSerializer: ValueSerializer[_],
3942
unwrapSingle: jl.Boolean, suppressableValue: Any, suppressNulls: Boolean) = {
4043
this(src.elemType, src.staticTyping, vts, property, valueSerializer.asInstanceOf[ValueSerializer[Object]],
41-
unwrapSingle, suppressableValue, suppressNulls)
44+
unwrapSingle, suppressableValue = suppressableValue, suppressNulls = suppressNulls)
4245
}
4346

4447
override def isEmpty(prov: SerializationContext, value: Iterator[Any]): Boolean = value.isEmpty
@@ -90,13 +93,13 @@ private case class ScalaIteratorSerializer(elemType: JavaType, staticTyping: Boo
9093

9194
override def withResolved(property: BeanProperty, vts: TypeSerializer, elementSerializer: ValueSerializer[_],
9295
unwrapSingle: jl.Boolean, suppressableValue: Any,
93-
suppressNulls: Boolean): AsArraySerializerBase[Iterator[Any]] = {
94-
new ScalaIteratorSerializer(this, property, vts, elementSerializer, unwrapSingle, suppressableValue, suppressNulls)
95-
}
96+
suppressNulls: Boolean): AsArraySerializerBase[Iterator[Any]] =
97+
new ScalaIteratorSerializer(this, property, vts, elementSerializer, unwrapSingle = unwrapSingle,
98+
suppressableValue = suppressableValue, suppressNulls = suppressNulls)
9699

97-
override def _withValueTypeSerializer(vts: TypeSerializer): StdContainerSerializer[_] = {
98-
new ScalaIteratorSerializer(this, _property, vts, _elementSerializer, _unwrapSingle, _suppressableValue, _suppressNulls)
99-
}
100+
override def _withValueTypeSerializer(vts: TypeSerializer): StdContainerSerializer[_] =
101+
new ScalaIteratorSerializer(this, _property, vts, _elementSerializer, unwrapSingle = _unwrapSingle,
102+
suppressableValue = _suppressableValue, suppressNulls = _suppressNulls)
100103

101104
private def serializeContentsUsing(it: Iterator[Any], g: JsonGenerator, serializationContext: SerializationContext, ser: ValueSerializer[AnyRef]): Unit = {
102105
if (it.hasNext) {

0 commit comments

Comments
 (0)