Skip to content

Commit

Permalink
SerializationContext
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Nov 29, 2024
1 parent 6fbd841 commit 2369fd2
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ private object EnumSerializerShared {
}

private object EnumSerializer extends ValueSerializer[Enum] {
def serialize(value: Enum, jgen: JsonGenerator, provider: SerializerProvider): Unit =
def serialize(value: Enum, jgen: JsonGenerator, provider: SerializationContext): Unit =
jgen.writeString(value.toString)
}

private object EnumKeySerializer extends ValueSerializer[Enum] {
def serialize(value: Enum, jgen: JsonGenerator, provider: SerializerProvider): Unit =
def serialize(value: Enum, jgen: JsonGenerator, provider: SerializationContext): Unit =
jgen.writeName(value.toString)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private class EitherSerializer(left: EitherDetails,
else new EitherSerializer(newLeft, newRight, prop, contentIncl, dynamicSerializers)
}

protected[this] def createContextualDetails(prov: SerializerProvider,
protected[this] def createContextualDetails(prov: SerializationContext,
prop: BeanProperty,
details: EitherDetails): EitherDetails = {
val vts = details.valueTypeSerializer.optMap(_.forProperty(prov, prop))
Expand Down Expand Up @@ -76,7 +76,7 @@ private class EitherSerializer(left: EitherDetails,
details.copy(valueTypeSerializer = vts, valueSerializer = serializerOption)
}

override def createContextual(prov: SerializerProvider, prop: BeanProperty): ValueSerializer[_] = {
override def createContextual(prov: SerializationContext, prop: BeanProperty): ValueSerializer[_] = {
val propOpt = Option(prop)

val newLeft = createContextualDetails(prov, prop, left)
Expand All @@ -95,11 +95,11 @@ private class EitherSerializer(left: EitherDetails,
withResolved(propOpt, newLeft, newRight, newIncl)
}

override def serialize(value: Either[AnyRef, AnyRef], jgen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(value: Either[AnyRef, AnyRef], jgen: JsonGenerator, provider: SerializationContext): Unit = {
serialize(value, jgen, provider, None)
}

def serialize(value: Either[AnyRef, AnyRef], jgen: JsonGenerator, provider: SerializerProvider, vts: Option[TypeSerializer]): Unit = {
def serialize(value: Either[AnyRef, AnyRef], jgen: JsonGenerator, provider: SerializationContext, vts: Option[TypeSerializer]): Unit = {
val (field, content, details) = value match {
case Left(c) => ("l", c, left)
case Right(c) => ("r", c, right)
Expand All @@ -118,7 +118,7 @@ private class EitherSerializer(left: EitherDetails,
jgen.writeEndObject()
}

override def serializeWithType(value: Either[AnyRef, AnyRef], jgen: JsonGenerator, provider: SerializerProvider, typeSer: TypeSerializer): Unit = {
override def serializeWithType(value: Either[AnyRef, AnyRef], jgen: JsonGenerator, provider: SerializationContext, typeSer: TypeSerializer): Unit = {
if (value == null) {
provider.defaultSerializeNullValue(jgen)
} else {
Expand All @@ -129,7 +129,7 @@ private class EitherSerializer(left: EitherDetails,
}
}

protected[this] def findCachedSerializer(prov: SerializerProvider, typ: Class[_]): ValueSerializer[AnyRef] = {
protected[this] def findCachedSerializer(prov: SerializationContext, typ: Class[_]): ValueSerializer[AnyRef] = {
var ser = dynamicSerializers.serializerFor(typ).asInstanceOf[ValueSerializer[AnyRef]]
if (ser == null) {
ser = findSerializer(prov, typ, property)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import tools.jackson.module.scala.{JacksonModule, JsonScalaEnumeration, ScalaMod
trait ContextualEnumerationSerializer {
self: ValueSerializer[_] =>

override def createContextual(serializerProvider: SerializerProvider, beanProperty: BeanProperty): ValueSerializer[_] =
override def createContextual(SerializationContext: SerializationContext, beanProperty: BeanProperty): ValueSerializer[_] =
Option(beanProperty)
.optMap(_.getAnnotation(classOf[JsonScalaEnumeration]))
.map(_ => new AnnotatedEnumerationSerializer)
Expand All @@ -24,7 +24,7 @@ trait ContextualEnumerationSerializer {
* http://jira.codehaus.org/browse/JACKSON-211
*/
private class EnumerationSerializer extends ValueSerializer[scala.Enumeration#Value] with ContextualEnumerationSerializer {
override def serialize(value: scala.Enumeration#Value, jgen: JsonGenerator, provider: SerializerProvider) = {
override def serialize(value: scala.Enumeration#Value, jgen: JsonGenerator, provider: SerializationContext) = {
val parentEnum = value.asInstanceOf[AnyRef].getClass.getSuperclass.getDeclaredFields.find( f => f.getName == "$outer" )
.getOrElse(throw new RuntimeException("failed to find $outer field on Enumeration class"))
val enumClass = parentEnum.get(value).getClass.getName stripSuffix "$"
Expand All @@ -36,7 +36,7 @@ private class EnumerationSerializer extends ValueSerializer[scala.Enumeration#Va
}

private class AnnotatedEnumerationSerializer extends ValueSerializer[scala.Enumeration#Value] with ContextualEnumerationSerializer {
override def serialize(value: scala.Enumeration#Value, jgen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(value: scala.Enumeration#Value, jgen: JsonGenerator, provider: SerializationContext): Unit = {
provider.writeValue(jgen, value.toString)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private trait IterableSerializer
override def hasSingleElement(value: collection.Iterable[Any]): Boolean =
value.size == 1

override def serialize(value: collection.Iterable[Any], gen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(value: collection.Iterable[Any], gen: JsonGenerator, provider: SerializationContext): Unit = {
if (provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED) && hasSingleElement(value)) {
collectionSerializer.serializeContents(value, gen, provider)
} else {
Expand All @@ -33,7 +33,7 @@ private trait IterableSerializer
}
}

override def serializeContents(value: collection.Iterable[Any], gen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serializeContents(value: collection.Iterable[Any], gen: JsonGenerator, provider: SerializationContext): Unit = {
serialize(value, gen, provider)
}

Expand All @@ -43,7 +43,7 @@ private trait IterableSerializer
unwrapSingle: jl.Boolean): ResolvedIterableSerializer =
new ResolvedIterableSerializer(this, property, vts, elementSerializer, unwrapSingle)

override def isEmpty(provider: SerializerProvider, value: collection.Iterable[Any]): Boolean = value.isEmpty
override def isEmpty(provider: SerializationContext, value: collection.Iterable[Any]): Boolean = value.isEmpty
}

private class ResolvedIterableSerializer( src: IterableSerializer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private trait IteratorSerializer
override def hasSingleElement(p1: collection.Iterator[Any]): Boolean =
p1.size == 1

override def serialize(value: collection.Iterator[Any], jgen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(value: collection.Iterator[Any], jgen: JsonGenerator, provider: SerializationContext): Unit = {
if (provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED) && hasSingleElement(value)) {
iteratorSerializer.serializeContents(value, jgen, provider)
} else {
Expand All @@ -33,15 +33,15 @@ private trait IteratorSerializer
}
}

override def serializeContents(value: collection.Iterator[Any], gen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serializeContents(value: collection.Iterator[Any], gen: JsonGenerator, provider: SerializationContext): Unit = {
serialize(value, gen, provider)
}

override def withResolved(property: BeanProperty, vts: TypeSerializer, elementSerializer: ValueSerializer[_], unwrapSingle: jl.Boolean) =
new ResolvedIteratorSerializer(this, property, vts, elementSerializer, unwrapSingle)


override def isEmpty(provider: SerializerProvider, value: collection.Iterator[Any]): Boolean = value.hasNext
override def isEmpty(provider: SerializationContext, value: collection.Iterator[Any]): Boolean = value.hasNext
}

private class ResolvedIteratorSerializer( src: IteratorSerializer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.util.control.Breaks.{break, breakable}
// This is still here because it is used in other places like EitherSerializer, it is no
// longer used for the Option serializer
object OptionSerializer {
def useStatic(provider: SerializerProvider, property: Option[BeanProperty], referredType: Option[JavaType]): Boolean = {
def useStatic(provider: SerializationContext, property: Option[BeanProperty], referredType: Option[JavaType]): Boolean = {
if (referredType.isEmpty) false
// First: no serializer for `Object.class`, must be dynamic
else if (referredType.get.isJavaLangObject) false
Expand Down Expand Up @@ -57,17 +57,17 @@ object OptionSerializer {
}
}

def findSerializer(provider: SerializerProvider, typ: Class[_], prop: Option[BeanProperty]): ValueSerializer[AnyRef] = {
def findSerializer(provider: SerializationContext, typ: Class[_], prop: Option[BeanProperty]): ValueSerializer[AnyRef] = {
// Important: ask for TYPED serializer, in case polymorphic handling is needed!
provider.findTypedValueSerializer(typ, true).asInstanceOf[ValueSerializer[AnyRef]]
}

def findSerializer(provider: SerializerProvider, typ: JavaType, prop: Option[BeanProperty]): ValueSerializer[AnyRef] = {
def findSerializer(provider: SerializationContext, typ: JavaType, prop: Option[BeanProperty]): ValueSerializer[AnyRef] = {
// Important: ask for TYPED serializer, in case polymorphic handling is needed!
provider.findTypedValueSerializer(typ, true).asInstanceOf[ValueSerializer[AnyRef]]
}

def hasContentTypeAnnotation(provider: SerializerProvider, property: BeanProperty): Boolean = {
def hasContentTypeAnnotation(provider: SerializationContext, property: BeanProperty): Boolean = {
val intr = provider.getAnnotationIntrospector
if (property == null || intr == null) {
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ private case class ScalaIterableSerializer(elemType: JavaType, staticTyping: Boo
this(src.elemType, src.staticTyping, vts, property, valueSerializer.asInstanceOf[ValueSerializer[Object]], unwrapSingle)
}

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

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

override def serialize(value: Iterable[Any], g: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(value: Iterable[Any], g: JsonGenerator, provider: SerializationContext): Unit = {
if (((_unwrapSingle == null && provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
|| _unwrapSingle) && hasSingleElement(value)) {
serializeContents(value, g, provider)
Expand All @@ -36,7 +36,7 @@ private case class ScalaIterableSerializer(elemType: JavaType, staticTyping: Boo
}
}

override def serializeContents(value: Iterable[Any], g: JsonGenerator, provider: SerializerProvider): Unit = {
override def serializeContents(value: Iterable[Any], g: JsonGenerator, provider: SerializationContext): Unit = {
g.assignCurrentValue(value)
if (_elementSerializer != null) {
serializeContentsUsing(value, g, provider, _elementSerializer)
Expand Down Expand Up @@ -79,7 +79,7 @@ private case class ScalaIterableSerializer(elemType: JavaType, staticTyping: Boo
new ScalaIterableSerializer(this, _property, vts, _elementSerializer, _unwrapSingle)
}

private def serializeContentsUsing(value: Iterable[Any], g: JsonGenerator, provider: SerializerProvider, ser: ValueSerializer[AnyRef]): Unit = {
private def serializeContentsUsing(value: Iterable[Any], g: JsonGenerator, provider: SerializationContext, ser: ValueSerializer[AnyRef]): Unit = {
val it = value.iterator
if (it.hasNext) {
val typeSer = _valueTypeSerializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ private case class ScalaIteratorSerializer(elemType: JavaType, staticTyping: Boo
this(src.elemType, src.staticTyping, vts, property, valueSerializer.asInstanceOf[ValueSerializer[Object]], unwrapSingle)
}

override def isEmpty(prov: SerializerProvider, value: Iterator[Any]): Boolean = value.isEmpty
override def isEmpty(prov: SerializationContext, value: Iterator[Any]): Boolean = value.isEmpty

override def hasSingleElement(value: Iterator[Any]): Boolean = value.size == 1

override def serialize(value: Iterator[Any], g: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(value: Iterator[Any], g: JsonGenerator, provider: SerializationContext): Unit = {
//writeSingleElement is unsupported - also unsupported in tools.jackson.databind.ser.impl.IteratorSerializer
//calculating the length of iterators can be expensive
g.writeStartArray(value)
serializeContents(value, g, provider)
g.writeEndArray()
}

override def serializeContents(it: Iterator[Any], g: JsonGenerator, provider: SerializerProvider): Unit = {
override def serializeContents(it: Iterator[Any], g: JsonGenerator, provider: SerializationContext): Unit = {
g.assignCurrentValue(it)
if (_elementSerializer != null) {
serializeContentsUsing(it, g, provider, _elementSerializer)
Expand Down Expand Up @@ -79,7 +79,7 @@ private case class ScalaIteratorSerializer(elemType: JavaType, staticTyping: Boo
new ScalaIteratorSerializer(this, _property, vts, _elementSerializer, _unwrapSingle)
}

private def serializeContentsUsing(it: Iterator[Any], g: JsonGenerator, provider: SerializerProvider, ser: ValueSerializer[AnyRef]): Unit = {
private def serializeContentsUsing(it: Iterator[Any], g: JsonGenerator, provider: SerializationContext, ser: ValueSerializer[AnyRef]): Unit = {
if (it.hasNext) {
val typeSer = _valueTypeSerializer
var i = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import tools.jackson.module.scala.{JacksonModule, ScalaModule}
import scala.languageFeature.postfixOps

private object SymbolSerializer extends ValueSerializer[Symbol] {
def serialize(value: Symbol, jgen: JsonGenerator, provider: SerializerProvider): Unit =
def serialize(value: Symbol, jgen: JsonGenerator, provider: SerializationContext): Unit =
jgen.writeString(value.name)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scala.languageFeature.postfixOps

private class TupleSerializer extends ValueSerializer[Product] {

def serialize(value: Product, jgen: JsonGenerator, provider: SerializerProvider): Unit = {
def serialize(value: Product, jgen: JsonGenerator, provider: SerializationContext): Unit = {
jgen.writeStartArray()
value.productIterator.foreach(provider.writeValue(jgen, _))
jgen.writeEndArray()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tools.jackson.module.scala.ser

import tools.jackson.core.JsonGenerator
import tools.jackson.databind.{JacksonSerializable, SerializerProvider}
import tools.jackson.databind.{JacksonSerializable, SerializationContext}
import tools.jackson.databind.jsontype.TypeSerializer

import scala.collection.immutable.AbstractMap
Expand All @@ -14,10 +14,10 @@ class SerializableMap extends AbstractMap[String, String] with JacksonSerializab
def get(key: String): Option[String] = None
def iterator: Iterator[(String, String)] = throw new IllegalArgumentException("This shouldn't get called")

override def serialize(jgen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(jgen: JsonGenerator, provider: SerializationContext): Unit = {
jgen.writeNumber(10)
}
override def serializeWithType(jgen: JsonGenerator, provider: SerializerProvider, typeSer: TypeSerializer): Unit = {
override def serializeWithType(jgen: JsonGenerator, provider: SerializationContext, typeSer: TypeSerializer): Unit = {
serialize(jgen, provider)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tools.jackson.module.scala.ser

import tools.jackson.core.JsonGenerator
import tools.jackson.databind.{JacksonSerializable, SerializerProvider}
import tools.jackson.databind.{JacksonSerializable, SerializationContext}
import tools.jackson.databind.jsontype.TypeSerializer

import scala.collection.immutable.AbstractMap
Expand All @@ -14,10 +14,10 @@ class SerializableMap extends AbstractMap[String, String] with JacksonSerializab
def get(key: String): Option[String] = None
def iterator: Iterator[(String, String)] = throw new IllegalArgumentException("This shouldn't get called")

override def serialize(jgen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(jgen: JsonGenerator, provider: SerializationContext): Unit = {
jgen.writeNumber(10)
}
override def serializeWithType(jgen: JsonGenerator, provider: SerializerProvider, typeSer: TypeSerializer): Unit = {
override def serializeWithType(jgen: JsonGenerator, provider: SerializationContext, typeSer: TypeSerializer): Unit = {
serialize(jgen, provider)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tools.jackson.module.scala.ser

import tools.jackson.core.JsonGenerator
import tools.jackson.databind.jsontype.TypeSerializer
import tools.jackson.databind.{JacksonSerializable, SerializerProvider}
import tools.jackson.databind.{JacksonSerializable, SerializationContext}

import scala.collection.immutable.AbstractMap

Expand All @@ -12,10 +12,10 @@ class SerializableMap extends AbstractMap[String, String] with JacksonSerializab
def get(key: String): Option[String] = None
def iterator: Iterator[(String, String)] = throw new IllegalArgumentException("This shouldn't get called")

override def serialize(jgen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(jgen: JsonGenerator, provider: SerializationContext): Unit = {
jgen.writeNumber(10)
}
override def serializeWithType(jgen: JsonGenerator, provider: SerializerProvider, typeSer: TypeSerializer): Unit = {
override def serializeWithType(jgen: JsonGenerator, provider: SerializationContext, typeSer: TypeSerializer): Unit = {
serialize(jgen, provider)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tools.jackson.module.scala.ser

import tools.jackson.core.JsonGenerator
import tools.jackson.databind.jsontype.TypeSerializer
import tools.jackson.databind.{JacksonSerializable, SerializerProvider}
import tools.jackson.databind.{JacksonSerializable, SerializationContext}

import scala.collection.immutable.AbstractMap

Expand All @@ -12,10 +12,10 @@ class SerializableMap extends AbstractMap[String, String] with JacksonSerializab
def get(key: String): Option[String] = None
def iterator: Iterator[(String, String)] = throw new IllegalArgumentException("This shouldn't get called")

override def serialize(jgen: JsonGenerator, provider: SerializerProvider): Unit = {
override def serialize(jgen: JsonGenerator, provider: SerializationContext): Unit = {
jgen.writeNumber(10)
}
override def serializeWithType(jgen: JsonGenerator, provider: SerializerProvider, typeSer: TypeSerializer): Unit = {
override def serializeWithType(jgen: JsonGenerator, provider: SerializationContext, typeSer: TypeSerializer): Unit = {
serialize(jgen, provider)
}

Expand Down
Loading

0 comments on commit 2369fd2

Please sign in to comment.