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