wg / jacks Goto Github PK
View Code? Open in Web Editor NEWJackson module for Scala
License: Apache License 2.0
Jackson module for Scala
License: Apache License 2.0
The ScalaTypeSig object has no mapping for the "Scala.Any" type.
For instance when a Map[String,Any] is used in a case class, an exception is thrown during deserizalization:
Exception in thread "main" java.lang.ClassNotFoundException: scala.Any
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.lambdaworks.jacks.ScalaTypeSig$.findClass(module.scala:326)
I think the following mapping should be added:
"scala.Any" -> classOf[Any],
The scala builtin @transient annotation seems to be ignored.
@JsonIgnore does the job but the standard @transient annotation should produce a similar result isn't it ?
The serialization of case classes includes only fields declared in the primary constructor
However it is sometimes interesting to add some extra fields (that could be used by the JsonCreator in the deserialization phase) in the resulting JSON string.
One solution to do that with Jackson is to add the @JsonProperty annotation on the method one want to serialiaze the result (see http://stackoverflow.com/questions/14362247/jackson-adding-extra-fields-to-an-object-in-serialization).
But it seems it is not supported by the current implementation of Jacks.
case class user(firstName:String,lastName:String,@JsonProperty("jsonclass") jsonclass:String)
this should serialize the final field as jsonclass but actually serializes as jsonclass. the serializer should respect annotations, even for case classes.
Jacks 2.1.4, 2.1.2, reproducible with both Scala 2.9.2 and 2.10
scala> case class MySchema(bigBucketSecs: Int, groupingField: String, timeWindowIndexes: String)
defined class MySchema
scala> JacksMapper.writeValueAsString(MySchema(864,"guid","300"))
res5: java.lang.String = {}
scala> JacksMapper.readValue[MySchema]("{}")
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class MySchema]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.StringReader@66c17c; line: 1, column: 2]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:400)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:289)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2793)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1957)
However, the unit tests in Jacks themselves clearly run, and it seems the serialization works for classes actually defined in code, which I bring into the REPL via "sbt console".
Strange.
Result of this code is empty.
import com.lambdaworks.jacks.JacksMapper
class First (val num: Int, val str: String) {}
class Second (val firstClass: First, val num: Int, val str: String) {}
class Third (val firstClass: First, val secondClass: Second, val num: Int, val str: String) {}
object Program extends App{
val first = new First(1, "first")
val second = new Second(first, 11, "second")
val third = new Third(first, second, 111, "third")
println(JacksMapper.writeValueAsString(third))
}
Is there a way to automatically "snake case" field names as the @JsonSnakeCase annotation does in the jerkson library ?
Could you please compare Jacks with Jerkson and Jackson Scala module?
https://github.com/codahale/jerkson
https://github.com/FasterXML/jackson-module-scala
I guess there must be a motivation why you create Jackson.
I know Jacks from this page:
http://wiki.fasterxml.com/JacksonModuleScala
Sorry I don't know a better place to post this discussion.
(My apologies if this is just a simple mistake on my part as I am new to scala)
I checkout and build jacks and added two of my own classes into the file test.scala:
abstract class Message
case class CardDrawn(player: Long, card: Int, mType: String = "CardDrawn") extends Message
case class CardSet(cards: List[CardDrawn], mType: String = "CardSet") extends Message
And then added in a test to check that they roundtrip fine.
test("cardset works"){
rw(CardSet(List(CardDrawn(1L,2),CardDrawn(3L,4)))) should equal (CardSet(List(CardDrawn(1L,2),CardDrawn(3L,4))))
val cs = CardSet(List(CardDrawn(1L,2),CardDrawn(3L,4)))
val json = write(cs)
val obj = read(json)
cs should equal (obj)
}
The firs call to "rw()" works suggesting that all is very well indeed. The following lines where I try to explicitly to the read and write (the same operations as the rw call) gets an exception:
Can not instantiate abstract type [simple type, class scala.runtime.Nothing$] (need to add/enable type information?)
at [Source: java.io.StringReader@76d26a; line: 1, column: 2]
com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate abstract type [simple type, class scala.runtime.Nothing$] (need to add/enable type information?)
at [Source: java.io.StringReader@76d26a; line: 1, column: 2]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:77)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2048)
at com.lambdaworks.jacks.JacksMapper$class.readValue(jacks.scala:20)
at com.lambdaworks.jacks.JacksMapper$.readValue(jacks.scala:40)
at com.lambdaworks.jacks.JacksTestSuite$class.read(test.scala:287)
at com.lambdaworks.jacks.CaseClassSuite.read(test.scala:91)
Whats the correct way to roundtrip such a structured class and is there something wrong with the rw method which is masking an issue?
Case classes with companion objects are sometimes not recognized by Jacks. It seems that the problem is in ScalaTypeSig.cls
computation โ it selects the first available ClassSymbol
, but the first one is something synthetic generated by a companion object. Here is a list of class symbols I have:
[info] ClassSymbol(CaseClass, owner=com.my.package, flags=400, info=25 ,None)
[info] ClassSymbol(CaseClass, owner=com.my.package, flags=40, info=43 ,None)
I use scala 2.10.4, jacks 2.3.3.
It turned out that ScalaSig parsing takes too long time, and because of per-serializer context creation class is being parsed each time message processed. Is there any reasons for not caching ScalaSig results?
I have case class like this:
case class Parameter(val name:String, val value:String, @(JsonProperty@field)("type") val aType:String, val restriction:String, val defaultValue:String, val required:Boolean, val description:String)
The output Json should have a 'type' field which is a keyword in Scala. hence, I use @JsonProperty to convert it to 'type' field.
Running this line of code:
val p = Parameter("name", "value", "string", "restricted", "myDefault", true, "desc")
println(JacksMapper.writeValueAsString(p))
prints:
{"name":"name","value":"value","aType":"string","restriction":"restricted","defaultValue":"myDefault","required":true,"description":"desc"}
Looks like the @JsonProperty is ignored.
Is this a bug?
Trying to convert a simple case class structure to Json:
jsonResponse: String = writeValueAsString(ListView(myList))
I get a ClassNotFoundException model.Item a class in the model package
Perhaps it's similar to the issue mentioned here, the exception seems the same. They fixed it by using a more dynamic way of class loading (I think)
sorm/sorm#10
In its current state the library is unusable in Play 2.1 for me.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.