Harshal Pandya
Harshal Pandya

Reputation: 1624

Jerkson JsonMappingException

I'm trying to serialize scala case class to JSON string using Jerkson like this:

case class Page(title: String, id: String, ls: List[(String, String, Int)])
val pageList = new mutable.ArrayBuffer[Page]()
val jsonString = Json.generate(pageList)

pageList is extremely large with several million Page objects. The call fails with this exception:

Caused by: org.codehaus.jackson.map.JsonMappingException:
[no message for java.lang.ArrayIndexOutOfBoundsException]

Upvotes: 0

Views: 422

Answers (2)

Steven Levine
Steven Levine

Reputation: 3675

You may want to consider using a Streaming solution. You can use one of the the Jackson Streaming APIs:

JsonGenerator jg = jsonFactory.createJsonGenerator(file, JsonEncoding.UTF8); // or Stream, Reader

or, you can use a TokenBuffer (which is considered best practice for some situations):

TokenBuffer buffer = new TokenBuffer();
// serialize object as JSON tokens (but don't serialize as JSON text!)
objectMapper.writeValue(buffer, myBean);

Details: Jackson Streaming Documentation

Upvotes: 1

Kristian Domagala
Kristian Domagala

Reputation: 3696

Given that you've got "several million" objects, I'm guessing you might be hitting the length limit of String. Try generating to an OutputStream, ie, Json.generate(pageList, out).

Upvotes: 0

Related Questions