[2.1 Java] Play and JSON mapping with or without Jackson

1,458 views
Skip to first unread message

Michael Janiak

unread,
Aug 7, 2013, 1:54:37 PM8/7/13
to play-fr...@googlegroups.com
Howdy,

I'm using the Jackson ObjectMapper to map JSON like so:

MyObject foo = new ObjectMapper().readValue(request().body().asJson(), MyObject.class);


The documentation unfortunately does not sufficiently explain how to handle complex JSON requests http://www.playframework.com/documentation/2.1.3/JavaJsonRequests

Is there a way to do this now that Play is on 2.1?

Thanks in advance!

Michael

Ryan Means

unread,
Aug 7, 2013, 2:15:33 PM8/7/13
to play-fr...@googlegroups.com
James Roper implemented a change for this in the 2.2.x branch so that the ObjectMapper is cleanly reused. See his commit:  https://github.com/playframework/playframework/commit/ee0c74abaa9d90842b7ead70d9632eb9ac9e1f3d  If you look at the code committed - the core concepts are very simple. We took that concept and re-impeled it for ourselves in Play 2.1.x, the difference being that we now have our own "JsonUtil" class instead of using the baked in Play Json helper.  Since this code was unique to our project as well, we just implemented the ObjectMapper plugin portion directly in our Global handler.

We did this as a stop gap and will likely move back over to the official play Json helper after we migrate to the 2.2.x series.

Hope that helps.

Michael Janiak

unread,
Aug 8, 2013, 4:50:47 PM8/8/13
to play-fr...@googlegroups.com
Thank you!

Michael Janiak

unread,
Oct 18, 2013, 1:47:24 PM10/18/13
to play-fr...@googlegroups.com
Now that 2.2 is out, I noticed that you easily map to and from Json, which is brilliant.

JsonNode json =Json.toJson(task);
Task task = Json.fromJson(request().body().asJson(), Task.class);

But how do you map from Json to a collection like List<Task>? I used to do this

ObjectMapper mapper = new ObjectMapper();
List<Task> taskList = mapper.readValue(request().body().asJson(), mapper.getTypeFactory().constructCollectionType(ArrayList.class, Task.class));

play.libs.Json uses the ObjectMapper.treeToValue() method, which takes a JsonNode and a Class but that won't work with List.class.

Am I missing something?

Michael Janiak

unread,
Oct 18, 2013, 3:52:52 PM10/18/13
to play-fr...@googlegroups.com
Big of a hack but this works...

Create a wrapper class 

public class ListWrap<T> {
public List<Task> list;
}

Create a new Json object with with the same field as the wrapper and then map it

List<Task> list = Json.fromJson(Json.newObject().set("list", request().body().asJson()), ListWrap.class).list;

juhi jariwala

unread,
Apr 28, 2014, 2:34:27 AM4/28/14
to play-fr...@googlegroups.com
Hello Michael ,

                      My problem is similar to this only .considering Form Sample of Play 2.1 java in which I am passing all contact Information through json which contains Contact class details as well as object List of Information Class..My doubt is how bind JsonNode to particular class(Contact).Is there any direct method or I have to do it manually.

Thanks,
Juhi Jariwala
Reply all
Reply to author
Forward
0 new messages