Comments (13)
Could you please provide:
- a full stracktrace
- a small sample project which demonstrates the error?
I'd be happy to fork and fix the project or identify a bug with Salat, but the model objects you have provided do not compile.
from salat.
Here the full code which will reproduce the error. It will fail on the line "println(im.toString)".
object Main {
def main(args:Array[String]) = {
val importFile = new ConcreteClass(new File("."))
val in = grater[ConcreteClass].asDBObject(importFile)
println(in.toString)
}
}
abstract class AbstractClass(@Key("_id")val id:ObjectId = new ObjectId, var text:String) {
def this() = this(text = "")
}
case class ConcreteClass(@Ignore var file: File = null) extends AbstractClass
The stack trace is:
Exception in thread "main" java.lang.RuntimeException: json can't serialize type : class java.io.File
at com.mongodb.util.JSON.serialize(JSON.java:222)
at com.mongodb.util.JSON.serialize(JSON.java:111)
at com.mongodb.util.JSON.serialize(JSON.java:28)
at com.mongodb.BasicDBObject.toString(BasicDBObject.java:84)
at ch.salatTest.Main$.main(Main.scala:16)
at ch.salatTest.Main.main(Main.scala)
from salat.
This model setup is rather confusing: the concrete instance can never supply a value for "text", and the abstract superclass has multiple constructors, of which the default supplies no default argument for "text".
It does compile, but even if it serialized your next problem would be that the multiple construction/var setup is not compatible with Salat's use of the constructor on ConcreteClass when deserializing: the ObjectId would always be lost and text would never have a value.
from salat.
Suggestion for a more idiomatic concrete case class:
case class ConcreteClass(@Key("_id") val id: ObjectId = new ObjectId,
text: Option[String] = None,
@Ignore file: File = new java.io.File("."))
from salat.
Thanks, looks like there might be a problem with @Ignore
here, working to track it down.
But if you were to replace java.io.File here with String, I suspect it still wouldn't deserialize as expected due to the constructor issuer.
from salat.
OK, I couldn't find any bug with @Ignore
annotation. Here are some specs demonstrating expected behaviour:
I think if you revise your model to be more idiomatic to Scala, you will find @Ignore
works as expected.
from salat.
Hm, I can't solve it with your tips.
I tried to serialize the following class and it failed:
case class Test(@Ignore file: File)
val t = new Test(new File("."))
val in = grater[Test].asDBObject(t)
println(in.toString)
Could it be that I use Scala 2.9.0-1 and salat 0.0.8.
from salat.
Another update on the issue.
If I serialize the following class:
case class Test2(test: String, @Ignore test2: String)
I get back this:
{ "_typeHint" : "ch.salatTest.Test2" , "test" : "test" , "test2" : "test2"}
So it definitely ignores the @ignore statement in my case.
As said before, my setup is scala-library-2.9.0-1 and salat 0.0.8-SNAPSHOT.
from salat.
Patrick, I am so sorry you have continued to have issues with @ignore.
So I have created a project that shows an instance of your Test2 class being serialized correctly, missing the "test2" field:
https://github.com/rktoomey/salat-ignore-demo
Please clone the project, run your sbt script and type in "run" at the command line. Your output should match the output in the README.
Closely examine your imports to make sure they are matching mine: the only factor I can still imagine causing you a problem is that somehow you are importing the raw annotation and it is not being properly applied to the getter.
Best,
Rose
from salat.
Ah, just saw you are using 2.9.0-1 - is there any reason you aren't using 2.9.1? The salat snapshot for 2.9.0-1 will not be maintained any longer now that Salat 2.9.1 is mainstream.
from salat.
Ok, I solved it, but I'm not really sure why it only works that way.
After moving to 2.9.1 it didn't work. I checked everything and had the same includes as your testproject (which functioned correctly btw.)
So i checked my generated classfile with scalap against your generated classfile and noticed that my the @ignore-Annotation got lost when I compiled.
So I checked line for line what was different and notices, that I included the ignore-Statement like this:
import com.novus.salat.annotations.raw.Ignore
and you include it with:
import com.novus.salat.annotations._
After I changed my import to yours, everything worked and the annotation was loaded correctly into the compiled file.
from salat.
Yes, that is a scala thing - the annotation needs to be applied to the getter. Importing the raw annotation does not apply it to the getter. You actually can do this with the raw import using this syntax:
case class Test2(test: String, @(Ignore @getter) test2 = "test2")
from salat.
Ok, good to know.
Thanks for all the help.
from salat.
Related Issues (20)
- Custom TypeHint strategy for JSON HOT 2
- Unable to serialize case class hierarchy HOT 3
- mongo connection leak - re-use one mongo connection for each test DAO to reduce connection footprint. HOT 2
- Update Casbah to 2.8.2 in Salat 1.10.x to support connecting to Mongo 3.x server. HOT 16
- Play Salat HOT 4
- Project status update? HOT 23
- java.lang.ClassCastException when deserializing from Salat in context of akka runtime. HOT 4
- Error with reason "Very strange!" - GRATER GLITCH HOT 2
- Upgrade to sbt 0.13.12
- Release Salat 1.10 HOT 1
- JSON data can deserialize into collections containing data not matching the container generic type.
- More specific error class for Grater errors: SalatDeserializationException
- Release 1.11.x with Casbah 3.1.1 support (but not Scala 2.12) HOT 18
- Will this work with the latest Casbah 3.1.1? HOT 1
- SalatDAO support for collections HOT 7
- Scala 2.12 HOT 8
- Optional field in MongoDBObject with null value causing MatchError HOT 11
- Custom Transformers never get applied to case classes HOT 9
- Case class not serialising in order HOT 3
- Support for Mongo 3.6+ HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from salat.