2

I need to return a list of ordered String from an ajax Call

my DTO has an attributes sizes which is a list of String values , eg : 36,38,40/46, 46, S, XS, M, L

my comparator and sorting algorithm work well , the list of sizes is well sorted until its converted to json format, i found a totally different order .

@ResponseBody
@RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/crossell", method = RequestMethod.GET)
public ProductData cosselProductDetail(@PathVariable("productCode") final String productCode,
        final HttpServletResponse response) throws CMSItemNotFoundException,
        UnsupportedEncodingException
{

   Collections.sort(sortedSizes,mycomparator);
    productData.setSizes(sortedSizes);
    return productData;   
}

the spring config is the default configuration no thing added or modified .

<!-- activates annotation driven binding -->
<mvc:annotation-driven ignoreDefaultModelOnRedirect="true" validator="validator">
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
    </mvc:message-converters>
</mvc:annotation-driven>

after searching all around , all what i found is the serialisation key config : SORT_PROPERTIES_ALPHABETICALLY , which order the properties not the values.

Thanks

  • What is the type of `productData.sizes`? – ponomandr Oct 15 '14 at 13:39
  • 1
    there's a strong argument that if the order matters its not really JSON, well discussed here http://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv/4515863#4515863 That being sad, I would expect that the default configuration preserves the order after converting. So pardon me for asking but are you sure that that's the point where you lose sorting. Also, can you check (in debugger) and paste what is the concrete implementation of the sizes list – Master Slave Oct 15 '14 at 19:36
  • To be more specific , sizes is a linkedHashMap (OfferOption extends ProductDTO) i lost the order of the keys I'm afraid i miss-explains the problem when i said is just a List , is that making any difference ? Thanks – El Jaoujat Youssef Oct 16 '14 at 09:44

1 Answers1

1

Its not an answer, but I'm posting like that 'cause its easier to format. Regardless of being the LinkedHashMap, I still believe that serialization will keep the order. I was curious so I typed out some an analogous example, something like this:

@ResponseBody
@RequestMapping(value = "/crossel", method = RequestMethod.GET)
public ProductData cosselProductDetail() {

    List<String> sortedSizes = new LinkedList<>();
    sortedSizes.add("11");
    sortedSizes.add("8");
    sortedSizes.add("10");
    sortedSizes.add("15");
    sortedSizes.add("20");
    sortedSizes.add("5");
    sortedSizes.add("3");
    sortedSizes.add("7");
    sortedSizes.add("40");
    Map<String, String> linkedHS = new LinkedHashMap<>();
    Collections.sort(sortedSizes, new ValueComparator(linkedHS));
    for (String key : sortedSizes) {
        linkedHS.put(key, "test");
    }
    ProductData productData = new ProductData();
    productData.setSizes(linkedHS);
    return productData;
}

class SizeComparator implements Comparator<String> {

    @Override
    public int compare(String e1, String e2) {
        if (e1.length() == 1) e1 = "0" + e1;
        if (e2.length() == 1) e2 = "0" + e2;
        return e1.compareTo(e2);
    }
}


class ProductData {
    Map<String, String> sizes;

    public Map<String, String> getSizes() {
        return sizes;
    }

    public void setSizes(Map<String, String> sizes) {
        this.sizes = sizes;
    }
}

and this returns

{"sizes":{"3":"test","5":"test","7":"test","8":"test","10":"test","11":"test","15":"test","20":"test","40":"test"}}

and keep in mind that keys are custom sorted. So in Spring MVC 3.2 + with more or less the default configuration I would expect the order preserved. Post your code, don't worry about bloating the question, should be fixable

Master Slave
  • 27,771
  • 4
  • 57
  • 55