Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jackson to (De)-serialize a Scala Case Class

I tested out the serialization of a Scala case class using Jackson.

DeserializeTest.java

    public static void main(String[] args) throws Exception { // being lazy to catch-all

        final ObjectMapper mapper          = new ObjectMapper();
        final ByteArrayOutputStream stream = new ByteArrayOutputStream();

        mapper.writeValue(stream, p.Foo.personInstance());

        System.out.println("result:" +  stream.toString());
    }
}

Foo.scala

object Foo {
  case class Person(name: String, age: Int, hobbies: Option[String])
  val personInstance = Person("foo", 555, Some("things"))
  val PERSON_JSON = """ { "name": "Foo", "age": 555 } """
}

When I ran the above main of the Java class, an exception was thrown:

[error] Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: 
 No serializer found for class p.Foo$Person and no properties discovered 
 to create BeanSerializer (to avoid exception, 
 disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )

How can I (de)-serialize Scala case classes?

like image 310
Kevin Meredith Avatar asked Feb 02 '15 03:02

Kevin Meredith


People also ask

What is the use of ObjectMapper class?

ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.

How do you tell Jackson to ignore a field during serialization?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

What is serialization in Jackson?

We use the Jackson library to serialize an Object such as List, Map, Java object, etc. We can serialize an Object into JSON and put it into a file. In order to serialize an object, we use more than one class and method. We create the ObjectMapper class to use the writeValue() method.

What does Jackson ObjectMapper do?

The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON. The Jackson ObjectMapper can also create JSON from Java objects.


1 Answers

Jackson is expecting your class to be a JavaBean, which means its expects the class to have a getX() and/or setX() for every property.

Option 1

You can create JavaBean classes in Scala using the annotation BeanProperty.

Example

case class Person(
   @BeanProperty val name: String, 
   @BeanProperty val age: Int, 
   @BeanProperty val hobbies: Option[String]
)

In this case a val will mean only a getter is defined. If you want setters for deserialization you defined the properties as var.

Option 2

While option 1 will work, if you really want to use Jackson there are wrappers that allow it to deal with Scala classes like FasterXML's scala module which might be a better approach. I haven't used it as I've just been using the Json library built in to play.

like image 158
Lionel Port Avatar answered Oct 17 '22 04:10

Lionel Port