Skip to content

Instantly share code, notes, and snippets.

@magixsource
Created July 2, 2014 13:09
Show Gist options
  • Save magixsource/b39eeb8ddb8acf2c32ee to your computer and use it in GitHub Desktop.
Save magixsource/b39eeb8ddb8acf2c32ee to your computer and use it in GitHub Desktop.
datatables request and response by java
package com.lp.jackson;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Jsonable {
public String asJson() {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
public static Object fromJson(String json, Class clz) {
ObjectMapper mapper = new ObjectMapper();
Object t = null;
try {
t = mapper.readValue(json, clz);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return t;
}
}
{"draw":"drawit","start":0,"length":10,"order":[{"column":"username","dir":"desc"},{"column":"password","dir":"asc"}],"search":{"value":"searchValue","regex":false},"columns":[{"data":"ccccccccccccccccc","name":"username","searchable":true,"orderable":true,"search":{"value":"searchValueC1","regex":false}},{"data":"aaaaaaaaaaaaa","name":"password","searchable":true,"orderable":true,"search":{"value":"searchValueC2","regex":false}},{"data":"hhhhhhhhhhhhhhhhh","name":"address","searchable":true,"orderable":true,"search":{"value":"searchValueC3","regex":false}}]}
{"draw":"drawit","start":0,"length":10,"order":[{"column":"username","dir":"desc"},{"column":"password","dir":"asc"}],"search":{"value":"searchValue","regex":false},"columns":[{"data":"ccccccccccccccccc","name":"username","searchable":true,"orderable":true,"search":{"value":"searchValueC1","regex":false}},{"data":"aaaaaaaaaaaaa","name":"password","searchable":true,"orderable":true,"search":{"value":"searchValueC2","regex":false}},{"data":"hhhhhhhhhhhhhhhhh","name":"address","searchable":true,"orderable":true,"search":{"value":"searchValueC3","regex":false}}]}
=====================================
{"draw":1,"recordsTotal":57,"recordsFiltered":57,"datas":[["Angelica","Ramos","System Architect","London","9th Oct 09","$2,875"],["Ashton","Cox","Technical Author","San Francisco","12th Jan 09","$4,800"]]}
package com.lp.jackson;
import java.util.List;
public class Request extends Jsonable {
public String draw;
public int start;
public int length;
public List<Order> order;
public Search search;
public List<Column> columns;
}
class Search {
public String value;
public boolean regex;
}
class Order {
public String column;
public String dir;
}
class Column {
public String data;
public String name;
public boolean searchable;
public boolean orderable;
public Search search;
}
package com.lp.jackson;
import java.util.List;
public class Response extends Jsonable {
public int draw;
public int recordsTotal;
public int recordsFiltered;
public List<List<String>> datas;
}
package com.lp.jackson;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TestCase {
public static Request createRequest() {
Request req = new Request();
req.draw = "drawit";
req.length = 10;
req.start = 0;
List<Order> order = new ArrayList<Order>();
Order h = new Order();
h.column = "username";
h.dir = "desc";
order.add(h);
Order h2 = new Order();
h2.column = "password";
h2.dir = "asc";
order.add(h2);
req.order = order;
Search search = new Search();
search.value = "searchValue";
search.regex = false;
req.search = search;
Column column = new Column();
column.orderable = true;
column.searchable = true;
column.name = "username";
column.data = "ccccccccccccccccc";
Search searchC1 = new Search();
searchC1.value = "searchValueC1";
searchC1.regex = false;
column.search = searchC1;
Column column2 = new Column();
column2.orderable = true;
column2.searchable = true;
column2.name = "password";
column2.data = "aaaaaaaaaaaaa";
Search searchC2 = new Search();
searchC2.value = "searchValueC2";
searchC2.regex = false;
column2.search = searchC2;
Column column3 = new Column();
column3.orderable = true;
column3.searchable = true;
column3.name = "address";
column3.data = "hhhhhhhhhhhhhhhhh";
Search searchC3 = new Search();
searchC3.value = "searchValueC3";
searchC3.regex = false;
column3.search = searchC3;
List<Column> columns = new ArrayList<Column>();
columns.add(column);
columns.add(column2);
columns.add(column3);
req.columns = columns;
return req;
}
/**
* @param args
*/
public static void main(String[] args) {
Request req = createRequest();
System.out.println(req.asJson());
Request req2 = (Request) Jsonable.fromJson(req.asJson(), Request.class);
System.out.println(req2.asJson());
System.out.println("=====================================");
Response resp = new Response();
resp.draw = 1;
resp.recordsFiltered = 57;
resp.recordsTotal = 57;
List<List<String>> data = new ArrayList<List<String>>();
String[] user1 = new String[] { "Angelica", "Ramos",
"System Architect", "London", "9th Oct 09", "$2,875" };
String[] user2 = new String[] { "Ashton", "Cox", "Technical Author",
"San Francisco", "12th Jan 09", "$4,800" };
data.add(Arrays.asList(user1));
data.add(Arrays.asList(user2));
resp.datas = data;
System.out.println(resp.asJson());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment