mixed Java objects and scala objects for JSON serialization

869 views
Skip to first unread message

Lei Tang

unread,
Sep 3, 2014, 6:19:57 PM9/3/14
to scala...@googlegroups.com
Hi,

My codebase is a mixed version of Java and Scala classes.  In particular, there is one Java class containing both Java and Scala objects.  When I tried to use jackson to dump it into a JSON string, I countered error saying that

org.codehaus.jackson.map.JsonMappingException: No serializer found for the scala object.

I know that jackson by default just serializes Java objects.  Is there anything I can do to seamlessly serialize both Java and Scala objects in the Java class?


To be more clear, this is the toy script:

import org.codehaus.jackson.map.ObjectMapper;

Java Class A {
   Java Class: b
   Scala Class: c

  public String dumpAsJSONString() {
     ObjectMapper mapper = new ObjectMapper();
    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
 }
}


Thanks for your help! 

Regards,
- Lei

Stephen Compall

unread,
Sep 6, 2014, 5:49:58 PM9/6/14
to scala...@googlegroups.com

On 9/3/2014 6:19 PM, Lei Tang wrote:

My codebase is a mixed version of Java and Scala classes.  In particular, there is one Java class containing both Java and Scala objects.  When I tried to use jackson to dump it into a JSON string, I countered error saying that

org.codehaus.jackson.map.JsonMappingException: No serializer found for the scala object.

I know that jackson by default just serializes Java objects.  Is there anything I can do to seamlessly serialize both Java and Scala objects in the Java class?


A "Scala object" isn't fundamentally different from a "Java object"; they're both "Java objects", just as "Scala classes" are "Java classes".  Moreover, you've done a good enough job of anonymizing your code sample that it's too hard to tell what your real underlying problem is.

Could you produce a sample that demonstrates the error and successfully compiles on http://scastie.org/ and share that here, instead?  scastie has the features you need to add Jackson and use it; then you can write an App that scastie will execute and reproduce the error.

-- 
Stephen Compall
^aCollection allSatisfy: [:each | aCondition]: less is better

signature.asc

Lei Tang

unread,
Sep 10, 2014, 12:29:33 PM9/10/14
to Stephen Compall, scala...@googlegroups.com
Hi Stephen,

Thanks for your reply. 

Actually, by using fasterXML's jackson-module-scala,
https://github.com/FasterXML/jackson-module-scala

I'm able to dump both java/scala objects into one json file.  Problem solved :-)

- Lei
Reply all
Reply to author
Forward
0 new messages