/** * Parses the passed JSON string into an OpenSocialResponse object -- if the * passed string represents a JSON array, each object _in the array is added * to the returned object keyed on its "id" property. * * @param _in The complete JSON string returned from an OpenSocial container * _in response to a request for data * @throws JSONException */ public static OpenSocialResponse getResponse(String _in) { OpenSocialResponse r = null; if (_in[0] == '[') { JsonArray responseArray = new JsonArray(_in); r = new OpenSocialResponse(); for (int i=0; i<responseArray.Length; i++) { JsonObject o = responseArray.GetObject(i); if (o.Contains("id")) { String id = o["id"].ToString(); r.addItem(id, escape(o.ToString())); } } } return r; }
protected void dispatchBatch(JsonArray batch, HttpResponse response, ISecurityToken token) { // Use linked hash map to preserve order List<IAsyncResult> responses = new List<IAsyncResult>(batch.Length); // Gather all Futures. We do this up front so that // the first call to get() comes after all futures are created, // which allows for implementations that batch multiple Futures // into single requests. for (int i = 0; i < batch.Length; i++) { JsonObject batchObj = batch.GetObject(i); RpcRequestItem requestItem = new RpcRequestItem(batchObj, token, jsonConverter); responses.Add(HandleRequestItem(requestItem)); } // Resolve each Future into a response. // TODO: should use shared deadline across each request List<Object> result = new List<object>(); for (int i = 0; i < batch.Length; i++) { JsonObject batchObj = batch.GetObject(i); String key = null; if (batchObj.Contains("id")) { key = batchObj["id"] as String; } result.Add(getJsonResponse(key, getResponseItem(responses[i]), null)); } response.Output.Write(jsonConverter.ConvertToString(result)); }
/** * Iterates through the objects _in the passed JsonArray object, recursively * transforms each as needed, and returns a List of Java objects. * * @param a Object-oriented representation of a JSON array which is iterated * through and returned as a List of Java objects * @throws JSONException */ private static List<Object> createArrayRepresentation(JsonArray a) { List<Object> r = new List<Object>(a.Length); for (int i=0; i<a.Length; i++) { String member = a.GetString(i); if (member.Length > 0 && member[0] == '{') { JsonObject p = a.GetObject(i); r.Add(new OpenSocialObject(createObjectRepresentation(p))); } else if (member.Length > 0 && member[0] == '[') { JsonArray p = (JsonArray)a[i]; List<Object> values = createArrayRepresentation(p); foreach(var v in values) { r.Add(v); } } else if (member.Length > 0) { r.Add(member); } } return r; }