public static EtcdResult Parse(string jsonStr) { Object obj; if (!SimpleJson.SimpleJson.TryDeserializeObject(jsonStr, out obj)) { return(null); } SimpleJson.JsonObject jsonObj = (SimpleJson.JsonObject)obj; EtcdResult result = new EtcdResult(); if (jsonObj.ContainsKey("errorCode")) { result.action = "unknown"; result.error = EtcdError.Parse(jsonObj); } else { if (jsonObj.ContainsKey("action")) { result.action = (string)jsonObj["action"]; } if (jsonObj.ContainsKey("node")) { result.node = EtcdResultNode.Parse((SimpleJson.JsonObject)jsonObj["node"]); } if (jsonObj.ContainsKey("prevNode")) { result.prevNode = EtcdResultNode.Parse((SimpleJson.JsonObject)jsonObj["prevNode"]); } } return(result); }
public static EtcdResultNode Parse(SimpleJson.JsonObject jsonObj) { EtcdResultNode node = new EtcdResultNode(); node.key = (string)jsonObj["key"]; node.createdIndex = (long)jsonObj["createdIndex"]; node.modifiedIndex = (long)jsonObj["modifiedIndex"]; if (jsonObj.ContainsKey("dir")) { node.dir = (bool)jsonObj["dir"]; } if (jsonObj.ContainsKey("ttl")) { node.ttl = (long)jsonObj["ttl"]; } if (jsonObj.ContainsKey("expiration")) { DateTime expiration; if (DateTime.TryParse((string)jsonObj["expiration"], out expiration)) { node.expiration = expiration; } } if (jsonObj.ContainsKey("value")) { node.value = (string)jsonObj["value"]; } if (jsonObj.ContainsKey("nodes")) { SimpleJson.JsonArray children = (SimpleJson.JsonArray)jsonObj["nodes"]; node.nodes = new EtcdResultNode[children.Count]; for (int i = 0; i < children.Count; ++i) { node.nodes[i] = Parse((SimpleJson.JsonObject)children[i]); } } return(node); }
public static EtcdError Parse(SimpleJson.JsonObject jsonObj) { EtcdError error = new EtcdError(); error.errorCode = (long)jsonObj["errorCode"]; error.index = (long)jsonObj["index"]; if (jsonObj.ContainsKey("cause")) { error.cause = (string)jsonObj["cause"]; } error.message = (string)jsonObj["message"]; return(error); }
static int _m_ContainsKey(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); SimpleJson.JsonObject gen_to_be_invoked = (SimpleJson.JsonObject)translator.FastGetCSObj(L, 1); { string _key = LuaAPI.lua_tostring(L, 2); bool gen_ret = gen_to_be_invoked.ContainsKey(_key); LuaAPI.lua_pushboolean(L, gen_ret); return(1); } } catch (System.Exception gen_e) { return(LuaAPI.luaL_error(L, "c# exception:" + gen_e)); } }
internal virtual object ProcessResponse(GithubApiVersion version, FluentHttpAsyncResult asyncResult, Stream responseStream, Type resultType, out Exception exception) { // FluentHttpRequest has ended. // don't throw exception in this method but send it as an out parameter. // we can reuse this method for async methods too. // so the caller of this method decides whether to throw or not. exception = asyncResult.Exception; if (exception != null) { if (responseStream != null) responseStream.Dispose(); return null; } if (asyncResult.IsCancelled) { exception = new NotImplementedException(); return null; } var response = asyncResult.Response; // async request completed if (response.HttpWebResponse.StatusCode == HttpStatusCode.BadGateway) { if (responseStream != null) responseStream.Dispose(); exception = asyncResult.InnerException; return null; } var httpWebRequestHeaders = response.HttpWebResponse.Headers; var headers = new SimpleJson.JsonObject(); foreach (var headerKey in httpWebRequestHeaders.AllKeys) headers.Add(headerKey, httpWebRequestHeaders[headerKey]); var result = new SimpleJson.JsonObject { {"code", (int) response.HttpWebResponse.StatusCode}, {"headers", headers} }; if (response.Request.GetMethod().Equals("HEAD", StringComparison.OrdinalIgnoreCase)) { return result; } // convert the response stream to string. responseStream.Seek(0, SeekOrigin.Begin); switch (response.HttpWebResponse.StatusCode) { case HttpStatusCode.Created: case HttpStatusCode.OK: try { if (headers.ContainsKey("X-GitHub-Blob-Sha")) { // responseStream is a binary data // responseStream is always memory stream var ms = (MemoryStream)responseStream; var data = ms.ToArray(); ms.Dispose(); result["body"] = data; } else { // responseStream is a string. var responseString = FluentHttpRequest.ToString(responseStream); // we got the response string already, so dispose the memory stream. responseStream.Dispose(); result["body"] = (resultType == null) ? JsonSerializer.Current.DeserializeObject(responseString) : JsonSerializer.Current.DeserializeObject(responseString, resultType); } return result; } catch (Exception ex) { exception = ex; return null; } case HttpStatusCode.NoContent: responseStream.Dispose(); return result; default: { var responseString = FluentHttpRequest.ToString(responseStream); responseStream.Dispose(); // Github api responded with an error. object json; exception = ExceptionFactory.GetException(responseString, version, response, out json); result["body"] = json; return result; } } }