use of com.google.gson.GsonBuilder in project ninja by ninjaframework.
the class ApiControllerDocTest method getGsonWithLongToDateParsing.
private Gson getGsonWithLongToDateParsing() {
// Creates the json object which will manage the information received
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.create();
return gson;
}
use of com.google.gson.GsonBuilder in project ninja by ninjaframework.
the class ApiControllerDocTesterTest method getGsonWithLongToDateParsing.
private Gson getGsonWithLongToDateParsing() {
// Creates the json object which will manage the information received
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.create();
return gson;
}
use of com.google.gson.GsonBuilder in project ninja by ninjaframework.
the class ApiControllerTest method getGsonWithLongToDateParsing.
private Gson getGsonWithLongToDateParsing() {
// Creates the json object which will manage the information received
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.create();
return gson;
}
use of com.google.gson.GsonBuilder in project Prism-Bukkit by prism.
the class PsteApi method createPaste.
/**
* Create a new paste
*
* @param paste
* @throws IOException
*/
public Results createPaste(Paste paste) throws IOException {
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
final String jsonPayload = gson.toJson(paste);
final URL url = new URL(apiUrl + "/paste/");
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Length", Integer.toString(jsonPayload.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setRequestProperty("Authorization", "Basic " + DatatypeConverter.printBase64Binary((apiUsername + ":" + apiKey).getBytes()));
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
final DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(jsonPayload);
wr.flush();
wr.close();
// Read response
final InputStream is = connection.getInputStream();
final BufferedReader rd = new BufferedReader(new InputStreamReader(is));
final String json = readAll(rd);
final Results response = gson.fromJson(json, Results.class);
return response;
}
use of com.google.gson.GsonBuilder in project core by s4.
the class ObjectBuilder method main.
public static void main(String[] argv) throws Exception {
ObjectBuilder b = new ObjectBuilder();
String s = "{a:5, b:100}";
Object out = b.fromJson(s, TEST.class);
System.out.println(out.toString());
TEST t = new TEST(1, 2);
System.out.println(b.toJson(t));
String[] query = { "name", "count", "freq" };
String[] target = { "ACDW", "11" };
io.s4.message.Request.ClientRInfo rinfo = new io.s4.message.Request.ClientRInfo();
rinfo.setRequesterUUID(UUID.randomUUID());
Request req = new io.s4.message.SinglePERequest(Arrays.asList(target), Arrays.asList(query), rinfo);
System.out.println(req.toString());
InstanceCreator<io.s4.message.Request.RInfo> infoCreator = new InstanceCreator<io.s4.message.Request.RInfo>() {
public io.s4.message.Request.RInfo createInstance(Type type) {
return new io.s4.message.Request.ClientRInfo();
}
};
Gson gson = (new GsonBuilder()).registerTypeAdapter(io.s4.message.Request.RInfo.class, infoCreator).registerTypeAdapter(Object.class, new ObjectTypeAdapter()).create();
System.out.println("gson: " + gson.toJson(req));
System.out.println("gson reversed: " + gson.fromJson(gson.toJson(req), SinglePERequest.class));
System.out.println(b.toJson(req));
System.out.println(b.toJson(Arrays.asList(query)));
System.out.println("----------------------------------------------");
ArrayList<SSTest> list = new ArrayList<SSTest>();
SSTest ss1 = new SSTest();
ss1.str = "list-element-1";
SSTest ss2 = new SSTest();
ss2.str = "list-element-2";
list.add(ss1);
list.add(ss2);
Map<String, Object> listmap = new HashMap<String, Object>();
listmap.put("ll", list);
MapTest mt = new MapTest();
mt.map = listmap;
Object listmapobj = listmap;
System.out.println("list: " + gson.toJson(list));
System.out.println("listmap: " + gson.toJson(listmap));
System.out.println("listmapobj: " + gson.toJson(listmapobj));
System.out.println("mapobject: " + gson.toJson(mt));
}
Aggregations