com.fasterxml.jackson.annotation.JsonInclude.Include Scala Examples
The following examples show how to use com.fasterxml.jackson.annotation.JsonInclude.Include.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: TestCaseClass.scala From daf with BSD 3-Clause "New" or "Revised" License | 5 votes |
import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.annotation.JsonInclude.Include import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.scala.DefaultScalaModule import de.zalando.play.controllers.WriterFactories import it.gov.daf.catalogmanager.{ConversionField, MetaCatalog, StdSchema} import play.api.libs.json._ object TestCaseClass extends App{ //@JsonInclude(JsonInclude.Include.NON_ABSENT) case class Pippo (ogg1: Option[String], ogg2: String, ogg3: Option[Pluto]) //case class Pippo (ogg1: Option[String], ogg2: String) //case class Pluto (prop1: String, prop2: String, prop3: List[String]) case class Pluto (prop1: String, prop2: String) val pippo = Pippo(Some("ciao"), "", None) //val pippo = Pippo(Some("ciao"), "ciao2") def jacksonMapper(mimeType: String): ObjectMapper = { //noinspection ScalaStyle assert(mimeType != null) val factory = WriterFactories.factories(mimeType) val mapper = new ObjectMapper(factory) mapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT) mapper.registerModule(DefaultScalaModule) mapper } val json = jacksonMapper("blabla").writeValueAsString(pippo) //val json = "{\"ogg1\":\"ciao\",\"ogg2\":\"ciao2\"}" println(json) //implicit val plutoReads = Json.reads[Pluto] //implicit val pippoReads = Json.reads[Pippo] //implicit val plutoReads = Json.reads[Option[Pluto]] }
Example 2
Source File: JsonUtils.scala From delta with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.delta.util import com.fasterxml.jackson.annotation.JsonInclude.Include import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper} import com.fasterxml.jackson.module.scala.DefaultScalaModule import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper val mapper = new ObjectMapper with ScalaObjectMapper mapper.setSerializationInclusion(Include.NON_ABSENT) mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) mapper.registerModule(DefaultScalaModule) def toJson[T: Manifest](obj: T): String = { mapper.writeValueAsString(obj) } def toPrettyJson[T: Manifest](obj: T): String = { mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj) } def fromJson[T: Manifest](json: String): T = { mapper.readValue[T](json) } }
Example 3
Source File: ObjectMapping.scala From ncdbg with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.programmaticallyspeaking.ncd.infra import com.fasterxml.jackson.annotation.JsonInclude.Include import com.fasterxml.jackson.core.JsonGenerator import com.fasterxml.jackson.databind._ import com.fasterxml.jackson.databind.ser.Serializers import com.fasterxml.jackson.databind.ser.std.StdSerializer import com.fasterxml.jackson.module.scala.{DefaultScalaModule, JacksonModule} import com.programmaticallyspeaking.ncd.chrome.domains.Runtime.RemoteObject import scala.reflect.ClassTag private class RemoteObjectSerializer extends StdSerializer[RemoteObject](classOf[RemoteObject]) { override def serialize(value: RemoteObject, gen: JsonGenerator, provider: SerializerProvider): Unit = { assert(value.productArity == 8, "Expected RemoteObject to have 8 product elements, but it has " + value.productArity) gen.writeStartObject() // (`type`: String, subtype: String, className: String, description: String, value: Any, unserializableValue: String, objectId: String) write(value, value.`type`, "type", gen) write(value, value.subtype, "subtype", gen) write(value, value.className, "className", gen) write(value, value.description, "description", gen) write(value, value.value, "value", gen) write(value, value.unserializableValue, "unserializableValue", gen) write(value, value.objectId, "objectId", gen) write(value, value.preview, "preview", gen) gen.writeEndObject() } private def write(remoteObject: RemoteObject, value: Any, field: String, gen: JsonGenerator): Unit = value match { case Some(x) => gen.writeObjectField(field, x) case None => // omit case other => gen.writeObjectField(field, other) } } private object RemoteObjectSerializerResolver extends Serializers.Base { private val REMOTE_OBJECT = classOf[RemoteObject] override def findSerializer(config: SerializationConfig, `type`: JavaType, beanDesc: BeanDescription): JsonSerializer[_] = { if (!REMOTE_OBJECT.isAssignableFrom(`type`.getRawClass)) null else new RemoteObjectSerializer() } } trait RemoteObjectModule extends JacksonModule { this += (_ addSerializers RemoteObjectSerializerResolver) } object ObjectMapping { private val mapper = new ObjectMapper() mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) mapper.setSerializationInclusion(Include.NON_ABSENT) mapper.registerModule(new DefaultScalaModule with RemoteObjectModule) def fromJson[R <: Any : ClassTag](json: String): R = { val clazz = implicitly[ClassTag[R]].runtimeClass fromJson(json, clazz).asInstanceOf[R] } def fromJson[R](json: String, clazz: Class[R]): R = mapper.readValue(json, clazz) def toJson[A](obj: A) = mapper.writeValueAsString(obj) def fromMap[R](map: Map[String, Any], clazz: Class[R]): R = mapper.convertValue(map, clazz) }