Pregunta

How do I use a custom order when using FlexJSON 2.1 with Play Framework? Right now I have:

public String toJsonString() {
    // Include id & name, exclude all others.
    JSONSerializer ser = new JSONSerializer().include(
           "id",
           "firstName",
           "lastName",
           "email",
           "authToken",
           "dob",
           "home_zip",
           "gender",
    ).exclude("*").prettyPrint(true);
   return ser.serialize(this);
 }

And it prints out in alphabetical order.

I found this SO post, but I don't have any class defined as @XMLRootElement. How do I tell FlexJSON to use a custom ordering?

¿Fue útil?

Solución

Order is only derived from the data structure itself. Data structures that employ an order (ie List, TreeSet, TreeMap) will have an order. Fields within objects or HashMap order isn't guaranteed. Bottom line there is no order explicitly imposed on things other than their natural order. You can try and override the default serialization Transformers to impose order if you want. You can start by subclassing ObjectTransformer and MapTransformer, and trying to impose an order.

Ordering JSON data structures seems odd since fields within objects in javascript don't have an order either. I can't imagine why this would be important to do.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top