Convert Gson object or string to url query String

396 views
Skip to first unread message

Alberto Starosta

unread,
Apr 14, 2016, 3:45:01 PM4/14/16
to google-gson
Hi

I need to convert json object/string to url query string , like this

src:
"searchCriteria": {
    "filterGroups": [
      {
        "filters": [
          {
            "field": "test",
            "value": "1",
            "conditionType": "eq"
          }
        ]
      }
    ],
    "sortOrders": [
      {
        "field": "all",
        "direction": "asc"
      }
    ],
    "pageSize": 2,
    "currentPage": 1
  }


to:

http://host/api/?
searchCriteria[filterGroups][][filters][][field]=test&
searchCriteria[filterGroups][][filters][][value]=1&
searchCriteria[filterGroups][][filters][][
conditionType
]=eq& ...

I've googled two days and not founded nothing util, i'jve tryed also map manually but with complex structure is very difficult,

Any suggestions ?
Thank You, alberto

Chucho Valladares

unread,
Apr 19, 2016, 8:47:30 PM4/19/16
to google-gson
I think you are misunderstanding the point of a serialization util, your best best is to use the polymorphic characteristics of the language to implement the behavior you are looking for.

As per GSON goals: "deep inheritance hierarchies and extensive use of generic types", so your best bet is to implement your own hierarchy and use GSON to serialize/deserialize.

Remember, it is important to separate object responsibilities,  keep in mind that an on object can only have 1 inheritance hierarchy which suggests that it should only have 1 responsibility. A better idea would be break up the logic into smaller objects that collaborate with each other or put it differently, polymorphism is all or nothing proposition.

Regards !
Reply all
Reply to author
Forward
0 new messages