FlexJSON Orders Alphabetically by Default

谁说我不能喝 提交于 2019-12-10 23:03:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17013123/flexjson-orders-alphabetically-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!