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 17package com.googlecode.android_scripting.jsonrpc; 18 19import org.json.JSONException; 20import org.json.JSONObject; 21 22/** 23 * Represents a JSON RPC result. 24 * 25 * @see http://json-rpc.org/wiki/specification 26 * 27 */ 28public class JsonRpcResult { 29 30 private JsonRpcResult() { 31 // Utility class. 32 } 33 34 public static JSONObject empty(int id) throws JSONException { 35 JSONObject json = new JSONObject(); 36 json.put("id", id); 37 json.put("result", JSONObject.NULL); 38 json.put("error", JSONObject.NULL); 39 return json; 40 } 41 42 public static JSONObject result(int id, Object data) throws JSONException { 43 JSONObject json = new JSONObject(); 44 json.put("id", id); 45 json.put("result", JsonBuilder.build(data)); 46 json.put("error", JSONObject.NULL); 47 return json; 48 } 49 50 public static JSONObject error(int id, Throwable t) throws JSONException { 51 JSONObject json = new JSONObject(); 52 json.put("id", id); 53 json.put("result", JSONObject.NULL); 54 json.put("error", t.toString()); 55 return json; 56 } 57} 58