Nested object de-serialization in JsonRepresentation not working

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Nested object de-serialization in JsonRepresentation not working

rameshsr
I have an Order class and OrderDetail model as described below.  

class Order {
   long id;
   String description;
   Collection<OrderDetail> details;

   public Collection<OrderDetail> getDetails() {
         return details;
   }
  .... other methods
}

class OrderDetail {
    long id;
    String productId;
    String qty;

   ... methods here...
}

And I have a JSON string of one Order with nested collection of OrderDetail. And I am trying to de-serialize this json string into Order object and expecting that the Order object will now contain the OrderDetail collection also as expected. But it is not working as expected.

Here is the code snippet I have:

JacksonRepresentation<Order> jsonRepresentation = new JacksonRepresentation<Order>(representation, Order.class);
System.out.println(jsonRepresentation.getText()); // HERE IS THE CULPRIT
Order order = jsonRepresentation.getObject();  
Collection<OrderDetail> details = order.getDetails();

1) When I do jsonRepresentation.getObject(), it is throwing an exception.

 (java.io.EOFException) java.io.EOFException: No content to map to Object due to end of input

Why is that jsonRepresentation.getText(), causing the subsequent getObject to fail?

2) I have an extended Collection class called ForeignCollection, which is simply a subclass of Collection. In order to use ForeignCollection instead of Collection, what do I have to do in Restlet? I am getting the following error when I change Collection to ForeignCollection in the above two classes - Order and OrderDetail:

 (org.codehaus.jackson.map.JsonMappingException) org.codehaus.jackson.map.JsonMappingException: Can not find a deserializer for non-concrete Collection type [collection type; ForeignCollection, contains [simple type, class Order]

I am using Restlet 2.1.2.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3070147
Reply | Threaded
Open this post in threaded view
|

Re: Nested object de-serialization in JsonRepresentation not working

Thierry Boileau-4
Hello Ramesh,

1) You must remove this instruction before calling getObject() :
System.out.println(jsonRepresentation.getText()); // HERE IS THE CULPRIT

The incoming representation is an instance of InputRepresentation. It's a kind of representation that reads its content directly from the network socket.
The problem is that you cannot read a socket twice, thus this representation must be consumed (by getting the object, or printing its content) only once.

If you want to reuse the content, make it persistent by, for example storing the representation in memory (cf StringRepresentation), or storing it on the file system (cf FileRepresentation).

Your next question is: why the framework generates by default this kind of non-persistent representation?
We cannot assume to use persistent representations:
 - storing content in memory exposes you to potentially dramatic problem: for example a request may post an infinite representation which would injure your JVM.
 - storing content on file system is not a solution that the framework can impose by default

2)

Best regards,
Thierry Boileau



2013/12/12 Ramesh <[hidden email]>
I have an Order class and OrderDetail model as described below.

class Order {
   long id;
   String description;
   Collection<OrderDetail> details;

   public Collection<OrderDetail> getDetails() {
         return details;
   }
  .... other methods
}

class OrderDetail {
    long id;
    String productId;
    String qty;

   ... methods here...
}

And I have a JSON string of one Order with nested collection of OrderDetail. And I am trying to de-serialize this json string into Order object and expecting that the Order object will now contain the OrderDetail collection also as expected. But it is not working as expected.

Here is the code snippet I have:

JacksonRepresentation<Order> jsonRepresentation = new JacksonRepresentation<Order>(representation, Order.class);
System.out.println(jsonRepresentation.getText()); // HERE IS THE CULPRIT
Order order = jsonRepresentation.getObject();
Collection<OrderDetail> details = order.getDetails();

1) When I do jsonRepresentation.getObject(), it is throwing an exception.

 (java.io.EOFException) java.io.EOFException: No content to map to Object due to end of input

Why is that jsonRepresentation.getText(), causing the subsequent getObject to fail?

2) I have an extended Collection class called ForeignCollection, which is simply a subclass of Collection. In order to use ForeignCollection instead of Collection, what do I have to do in Restlet? I am getting the following error when I change Collection to ForeignCollection in the above two classes - Order and OrderDetail:

 (org.codehaus.jackson.map.JsonMappingException) org.codehaus.jackson.map.JsonMappingException: Can not find a deserializer for non-concrete Collection type [collection type; ForeignCollection, contains [simple type, class Order]

I am using Restlet 2.1.2.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3070147