Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to serialize generics in Scala #100

Closed
inmyth opened this issue Oct 21, 2018 · 12 comments
Closed

Unable to serialize generics in Scala #100

inmyth opened this issue Oct 21, 2018 · 12 comments

Comments

@inmyth
Copy link

inmyth commented Oct 21, 2018

I have been unable to serialize a generic class using GenericTest as reference. I have below a very simple class in Scala. To imitate the Java test code I don't use Option or case class.

  @CompiledJson
  class Abc[A] {
    var test: A = _
  }

  val dslJson = new DslJson[Any](new DslJson.Settings()
                .allowArrayFormat(true).includeServiceLoader())
  val writer = dslJson.newWriter
  var model = new Abc[String]
  model.test = "aaa"
  val t = new TypeDefinition[Abc[String]]() {}.`type`
  val os = new ByteArrayOutputStream
  writer.reset(os)
  val res = dslJson.serialize(writer, t, model)     
  writer.flush()

res always returns false signifying failed serialization. Perhaps you could help point out the problem ? Thank you.

@zapov
Copy link
Member

zapov commented Oct 21, 2018

Unfortunately, thats currently expected behavior.
Generics are supported only via annotation processor.
Scala version currently uses only reflection features.
Scala tooling is not really annotation processor friendly, and while technically you could run it, in practice I have not seen people do it.

Ideally Scala version will get some kind of compilation support... but community should help out with that.

@inmyth
Copy link
Author

inmyth commented Oct 21, 2018

I see. I guess at the time being, generics are only supported in Java version ?

@zapov
Copy link
Member

zapov commented Oct 21, 2018

Yes

@inmyth
Copy link
Author

inmyth commented Oct 22, 2018

So I tried recoding it in Java but it still didn't work. Finally I copied and ran GenericTest but all tests failed. I was using dsl-json-java8, version: 1.8.3 (version 1.8.4 was not yet available at Maven). This is one result I get from testSerializeAndDeserializeGeneric . Basically dslJson.serialize would still return false and the resulting json returns an empty string.

java.io.IOException: Unexpected end of JSON input

	at com.dslplatform.json.JsonReader.read(JsonReader.java:304)
	at com.dslplatform.json.JsonReader.getNextToken(JsonReader.java:747)
	at com.dslplatform.json.DslJson.deserialize(DslJson.java:1550)
	at com.mypackage.json.dsl.GenericTest.testSerializeAndDeserializeGeneric(GenericTest.java:67)

@zapov
Copy link
Member

zapov commented Oct 23, 2018

If you write your example as a test in GenericTest does it work then? If not, can you make a PR?

I suspect that you need to initialize library via new DslJson<>(Settings.withRuntime()) before it behaves as you expect.

@inmyth
Copy link
Author

inmyth commented Oct 23, 2018

I copied the file without changing anything. DslJson is initialialized like this

private final DslJson<Object> dslJson = new DslJson<>(new DslJson.Settings<>()
.allowArrayFormat(true).includeServiceLoader());

And I tried cloning the project. It failed to build with the same reason as when I did it with my own test. Basically dsl-json version 1.8.4 is not available and artifact dsl-json-integration-tests-java8-external-models doesn't exist. As the last version was pushed very recent like a week ago, maybe it has something to do with it ?

@zapov
Copy link
Member

zapov commented Oct 23, 2018

dsl-json 1.8.4 is not released, but you should be able to build it locally by going to root project and running

mvn clean install

That should build the entire project including the external-models.
What happens if you do that?

@inmyth
Copy link
Author

inmyth commented Oct 23, 2018

Still failed with error

DSL Platform JSON (core) 1.8.4 ..................... FAILURE [ 10.291 s]
java.lang.NoClassDefFoundError: org/junit/runner/notification/RunNotifier
	at java.lang.Class.getDeclaredConstructors0(Native Method)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.notification.RunNotifier
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)

though test-compile works.

@wadejensen
Copy link

wadejensen commented Dec 1, 2018

Would be very keen for generics support in Scala.

@zapov
Copy link
Member

zapov commented Dec 1, 2018

Yes, it would be nice to resolve such minor TODOs: https://github.com/ngs-doo/dsl-json/blob/master/scala/src/main/scala/com/dslplatform/json/runtime/ScalaClassAnalyzer.scala#L20

I don't have much time ATM, but I suspect it would not be complicated to add that support for reflection.

@zapov
Copy link
Member

zapov commented Dec 2, 2018

There is now basic support for generics in Scala: https://github.com/ngs-doo/dsl-json/blob/master/scala/src/test/scala/com/dslplatform/json/GenericsTest.scala

Will be included in 1.8.5

@zapov
Copy link
Member

zapov commented Jan 15, 2019

v1.8.5 released

@zapov zapov closed this as completed Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants