List of usage examples for com.google.gson JsonSerializationContext serialize
public JsonElement serialize(Object src);
From source file:org.rapla.rest.gwtjsonrpc.server.JsonServlet.java
License:Apache License
private String formatResult(final ActiveCall call) throws UnsupportedEncodingException, IOException { final GsonBuilder gb = createGsonBuilder(); gb.registerTypeAdapter(call.getClass(), new JsonSerializer<ActiveCall>() { @Override/*from w w w. ja v a 2 s . c om*/ public JsonElement serialize(final ActiveCall src, final Type typeOfSrc, final JsonSerializationContext context) { if (call.externalFailure != null) { final String msg; if (call.method != null) { msg = "Error in " + call.method.getName(); } else { msg = "Error"; } logger.error(msg, call.externalFailure); } Throwable failure = src.externalFailure != null ? src.externalFailure : src.internalFailure; Object result = src.result; if (result instanceof FutureResult) { try { result = ((FutureResult) result).get(); } catch (Exception e) { failure = e; } } final JsonObject r = new JsonObject(); if (src.versionName == null || src.versionValue == null) { r.add("jsonrpc", new JsonPrimitive("2.0")); } else { r.add(src.versionName, src.versionValue); } if (src.id != null) { r.add("id", src.id); } if (failure != null) { final int code = to2_0ErrorCode(src); final JsonObject error = getError(src.versionName, code, failure, gb); r.add("error", error); } else { r.add("result", context.serialize(result)); } return r; } }); Gson create = gb.create(); final StringWriter o = new StringWriter(); create.toJson(call, o); o.close(); String string = o.toString(); return string; }
From source file:org.rapla.server.jsonrpc.JsonServlet.java
License:Apache License
private String formatResult(final ActiveCall call) throws UnsupportedEncodingException, IOException { final GsonBuilder gb = createGsonBuilder(); gb.registerTypeAdapter(call.getClass(), new JsonSerializer<ActiveCall>() { @Override/*w w w . ja v a 2 s . c om*/ public JsonElement serialize(final ActiveCall src, final Type typeOfSrc, final JsonSerializationContext context) { if (call.callback != null) { if (src.externalFailure != null) { return new JsonNull(); } return context.serialize(src.result); } final JsonObject r = new JsonObject(); r.add(src.versionName, src.versionValue); if (src.id != null) { r.add("id", src.id); } if (src.xsrfKeyOut != null) { r.addProperty("xsrfKey", src.xsrfKeyOut); } if (src.externalFailure != null) { final JsonObject error = new JsonObject(); if ("jsonrpc".equals(src.versionName)) { final int code = to2_0ErrorCode(src); error.addProperty("code", code); error.addProperty("message", src.externalFailure.getMessage()); } else { error.addProperty("name", "JSONRPCError"); error.addProperty("code", 999); error.addProperty("message", src.externalFailure.getMessage()); } r.add("error", error); } else { r.add("result", context.serialize(src.result)); } return r; } }); final StringWriter o = new StringWriter(); if (call.callback != null) { o.write(call.callback); o.write("("); } gb.create().toJson(call, o); if (call.callback != null) { o.write(");"); } o.close(); return o.toString(); }
From source file:org.restcomm.connect.http.converter.CallDetailRecordListConverter.java
License:Open Source License
@Override public JsonObject serialize(CallDetailRecordList cdrList, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); JsonArray array = new JsonArray(); for (CallDetailRecord cdr : cdrList.getCallDetailRecords()) { array.add(context.serialize(cdr)); }//from w w w .j av a2s.com if (total != null && pageSize != null && page != null) { result.addProperty("page", page); result.addProperty("num_pages", getTotalPages()); result.addProperty("page_size", pageSize); result.addProperty("total", total); result.addProperty("start", getFirstIndex()); result.addProperty("end", getLastIndex(cdrList)); result.addProperty("uri", pathUri); result.addProperty("first_page_uri", getFirstPageUri()); result.addProperty("previous_page_uri", getPreviousPageUri()); result.addProperty("next_page_uri", getNextPageUri(cdrList)); result.addProperty("last_page_uri", getLastPageUri()); } result.add("calls", array); return result; }
From source file:org.restcomm.connect.http.converter.IncomingPhoneNumberListConverter.java
License:Open Source License
@Override public JsonObject serialize(IncomingPhoneNumberList list, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); JsonArray array = new JsonArray(); for (IncomingPhoneNumber phoneNumber : list.getIncomingPhoneNumbers()) { array.add(context.serialize(phoneNumber)); }// ww w .j ava2 s . co m if (total != null && pageSize != null && page != null) { result.addProperty("page", page); result.addProperty("num_pages", getTotalPages()); result.addProperty("page_size", pageSize); result.addProperty("total", total); result.addProperty("start", getFirstIndex()); result.addProperty("end", getLastIndex(list)); result.addProperty("uri", pathUri); result.addProperty("first_page_uri", getFirstPageUri()); result.addProperty("previous_page_uri", getPreviousPageUri()); result.addProperty("next_page_uri", getNextPageUri(list)); result.addProperty("last_page_uri", getLastPageUri()); } result.add("incomingPhoneNumbers", array); return result; }
From source file:org.restcomm.connect.http.converter.MonitoringServiceConverter.java
License:Open Source License
@Override public JsonElement serialize(MonitoringServiceResponse monitoringServiceResponse, Type typeOfSrc, JsonSerializationContext context) { final Map<String, Integer> countersMap = monitoringServiceResponse.getCountersMap(); final Map<String, Double> durationMap = monitoringServiceResponse.getDurationMap(); JsonObject result = new JsonObject(); JsonObject metrics = new JsonObject(); JsonArray callsArray = new JsonArray(); //First add InstanceId and Version details result.addProperty("InstanceId", monitoringServiceResponse.getInstanceId().getId().toString()); result.addProperty("Version", Version.getVersion()); result.addProperty("Revision", Version.getRevision()); Iterator<String> counterIterator = countersMap.keySet().iterator(); while (counterIterator.hasNext()) { String counter = counterIterator.next(); metrics.addProperty(counter, countersMap.get(counter)); }// ww w.ja va 2 s. c o m Iterator<String> durationIterator = durationMap.keySet().iterator(); while (durationIterator.hasNext()) { String durationKey = durationIterator.next(); metrics.addProperty(durationKey, durationMap.get(durationKey)); } result.add("Metrics", metrics); if (monitoringServiceResponse.isWithCallDetailsList()) { if (monitoringServiceResponse.getCallDetailsList() != null && monitoringServiceResponse.getCallDetailsList().size() > 0) { for (CallInfo callInfo : monitoringServiceResponse.getCallDetailsList()) { callsArray.add(context.serialize(callInfo)); } } result.add("LiveCallDetails", callsArray); } else { result.addProperty("LiveCallDetails", UriUtils.resolve(monitoringServiceResponse.getCallDetailsUrl()).toString()); } return result; }
From source file:org.restcomm.connect.http.converter.MonitoringServiceConverterCallDetails.java
License:Open Source License
@Override public JsonElement serialize(LiveCallsDetails callDetails, Type typeOfSrc, JsonSerializationContext context) { JsonObject result = new JsonObject(); JsonArray callsArray = new JsonArray(); //First add InstanceId and Version details result.addProperty("InstanceId", RestcommConfiguration.getInstance().getMain().getInstanceId()); result.addProperty("Version", Version.getVersion()); result.addProperty("Revision", Version.getRevision()); List<CallInfo> liveCalls = callDetails.getCallDetails(); if (liveCalls.size() > 0) for (CallInfo callInfo : liveCalls) { callsArray.add(context.serialize(callInfo)); }/*from w w w.j a va 2 s.c o m*/ result.add("LiveCallDetails", callsArray); return result; }
From source file:org.restcomm.connect.http.converter.NotificationListConverter.java
License:Open Source License
@Override public JsonObject serialize(NotificationList ntfList, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); JsonArray array = new JsonArray(); for (Notification cdr : ntfList.getNotifications()) { array.add(context.serialize(cdr)); }/*from w w w .j a v a 2 s. co m*/ if (total != null && pageSize != null && page != null) { result.addProperty("page", page); result.addProperty("num_pages", getTotalPages()); result.addProperty("page_size", pageSize); result.addProperty("total", total); result.addProperty("start", getFirstIndex()); result.addProperty("end", getLastIndex(ntfList)); result.addProperty("uri", pathUri); result.addProperty("first_page_uri", getFirstPageUri()); result.addProperty("previous_page_uri", getPreviousPageUri()); result.addProperty("next_page_uri", getNextPageUri(ntfList)); result.addProperty("last_page_uri", getLastPageUri()); } result.add("notifications", array); return result; }
From source file:org.restcomm.connect.http.converter.RecordingListConverter.java
License:Open Source License
@Override public JsonObject serialize(RecordingList recList, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); JsonArray array = new JsonArray(); for (Recording cdr : recList.getRecordings()) { array.add(context.serialize(cdr)); }// w w w .j av a2 s . co m if (total != null && pageSize != null && page != null) { result.addProperty("page", page); result.addProperty("num_pages", getTotalPages()); result.addProperty("page_size", pageSize); result.addProperty("total", total); result.addProperty("start", getFirstIndex()); result.addProperty("end", getLastIndex(recList)); result.addProperty("uri", pathUri); result.addProperty("first_page_uri", getFirstPageUri()); result.addProperty("previous_page_uri", getPreviousPageUri()); result.addProperty("next_page_uri", getNextPageUri(recList)); result.addProperty("last_page_uri", getLastPageUri()); } result.add("recordings", array); return result; }
From source file:org.restcomm.connect.http.converter.SmsMessageListConverter.java
License:Open Source License
@Override public JsonObject serialize(SmsMessageList smsList, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); JsonArray array = new JsonArray(); for (SmsMessage cdr : smsList.getSmsMessages()) { array.add(context.serialize(cdr)); }//from w w w . jav a 2s. c o m if (total != null && pageSize != null && page != null) { result.addProperty("page", page); result.addProperty("num_pages", getTotalPages()); result.addProperty("page_size", pageSize); result.addProperty("total", total); result.addProperty("start", getFirstIndex()); result.addProperty("end", getLastIndex(smsList)); result.addProperty("uri", pathUri); result.addProperty("first_page_uri", getFirstPageUri()); result.addProperty("previous_page_uri", getPreviousPageUri()); result.addProperty("next_page_uri", getNextPageUri(smsList)); result.addProperty("last_page_uri", getLastPageUri()); } result.add("messages", array); return result; }
From source file:org.restcomm.connect.http.converter.TranscriptionListConverter.java
License:Open Source License
@Override public JsonObject serialize(TranscriptionList transList, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); JsonArray array = new JsonArray(); for (Transcription cdr : transList.getTranscriptions()) { array.add(context.serialize(cdr)); }//from www . ja v a2 s . co m if (total != null && pageSize != null && page != null) { result.addProperty("page", page); result.addProperty("num_pages", getTotalPages()); result.addProperty("page_size", pageSize); result.addProperty("total", total); result.addProperty("start", getFirstIndex()); result.addProperty("end", getLastIndex(transList)); result.addProperty("uri", pathUri); result.addProperty("first_page_uri", getFirstPageUri()); result.addProperty("previous_page_uri", getPreviousPageUri()); result.addProperty("next_page_uri", getNextPageUri(transList)); result.addProperty("last_page_uri", getLastPageUri()); } result.add("transcriptions", array); return result; }