• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.google.android.mobly.snippet.rpc;
18 
19 import java.io.PrintWriter;
20 import java.io.StringWriter;
21 import org.json.JSONException;
22 import org.json.JSONObject;
23 
24 /**
25  * Represents a JSON RPC result.
26  *
27  * @see <a href="http://json-rpc.org/wiki/specification">http://json-rpc.org/wiki/specification</a>
28  */
29 public class JsonRpcResult {
30 
JsonRpcResult()31     private JsonRpcResult() {
32         // Utility class.
33     }
34 
empty(int id)35     public static JSONObject empty(int id) throws JSONException {
36         JSONObject json = new JSONObject();
37         json.put("id", id);
38         json.put("result", JSONObject.NULL);
39         json.put("callback", JSONObject.NULL);
40         json.put("error", JSONObject.NULL);
41         return json;
42     }
43 
result(int id, Object data)44     public static JSONObject result(int id, Object data) throws JSONException {
45         JSONObject json = new JSONObject();
46         json.put("id", id);
47         json.put("result", JsonBuilder.build(data));
48         json.put("callback", JSONObject.NULL);
49         json.put("error", JSONObject.NULL);
50         return json;
51     }
52 
callback(int id, Object data, String callbackId)53     public static JSONObject callback(int id, Object data, String callbackId) throws JSONException {
54         JSONObject json = new JSONObject();
55         json.put("id", id);
56         json.put("result", JsonBuilder.build(data));
57         json.put("callback", callbackId);
58         json.put("error", JSONObject.NULL);
59         return json;
60     }
61 
error(int id, Throwable t)62     public static JSONObject error(int id, Throwable t) throws JSONException {
63         String stackTrace = getStackTrace(t);
64         JSONObject json = new JSONObject();
65         json.put("id", id);
66         json.put("result", JSONObject.NULL);
67         json.put("callback", JSONObject.NULL);
68         json.put("error", stackTrace);
69         return json;
70     }
71 
getStackTrace(Throwable throwable)72     public static String getStackTrace(Throwable throwable) {
73         StringWriter stackTraceWriter = new StringWriter();
74         stackTraceWriter.write("\n-------------- Java Stacktrace ---------------\n");
75         throwable.printStackTrace(new PrintWriter(stackTraceWriter));
76         stackTraceWriter.write("----------------------------------------------");
77         return stackTraceWriter.toString();
78     }
79 }
80