本文整理汇总了Scala中com.fasterxml.jackson.core.JsonToken类的典型用法代码示例。如果您正苦于以下问题:Scala JsonToken类的具体用法?Scala JsonToken怎么用?Scala JsonToken使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JsonToken类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: DplaJsonTraversable
//设置package包名称以及导入依赖的类
package la.dp.sitemap
import java.io.InputStream
import com.fasterxml.jackson.core.JsonToken
import com.fasterxml.jackson.core.JsonToken._
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
class DplaJsonTraversable(inputStream: InputStream) extends Traversable[JsonNode] {
override def hasDefiniteSize = false
override def foreach[U](f: (JsonNode) => U): Unit = {
try {
val parser = DplaJsonTraversable.jsonFactory.createParser(inputStream)
assume(parser.nextToken() == JsonToken.START_ARRAY)
while (parser.nextToken() != END_ARRAY) {
if (parser.currentToken() == START_OBJECT) {
val node = parser.readValueAsTree[JsonNode]
f(node)
}
}
} finally {
inputStream.close()
}
}
override def toString(): String = "DplaJsonTraversable: " + inputStream.toString
}
object DplaJsonTraversable {
private val objectMapper = new ObjectMapper()
private val jsonFactory = objectMapper.getFactory
}
示例2: DplaJsonTraversableTest
//设置package包名称以及导入依赖的类
package la.dp.sitemap
import com.fasterxml.jackson.core.JsonToken
class DplaJsonTraversableTest extends SitemapSpec {
"A DplaJsonTraversable" should "find all the objects in a DPLA JSON file" in {
assert(new DplaJsonTraversable(someDotJson).size == 49)
}
it should "present the caller with a stream of JSON objects containing DPLA metadata" in {
new DplaJsonTraversable(someDotJson).foreach(
obj => {
assert(obj.asToken() == JsonToken.START_OBJECT)
List(obj.get("_id"), obj.get("_type"), obj.get("_index")).foreach( value => {
assert(value.asToken() != JsonToken.VALUE_NULL)
})
})
}
}
示例3: CustomBigDecimalDeserialiserModule
//设置package包名称以及导入依赖的类
package cjp.catalogue.conf
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.core.{JsonToken, JsonParseException, JsonParser}
import com.fasterxml.jackson.databind.{DeserializationContext}
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer
import scala.util.{Failure, Success, Try}
class CustomBigDecimalDeserialiserModule extends SimpleModule("CustomBigDecimalDeserialiserModule") {
addDeserializer(classOf[BigDecimal], BigDecimalDeserializer)
}
object BigDecimalDeserializer extends StdScalarDeserializer[BigDecimal](classOf[BigDecimal]) {
override def deserialize(jsonParser: JsonParser, context: DeserializationContext): BigDecimal = {
Option(jsonParser.getValueAsString) match {
case Some(str: String) => Try(BigDecimal(str)) match {
case Success(value) => value
case Failure(error) => throw new JsonParseException(s"Unable to parse ${str} to BigDecimal", jsonParser.getCurrentLocation, error)
}
case None if jsonParser.getCurrentToken == JsonToken.VALUE_NULL => null.asInstanceOf[BigDecimal]
case _ => throw new JsonParseException(s"Expected a string token representing type 'BigDecimal', found: ${jsonParser.getCurrentToken}", jsonParser.getCurrentLocation)
}
}
}
示例4: JacksonParserAdapter
//设置package包名称以及导入依赖的类
package com.hypertino.binders.json
import com.fasterxml.jackson.core.JsonToken
import com.hypertino.binders.core.BindOptions
import com.hypertino.binders.json.api._
class JacksonParserAdapter(jacksonParser: com.fasterxml.jackson.core.JsonParser)
(implicit protected val bindOptions: BindOptions) extends JsonParserApi {
override def currentToken: JsToken = convertToken(jacksonParser.getCurrentToken)
override def nextToken(): JsToken = convertToken(jacksonParser.nextToken())
override def fieldName: Option[String] = Option(jacksonParser.getCurrentName)
override def stringValue: String = jacksonParser.getValueAsString
override def numberValue: BigDecimal = jacksonParser.getDecimalValue
private def convertToken(token: JsonToken): JsToken = {
token match {
case JsonToken.START_OBJECT ? JsStartObject
case JsonToken.END_OBJECT ? JsEndObject
case JsonToken.START_ARRAY ? JsStartArray
case JsonToken.END_ARRAY ? JsEndArray
case JsonToken.FIELD_NAME ? JsFieldName
case JsonToken.VALUE_STRING ? JsString
case JsonToken.VALUE_NUMBER_FLOAT ? JsNumber
case JsonToken.VALUE_NUMBER_INT ? JsNumber
case JsonToken.VALUE_TRUE ? JsTrue
case JsonToken.VALUE_FALSE ? JsFalse
case JsonToken.VALUE_NULL ? JsNull
case _ ? JsUnknown
}
}
override def location: String = jacksonParser.getCurrentLocation.toString
}