本文整理汇总了Scala中com.fasterxml.jackson.core.JsonGenerator类的典型用法代码示例。如果您正苦于以下问题:Scala JsonGenerator类的具体用法?Scala JsonGenerator怎么用?Scala JsonGenerator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JsonGenerator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: CirceJsonSerializer
//设置package包名称以及导入依赖的类
package io.circe.jackson
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.{ JsonSerializer, SerializerProvider }
import io.circe.{ Json, JsonBigDecimal, JsonBiggerDecimal, JsonDecimal, JsonDouble, JsonFloat, JsonLong }
private[jackson] final object CirceJsonSerializer extends JsonSerializer[Json] {
import java.math.{ BigDecimal => JBigDecimal, BigInteger }
import com.fasterxml.jackson.databind.node.{ BigIntegerNode, DecimalNode }
override final def serialize(
value: Json,
json: JsonGenerator,
provider: SerializerProvider
): Unit = value match {
case Json.JNumber(v) => v match {
case JsonLong(x) => json.writeNumber(x)
case JsonDouble(x) => json.writeNumber(x)
case JsonFloat(x) => json.writeNumber(x)
case JsonDecimal(x) => json.writeString(x)
case JsonBiggerDecimal(x) => json.writeString(x.toString)
case JsonBigDecimal(x) =>
// Workaround #3784: Same behaviour as if JsonGenerator were
// configured with WRITE_BIGDECIMAL_AS_PLAIN, but forced as this
// configuration is ignored when called from ObjectMapper.valueToTree
val raw = x.bigDecimal.stripTrailingZeros.toPlainString
if (raw contains ".") json.writeTree(new DecimalNode(new JBigDecimal(raw)))
else json.writeTree(new BigIntegerNode(new BigInteger(raw)))
}
case Json.JString(v) => json.writeString(v)
case Json.JBoolean(v) => json.writeBoolean(v)
case Json.JArray(elements) => {
json.writeStartArray()
elements.foreach { t => serialize(t, json, provider) }
json.writeEndArray()
}
case Json.JObject(values) => {
json.writeStartObject()
values.toList.foreach { t =>
json.writeFieldName(t._1)
serialize(t._2, json, provider)
}
json.writeEndObject()
}
case Json.JNull => json.writeNull()
}
}
示例2: Channel
//设置package包名称以及导入依赖的类
package com.flipkart.connekt.commons.entities
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer, JsonSerializer, SerializerProvider}
object Channel extends Enumeration {
type Channel = Value
val PUSH = Value("push")
val EMAIL = Value("email")
val SMS = Value("sms")
val CARDS = Value("cards")
val OPENWEB = Value("openweb")
}
class ChannelToStringSerializer extends JsonSerializer[Channel.Value] {
override def serialize(t: Channel.Value, jsonGenerator: JsonGenerator, serializerProvider: SerializerProvider) = {
jsonGenerator.writeObject(t.toString)
}
}
class ChannelToStringDeserializer extends JsonDeserializer[Channel.Value] {
@Override
override def deserialize(parser:JsonParser, context:DeserializationContext):Channel.Value={
try {
com.flipkart.connekt.commons.entities.Channel.withName(parser.getValueAsString.toLowerCase)
} catch {
case e: NoSuchElementException =>
null
}
}
}
示例3: SyncType
//设置package包名称以及导入依赖的类
package com.flipkart.connekt.commons.sync
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer, JsonSerializer, SerializerProvider}
object SyncType extends Enumeration {
type SyncType = Value
val CLIENT_ADD,TEMPLATE_CHANGE, AUTH_CHANGE, STENCIL_CHANGE, STENCIL_FABRIC_CHANGE,
STENCIL_COMPONENTS_UPDATE, CLIENT_QUEUE_CREATE, SUBSCRIPTION, USER_PROJECT_CONFIG_CHANGE = Value
}
class SyncTypeToStringSerializer extends JsonSerializer[SyncType.Value] {
override def serialize(t: SyncType.Value, jsonGenerator: JsonGenerator, serializerProvider: SerializerProvider) = {
jsonGenerator.writeObject(t.toString)
}
}
class SyncTypeToStringDeserializer extends JsonDeserializer[SyncType.Value] {
@Override
override def deserialize(parser: JsonParser, context: DeserializationContext): SyncType.Value = {
try {
SyncType.withName(parser.getValueAsString.toUpperCase)
} catch {
case e: NoSuchElementException =>
null
}
}
}
示例4: SortedObjectSerializer
//设置package包名称以及导入依赖的类
package io.mediachain.util
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.Module.SetupContext
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.databind.ser.Serializers
import org.json4s._
import org.json4s.jackson.Json4sScalaModule
class SortedObjectSerializer extends JsonSerializer[JValue]{
def serialize(value: JValue, json: JsonGenerator, provider: SerializerProvider) {
if (value == null) {
json.writeNull()
} else {
value match {
case JInt(v) => json.writeNumber(v.bigInteger)
case JLong(v) => json.writeNumber(v)
case JDouble(v) => json.writeNumber(v)
case JDecimal(v) => json.writeNumber(v.bigDecimal)
case JString(v) => json.writeString(v)
case JBool(v) => json.writeBoolean(v)
case JArray(elements) =>
json.writeStartArray()
elements filterNot (_ == JNothing) foreach (x => serialize(x, json, provider))
json.writeEndArray()
case JObject(fields) => {
json.writeStartObject()
fields sortWith { (f1, f2) =>
f1._1.compareTo(f2._1) < 0
} filterNot (_._2 == JNothing) foreach {
case (n, v) =>
json.writeFieldName(n)
serialize(v, json, provider)
}
json.writeEndObject()
}
case JNull => json.writeNull()
case JNothing => ()
}
}
}
override def isEmpty(value: JValue): Boolean = value == JNothing
}
private object SortedObjectSerializerResolver extends Serializers.Base {
private val JVALUE = classOf[JValue]
override def findSerializer(config: SerializationConfig, theType: JavaType, beanDesc: BeanDescription) = {
if (!JVALUE.isAssignableFrom(theType.getRawClass)) null
else new SortedObjectSerializer
}
}
class Json4sWithSortedObjectsScalaModule extends Json4sScalaModule {
override def setupModule(ctxt: SetupContext) {
super.setupModule(ctxt)
ctxt.addSerializers(SortedObjectSerializerResolver)
}
}
示例5: Json
//设置package包名称以及导入依赖的类
package com.amarjanica.discourse.util
import java.time._
import com.fasterxml.jackson.core.{JsonParser, JsonGenerator}
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.databind.DeserializationFeature._
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
object Json {
private val jtstampSerializer = new JsonSerializer[java.time.OffsetDateTime] {
override def serialize(value: java.time.OffsetDateTime, generator: JsonGenerator, x: SerializerProvider) =
generator.writeString(value.toString())
}
private val jtstampDeserializer = new JsonDeserializer[java.time.OffsetDateTime] {
override def deserialize(parser: JsonParser, context: DeserializationContext) =
OffsetDateTime.parse(parser.getValueAsString)
}
val timeModule = new SimpleModule()
.addSerializer(classOf[java.time.OffsetDateTime], jtstampSerializer)
.addDeserializer(classOf[java.time.OffsetDateTime], jtstampDeserializer)
val mapper = {
val o = new ObjectMapper()
o.registerModule(DefaultScalaModule)
o.registerModule(timeModule)
o.configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
o
}
}
示例6: LocalDateSerializer
//设置package包名称以及导入依赖的类
package com.buysomegames.kernel
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.{JsonSerializer, SerializerProvider}
import com.twitter.finatra.json.modules.FinatraJacksonModule
import org.bson.types.ObjectId
private class LocalDateSerializer extends JsonSerializer[LocalDate] {
override def serialize(value: LocalDate, gen: JsonGenerator, serializers: SerializerProvider): Unit = {
gen.writeString(value.format(DateTimeFormatter.ISO_DATE))
}
}
private class ObjectIdSerializer extends JsonSerializer[ObjectId] {
override def serialize(value: ObjectId, gen: JsonGenerator, serializers: SerializerProvider): Unit = {
gen.writeString(value.toHexString)
}
}
object BuysomegamesJacksonModule extends FinatraJacksonModule {
override val additionalJacksonModules = Seq(
new SimpleModule {
addSerializer(classOf[LocalDate], new LocalDateSerializer)
},
new SimpleModule {
addSerializer(classOf[ObjectId], new ObjectIdSerializer)
}
)
}
示例7: DtabCodec
//设置package包名称以及导入依赖的类
package io.buoyant.namerd
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer, JsonSerializer, SerializerProvider}
import com.fasterxml.jackson.databind.module.SimpleModule
import com.twitter.finagle.{Dentry, NameTree, Path}
object DtabCodec {
def module = {
val module = new SimpleModule
module.addSerializer(classOf[Path], new JsonSerializer[Path] {
override def serialize(path: Path, json: JsonGenerator, p: SerializerProvider): Unit =
json.writeString(path.show)
})
module.addDeserializer(classOf[Path], new JsonDeserializer[Path] {
override def deserialize(json: JsonParser, ctx: DeserializationContext) =
Path.read(json.getValueAsString)
})
module.addSerializer(classOf[Dentry.Prefix], new JsonSerializer[Dentry.Prefix] {
override def serialize(pfx: Dentry.Prefix, json: JsonGenerator, p: SerializerProvider) {
json.writeString(pfx.show)
}
})
module.addDeserializer(classOf[Dentry.Prefix], new JsonDeserializer[Dentry.Prefix] {
override def deserialize(json: JsonParser, ctx: DeserializationContext) =
Dentry.Prefix.read(json.getValueAsString)
})
module.addSerializer(classOf[NameTree[Path]], new JsonSerializer[NameTree[Path]] {
override def serialize(
nameTree: NameTree[Path],
json: JsonGenerator,
p: SerializerProvider
): Unit = json.writeString(nameTree.show)
})
module.addDeserializer(classOf[NameTree[Path]], new JsonDeserializer[NameTree[Path]] {
override def deserialize(json: JsonParser, ctx: DeserializationContext) =
NameTree.read(json.getValueAsString)
})
module
}
}
示例8: factory
//设置package包名称以及导入依赖的类
package io.buoyant.config.types
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{DeserializationContext, SerializerProvider}
import com.twitter.finagle.thrift.Protocols
import io.buoyant.config.{ConfigDeserializer, ConfigSerializer}
import org.apache.thrift.protocol.{TCompactProtocol, TProtocolFactory}
sealed trait ThriftProtocol {
def factory: TProtocolFactory
def name: String
}
object ThriftProtocol {
object Binary extends ThriftProtocol {
def factory = Protocols.binaryFactory()
val name = "binary"
}
object Compact extends ThriftProtocol {
def factory = new TCompactProtocol.Factory
val name = "compact"
}
}
class ThriftProtocolDeserializer extends ConfigDeserializer[ThriftProtocol] {
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): ThriftProtocol =
catchMappingException(ctxt) {
_parseString(jp, ctxt) match {
case "binary" => ThriftProtocol.Binary
case "compact" => ThriftProtocol.Compact
case protocol =>
throw new IllegalArgumentException(s"unsupported thrift protocol $protocol")
}
}
}
class ThriftProtocolSerializer extends ConfigSerializer[ThriftProtocol] {
override def serialize(
value: ThriftProtocol,
jgen: JsonGenerator,
provider: SerializerProvider
): Unit = jgen.writeString(value.name)
}
示例9: PathDeserializer
//设置package包名称以及导入依赖的类
package io.buoyant.config.types
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{SerializerProvider, DeserializationContext}
import com.twitter.finagle.Path
import io.buoyant.config.{ConfigSerializer, ConfigDeserializer}
class PathDeserializer extends ConfigDeserializer[Path] {
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): Path =
catchMappingException(ctxt) {
Path.read(_parseString(jp, ctxt))
}
}
class PathSerializer extends ConfigSerializer[Path] {
override def serialize(
value: Path,
jgen: JsonGenerator,
provider: SerializerProvider
): Unit = jgen.writeString(value.show)
}
示例10: InetAddressDeserializer
//设置package包名称以及导入依赖的类
package io.buoyant.config.types
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{SerializerProvider, DeserializationContext}
import com.google.common.net.InetAddresses
import io.buoyant.config.{ConfigSerializer, ConfigDeserializer}
import java.net.InetAddress
class InetAddressDeserializer extends ConfigDeserializer[InetAddress] {
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): InetAddress =
catchMappingException(ctxt) {
InetAddresses.forString(_parseString(jp, ctxt))
}
}
class InetAddressSerializer extends ConfigSerializer[InetAddress] {
override def serialize(
value: InetAddress,
jgen: JsonGenerator,
provider: SerializerProvider
): Unit = jgen.writeString(value.getHostName)
}
示例11: Directory
//设置package包名称以及导入依赖的类
package io.buoyant.config.types
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{SerializerProvider, DeserializationContext}
import io.buoyant.config.{ConfigSerializer, ConfigDeserializer}
import java.nio.file.Paths
case class Directory(path: java.nio.file.Path) {
require(path.toFile.isDirectory, s"$path is not a directory")
}
class DirectoryDeserializer extends ConfigDeserializer[Directory] {
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): Directory =
catchMappingException(ctxt) {
Directory(Paths.get(_parseString(jp, ctxt)))
}
}
class DirectorySerializer extends ConfigSerializer[Directory] {
override def serialize(
value: Directory,
jgen: JsonGenerator,
provider: SerializerProvider
): Unit = jgen.writeString(value.path.toString)
}
示例12: Port
//设置package包名称以及导入依赖的类
package io.buoyant.config.types
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{SerializerProvider, DeserializationContext}
import io.buoyant.config.{ConfigSerializer, ConfigDeserializer}
case class Port(port: Int) {
val MinValue = 0
val MaxValue = math.pow(2, 16) - 1
require((MinValue <= port) && (port <= MaxValue), s"$port outside valid range for ports")
}
class PortDeserializer extends ConfigDeserializer[Port] {
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): Port =
catchMappingException(ctxt) {
Port(_parseInteger(jp, ctxt))
}
}
class PortSerializer extends ConfigSerializer[Port] {
override def serialize(
value: Port,
jgen: JsonGenerator,
provider: SerializerProvider
): Unit = jgen.writeNumber(value.port)
}
示例13: LogLevelDeserializer
//设置package包名称以及导入依赖的类
package io.buoyant.config.types
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{SerializerProvider, DeserializationContext}
import com.twitter.logging.Level
import io.buoyant.config.{ConfigSerializer, ConfigDeserializer}
class LogLevelDeserializer extends ConfigDeserializer[Level] {
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): Level =
catchMappingException(ctxt) {
val lname = _parseString(jp, ctxt)
Level.parse(lname.toUpperCase).getOrElse {
throw new IllegalArgumentException(s"Illegal log level: $lname")
}
}
}
class LogLevelSerializer extends ConfigSerializer[Level] {
override def serialize(
level: Level,
jgen: JsonGenerator,
provider: SerializerProvider
): Unit = jgen.writeString(level.name)
}
示例14: DtabDeserializer
//设置package包名称以及导入依赖的类
package io.buoyant.config.types
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{SerializerProvider, DeserializationContext}
import com.twitter.finagle.Dtab
import io.buoyant.config.{ConfigSerializer, ConfigDeserializer}
class DtabDeserializer extends ConfigDeserializer[Dtab] {
override def deserialize(parser: JsonParser, ctx: DeserializationContext): Dtab =
catchMappingException(ctx) {
Dtab.read(_parseString(parser, ctx))
}
}
class DtabSerializer extends ConfigSerializer[Dtab] {
override def serialize(
value: Dtab,
jgen: JsonGenerator,
provider: SerializerProvider
): Unit = jgen.writeString(value.show)
}
示例15: File
//设置package包名称以及导入依赖的类
package io.buoyant.config.types
import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{SerializerProvider, DeserializationContext}
import io.buoyant.config.{ConfigSerializer, ConfigDeserializer}
import java.nio.file.Paths
case class File(path: java.nio.file.Path) {
require(path.toFile.isFile, s"$path is not a file")
}
class FileDeserializer extends ConfigDeserializer[File] {
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): File =
catchMappingException(ctxt) {
File(Paths.get(_parseString(jp, ctxt)))
}
}
class FileSerializer extends ConfigSerializer[File] {
override def serialize(
value: File,
jgen: JsonGenerator,
provider: SerializerProvider
): Unit = jgen.writeString(value.path.toString)
}