score:-1
my problem was a race condition due to not using chaining in my mapper configuration singleton.
my old code was more like this:
private var mapper: objectmapper with scalaobjectmapper = _
def getmapper: objectmapper with scalaobjectmapper = {
if (mapper == null) {
mapper = new objectmapper() with scalaobjectmapper
mapper.registermodule(defaultscalamodule)
mapper.disable(deserializationfeature.fail_on_unknown_properties)
}
mapper
}
as you can see, if one thread initializes the mapper, but hasn't yet disabled unknown properties failure, a second thread could return and use a mapper that hasn't had that flag set yet, which explains why i was seeing the error only some of the time.
the correct code uses chaining so that the mapper singleton is set with all of the configuration:
private var mapper: objectmapper = _
def getmapper: objectmapper = {
if (mapper == null) {
mapper = new objectmapper()
.registermodule(defaultscalamodule)
.disable(deserializationfeature.fail_on_unknown_properties)
}
mapper
}
(i also removed the experimental scalaobjectmapper
mix-in.)
score:-1
try jsoniter-scala and you will enjoy how it can be handy, safely, and efficient to parse and serialize json these days with scala: https://github.com/plokhotnyuk/jsoniter-scala
one of it's crucial features is an ability to generate codecs in compile time and even print their sources. you will have no any runtime magic like reflection or byte code replacement that will affect your code.
score:0
i made one simple test like this:
case class testclass (counts: mutable.hashmap[string, long])
and i converted it like:
val objectmapper = new objectmapper() with scalaobjectmapper
objectmapper.registermodule(defaultscalamodule)
val r3 = objectmapper.readvalue("{\"counts\":{\"foo\":1,\"bar\":2}}", classof[testclass])
and apparently it works for me. maybe it's something about the version you're using of jackson, or scala. have you tried different versions of jackson for example?
Source: stackoverflow.com
Related Query
- Deserialize Scala map field from JSON using Jackson
- Extracting a field from a Json string using jackson mapper in Scala
- How can I deserialize from JSON with Scala using *non-case* classes?
- Unable to deserialize json array to class object using jackson fasterxml in scala
- Deserialize json field from a complex object in scala
- NoSuchMethodError while trying to generate JSON strings from a Scala Map using Lift-JSON
- Creating JSON from mapFields in Scala using Jackson library
- What is the proper way to remove elements from a scala mutable map using a predicate
- JSON serialization of Scala enums using Jackson
- How to read json data using scala from kafka topic in apache spark
- Re-using A Schema from JSON within a Spark DataFrame using Scala
- Scala - How to convert from List of tuples of type (A,B) to type (B,A) using map
- How to select a object field for map value in Scala using groupby
- Serialize List[Any] to/from Json in Scala using Jackson
- Get a specific parameter from a json string using JsonPath in scala
- Retrieving Key From Map In Scala Using A Value
- Scala Collections: Using a value of Set find the key from a Map object.
- Using Scala Jackson for JSON deserialization?
- access scala map from dataframe without using UDFs
- Extracting string from JSON using Json4s using Scala
- Fetch all values irrespective of keys from a column of JSON type in a Spark dataframe using Spark with scala
- More idiomatic way of reading JSON file and creating a map from it in Scala
- How to ignore a field from serializing when using circe in scala
- How to update each field in a list using Play json in Scala
- How to drop multiple columns from JSON body using scala
- Get type of primitive field from an object using Scala reflection
- JSON decode nested field as Map[String, String] in Scala using circe
- Rendering JSON from parsed JSON using lift-json in Scala
- How can I automatically map JSON to a case class when a field is a scala keyword?
- Write a map with key as int to json in scala using json4s
More Query from same tag
- Scala FunSpec tests describe() initialization and execution order
- String passed to Class appear null
- sbt (Scala) via SSH results in command not found, but works if I do it myself
- Track and log/debug yarn apllication which have been started from a scala binary using spark-submit
- Spark read from Teradata view which has column with Title
- Access type parameters of generic parameter scala
- why scala puts the version on stderr instead
- Scala: import not required to use immutable.Map?
- For until square root
- How to count characters of a String?
- Play Framework as SBT Non-Root Module
- Java, Akka Actor and Bounded Mail Box
- Unpack the meaning of this lift function
- scala (spark) zio convert future to zio
- Apache DirectParquetOutputCommitter vs Netflix S3PartitionedOutputCommitter
- While loops generated via Scala Macros are not equal
- What is the difference between Exception and Throwable classes?
- How do parse fixed-position file with multiple sections in Spark using Scala
- Filtering futures using values in another future
- Convert GenericRecord to DF
- Play Framework: clear Flash scope when going back
- functional style to multiply two lists with Scala
- Implement a simple chat server with akka and scala
- Spark-Scala: Filtering Spark Dataset based on multiple column and conditions
- Can't capture a group in regex
- Are Scala functions that run on an Spark Array parallelized?
- Iterate sequence of future in sequence of futures
- jOOQ: how can I view a field which has been aliased?
- Scala recursive closure compile error
- Effect of Scala class definitions on perm gen space