Giter Club home page Giter Club logo

Comments (13)

rktoomey avatar rktoomey commented on July 25, 2024

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.

pakaufmann avatar pakaufmann commented on July 25, 2024

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.

rktoomey avatar rktoomey commented on July 25, 2024

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.

rktoomey avatar rktoomey commented on July 25, 2024

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.

rktoomey avatar rktoomey commented on July 25, 2024

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.

rktoomey avatar rktoomey commented on July 25, 2024

OK, I couldn't find any bug with @Ignore annotation. Here are some specs demonstrating expected behaviour:

novus@51416ef

I think if you revise your model to be more idiomatic to Scala, you will find @Ignore works as expected.

from salat.

pakaufmann avatar pakaufmann commented on July 25, 2024

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.

pakaufmann avatar pakaufmann commented on July 25, 2024

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.

rktoomey avatar rktoomey commented on July 25, 2024

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.

rktoomey avatar rktoomey commented on July 25, 2024

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.

pakaufmann avatar pakaufmann commented on July 25, 2024

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.

rktoomey avatar rktoomey commented on July 25, 2024

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.

pakaufmann avatar pakaufmann commented on July 25, 2024

Ok, good to know.
Thanks for all the help.

from salat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.