本文整理汇总了Scala中com.fasterxml.jackson.annotation.JsonInclude类的典型用法代码示例。如果您正苦于以下问题:Scala JsonInclude类的具体用法?Scala JsonInclude怎么用?Scala JsonInclude使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JsonInclude类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: JsonObjectMapper
//设置package包名称以及导入依赖的类
package de.stema.util
import javax.inject.Singleton
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper, SerializationFeature}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import de.stema.pullrequests.dto.PullRequestDTO
import scala.reflect.ClassTag
import scala.util.{Failure, Success, Try}
@Singleton
class JsonObjectMapper {
private lazy val mapper = new ObjectMapper with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.configure(SerializationFeature.INDENT_OUTPUT, true)
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
def getInstance[T](json: String)(implicit ct: ClassTag[T]): T =
Try {
mapper.readValue(json, ct.runtimeClass).asInstanceOf[T]
} match {
case Success(instance) => instance
case Failure(e) => throw new IllegalStateException(s"Error during parsing of '$json'", e)
}
def getInstances[T](json: String)(implicit ct: ClassTag[T]): Seq[PullRequestDTO] =
Try {
mapper.readValue[Seq[PullRequestDTO]](json)
} match {
case Success(instances) => instances
case Failure(e) => throw new IllegalStateException(s"Error during parsing of '$json'", e)
}
}
示例2: JacksonMapper
//设置package包名称以及导入依赖的类
package config
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
object JacksonMapper extends ObjectMapper {
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
registerModule(DefaultScalaModule)
setSerializationInclusion(JsonInclude.Include.NON_ABSENT)
}
// Instead of converting to/from JSON, this converts to/from nested key/value properties.
object JacksonPropertyMapper extends JavaPropsMapper {
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
registerModule(DefaultScalaModule)
setSerializationInclusion(JsonInclude.Include.NON_ABSENT)
def productToKeyVals(p: Product): List[(String, String)] = {
// This is a naive two-stage algorithm that works simply. Maybe it could be improved later.
val props = writeValueAsString(p)
val lines = props.split('\n')
val keyVals =
for (line <- lines
if line.length > 1) yield {
divide(line, '=')
}
keyVals.toList
}
private def divide(s: String, c: Char): (String, String) = {
val i = s.indexOf(c)
if (i < 0) s -> ""
else {
val w1 = s.substring(0, i)
val rest = s.substring(i + 1)
w1 -> rest
}
}
}
示例3: GenericResponse
//设置package包名称以及导入依赖的类
package com.flipkart.connekt.commons.iomodels
import com.fasterxml.jackson.annotation.JsonInclude.Include
import com.fasterxml.jackson.annotation.JsonSubTypes.Type
import com.fasterxml.jackson.annotation.{JsonInclude, JsonSubTypes, JsonTypeInfo}
case class GenericResponse(status: Int, request: AnyRef, response: ResponseBody)
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type"
)
@JsonSubTypes(Array(
new Type(value = classOf[Response], name = "RESPONSE"),
new Type(value = classOf[SendResponse], name = "SEND_RESPONSE")
))
abstract class ResponseBody
case class Response(@JsonInclude(Include.NON_NULL) message: String, data: Any) extends ResponseBody
case class SendResponse(@JsonInclude(Include.NON_NULL) message: String, success: Map[String, Set[String]], failure: List[String]) extends ResponseBody
示例4: JacksonSupport
//设置package包名称以及导入依赖的类
package io.eels.util
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
object JacksonSupport {
val mapper: ObjectMapper with ScalaObjectMapper = new ObjectMapper with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false)
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
}
示例5: JsonUtil
//设置package包名称以及导入依赖的类
package uk.ac.wellcome.utils
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import scala.util.Try
object JsonUtil {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
def toJson(value: Any): Try[String] =
Try(mapper.writeValueAsString(value))
def toMap[V](json: String)(implicit m: Manifest[V]) =
fromJson[Map[String, V]](json)
def fromJson[T](json: String)(implicit m: Manifest[T]): Try[T] =
Try(mapper.readValue[T](json))
}
示例6: ProjectDefaultJacksonMapper
//设置package包名称以及导入依赖的类
package works.weave.socks.aws.orders
import com.fasterxml.jackson.annotation.JsonAutoDetect
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.PropertyAccessor
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
object ProjectDefaultJacksonMapper {
def build() : ObjectMapper = {
val mapper = new ObjectMapper()
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS)
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
mapper.enable(SerializationFeature.INDENT_OUTPUT)
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
val javaTime : JavaTimeModule = new JavaTimeModule
mapper.registerModule(javaTime)
val scalaModule = new DefaultScalaModule()
mapper.registerModule(scalaModule)
mapper
}
}
示例7: CatalogueBooleanProductAttribute
//设置package包名称以及导入依赖的类
package cjp.catalogue.model
import com.fasterxml.jackson.annotation.JsonInclude
@JsonInclude(JsonInclude.Include.NON_NULL)
case class CatalogueBooleanProductAttribute(value: Boolean,
label: String = "",
facet: String,
referenceText: String = "",
referenceUrl: String = "",
evidenceKinds: List[EvidenceKind] = Nil,
relatedProducts: List[String] = Nil) extends CatalogueAttribute {
def kind = "Boolean"
}
示例8: CatalogueIntegerProductAttribute
//设置package包名称以及导入依赖的类
package cjp.catalogue.model
import com.fasterxml.jackson.annotation.JsonInclude
@JsonInclude(JsonInclude.Include.NON_NULL)
case class CatalogueIntegerProductAttribute(value: Int,
label: String = "",
facet: String,
referenceText: String = "",
referenceUrl: String = "",
evidenceKinds: List[EvidenceKind] = Nil,
relatedProducts: List[String] = Nil) extends CatalogueAttribute {
def kind = "Integer"
}
示例9: CatalogueBigDecimalProductAttribute
//设置package包名称以及导入依赖的类
package cjp.catalogue.model
import com.fasterxml.jackson.annotation.JsonInclude
@JsonInclude(JsonInclude.Include.NON_NULL)
case class CatalogueBigDecimalProductAttribute(value: BigDecimal,
label: String = "",
facet: String,
referenceText: String,
referenceUrl: String,
evidenceKinds: List[EvidenceKind] = Nil,
relatedProducts: List[String] = Nil) extends CatalogueAttribute {
def kind = "Decimal"
}
示例10: CatalogueAttributeDto
//设置package包名称以及导入依赖的类
package cjp.catalogue.service
import cjp.catalogue.model.{CatalogueAttribute, EvidenceKind}
import com.fasterxml.jackson.annotation.JsonInclude
@JsonInclude(JsonInclude.Include.NON_NULL)
case class CatalogueAttributeDto(value: Any,
label: String,
facet: String,
referenceText: String,
referenceUrl: String,
evidenceKinds: List[EvidenceKind] = Nil,
relatedProducts: List[RelatedProductSummary] = Nil,
validValues : Map[String,Int] = Map())
case class RelatedProductSummary(name: String, title: String)
示例11: Jackson
//设置package包名称以及导入依赖的类
package teleporter.integration.utils
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper, SerializationFeature}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
object Jackson {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.registerModule(DefaultScalaModule)
implicit def toStr[T <: AnyRef](o: T): String = {
mapper.writeValueAsString(o)
}
}
示例12: MongoComponent
//设置package包名称以及导入依赖的类
package teleporter.integration.component.mongo
import java.util.Date
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper, SerializationFeature}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import org.bson.json.{JsonMode, JsonWriterSettings}
import org.mongodb.scala.bson._
import org.scalatest.FunSuite
//case class Person(id: Long, name: String, birth: Date)
class MongoComponent$Test extends FunSuite {
test("bson to json") {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
mapper.registerModule(new Bson2JsonModule)
mapper.registerModule(DefaultScalaModule)
val bsonDoc = BsonDocument(
"id" ? BsonObjectId(),
"string" ? BsonString("aa"),
"int" ? BsonInt32(3434),
"long" ? BsonInt64(43343L),
"double" ? BsonDouble(3434.33f),
"date" ? BsonDateTime(new Date())
)
println(bsonDoc.toJson(new JsonWriterSettings(JsonMode.SHELL)))
// println(bsonDoc.toJson(new JsonWriterSettings()))
val str = mapper.writeValueAsString(bsonDoc)
println(str)
}
test("json to bson") {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
mapper.registerModule(new Json2BsonModule)
mapper.registerModule(DefaultScalaModule)
val map = Map("int" ? 3434, "long" ? 3433333333333333343L, "float" ? 434.33f, "double" ? 343.4343d, "date" ? new Date())
println(mapper.writeValueAsString(map))
}
test("str to bson") {
val bson = BsonDocument(
"""
|{id:3434343434333}
""".stripMargin)
println(bson)
}
}
示例13: Yaml
//设置package包名称以及导入依赖的类
package com.sk.app.proxmock.toolset.serialization
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.sk.app.proxmock.toolset.serialization.betterpolymorphism.BetterPolymorphismModule
object Yaml {
private val mapper = new ObjectMapper(new YAMLFactory())
mapper.registerModule(DefaultScalaModule)
mapper.registerModule(BetterPolymorphismModule())
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
mapper.enable(JsonParser.Feature.ALLOW_YAML_COMMENTS)
mapper.enable(SerializationFeature.INDENT_OUTPUT)
def parse[T](yaml: String, clazz: Class[T]): T =
mapper.readValue(yaml, clazz)
def serialize[T](o: T): String =
mapper.writeValueAsString(o)
}