Note that there are some explanatory texts on larger screens.

plurals
  1. POJersey and JSONConfiguration serialization an emplty list and single element list
    primarykey
    data
    text
    <p>I'm struggling with serialization an emplty list and single element list in Jersey for days now. I searched a lot I couldnt find a solution for my problem. I tried to fix it by adding JAXBContextResolver class. The goal is to return JSON object that contains JSON array to my Android application in all cases (if it returns 0 element or 1 element or more than 1). I tried JSONConfiguration by using <code>MAPPED</code> and <code>NATURAL</code> JSON notations. I would greatly appreciate your help. Thanks in advance</p> <p>I used <code>NATURAL JSONConfiguration</code> as presented in this question : </p> <p><a href="https://stackoverflow.com/questions/16674207/jersey-serialize-empty-list-and-single-element-list-as-an-array">Jersey serialize empty list and single element list as an array</a></p> <p>But I think the better way is to use <code>MAPPED JSONConfiguration</code> but the problem with this configuration that it working only for one Class and It is not resolving the issue with the empty list. Please I really need your help </p> <pre><code>@Provider public class JAXBContextResolver implements ContextResolver &lt; JAXBContext &gt; { private JAXBContext context; private Class[] types = {Workitem.class, Project.class, User.class}; public JAXBContextResolver() throws Exception { this.context = new JSONJAXBContext(JSONConfiguration.mapped().arrays("workitem").build(), types); } public JAXBContext getContext(Class objectType) { for (Class type : types) { if (type == objectType) { return context; } } return null; } } </code></pre> <p>I also tried this solution <a href="https://stackoverflow.com/questions/2199453/how-can-i-customize-serialization-of-a-list-of-jaxb-objects-to-json/3143214#3143214">How can I customize serialization of a list of JAXB objects to JSON?</a></p> <p>But I really need JSON Data to look like this :</p> <pre><code>{ "project": [ { "assignedTo": "assignee1", "businessKey": "Key1", "createdBy": "createdBy1", "description": "description1" } ] } </code></pre> <p>And for the empty list to look like this :</p> <pre><code> { "project": [] } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload