-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
848 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/test/scala/doc/org/apache/pekko/serialization/jackson/CustomAdtSerializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* license agreements; and to You under the Apache License, version 2.0: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This file is part of the Apache Pekko project, which was derived from Akka. | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2019-2022 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package doc.org.apache.pekko.serialization.jackson | ||
|
||
object CustomAdtSerializer { | ||
|
||
// #adt-trait-object | ||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer | ||
|
||
@JsonSerialize(`using` = classOf[DirectionJsonSerializer]) | ||
@JsonDeserialize(`using` = classOf[DirectionJsonDeserializer]) | ||
sealed trait Direction | ||
|
||
object Direction { | ||
case object North extends Direction | ||
case object East extends Direction | ||
case object South extends Direction | ||
case object West extends Direction | ||
} | ||
|
||
class DirectionJsonSerializer extends StdSerializer[Direction](classOf[Direction]) { | ||
import Direction._ | ||
|
||
override def serialize(value: Direction, gen: JsonGenerator, provider: SerializerProvider): Unit = { | ||
val strValue = value match { | ||
case North => "N" | ||
case East => "E" | ||
case South => "S" | ||
case West => "W" | ||
} | ||
gen.writeString(strValue) | ||
} | ||
} | ||
|
||
class DirectionJsonDeserializer extends StdDeserializer[Direction](classOf[Direction]) { | ||
import Direction._ | ||
|
||
override def deserialize(p: JsonParser, ctxt: DeserializationContext): Direction = { | ||
p.getText match { | ||
case "N" => North | ||
case "E" => East | ||
case "S" => South | ||
case "W" => West | ||
} | ||
} | ||
} | ||
|
||
final case class Compass(currentDirection: Direction) extends MySerializable | ||
// #adt-trait-object | ||
} |
Oops, something went wrong.