public void Test_serialization_complex_json() { var json = "[0, 1, 2, [1,2,3], {\"aaa\": [1,2]}, [{b: 5}], {1: 1.1, 2: 2.2} ]"; var array = (object[])PlainJsonConvert.Parse(json); Assert.IsInstanceOf <object[]>(array[3]); Assert.IsInstanceOf <Dictionary <string, object> >(array[4]); var dict1 = (Dictionary <string, object>)array[4]; Assert.AreEqual("aaa", dict1.Keys.ToArray()[0]); Assert.IsInstanceOf <object[]>(dict1["aaa"]); var array2 = (object[])dict1["aaa"]; Assert.AreEqual(2, array2.Length); Assert.AreEqual(1, array2[0]); Assert.AreEqual(2, array2[1]); Assert.IsInstanceOf <Dictionary <string, object> >(array[6]); var dict3 = (Dictionary <string, object>)array[6]; Assert.Contains("1", dict3.Keys); Assert.Contains("2", dict3.Keys); }
public void Test_serialization_of_array() { var a1 = new object[] { 123, "aaaa", 12.5M }; var json = PlainJsonConvert.Generate(a1); var a2 = (object[])PlainJsonConvert.Parse(json); Assert.NotNull(a2); Assert.AreEqual(123, a2[0]); Assert.AreEqual("aaaa", a2[1]); }
public void Test_serialization_of_property_bag() { var dt = new Dictionary <string, object>(); dt.Add("_id", 1111); dt.Add("name", "aaaaaaaa"); var str = PlainJsonConvert.Generate(dt); var dt2 = (IDictionary <string, object>)PlainJsonConvert.Parse(str); Assert.NotNull(dt2); Assert.AreEqual(2, dt2.Count); Assert.AreEqual(1111, (long)dt2["_id"]); }
public static IDictionary <string, object> JsonRpc(string method, params object[] args) { var id = Guid.NewGuid(); var requestData = new Dictionary <string, object>(); requestData["id"] = id; requestData["method"] = method; requestData["params"] = args; var requestContent = PlainJsonConvert.Generate(requestData); // Prepare web request... HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://localhost:9287/jsonrpc"); httpRequest.Method = "POST"; httpRequest.ContentType = "text/json"; using (var stream = httpRequest.GetRequestStream()) using (var writer = new StreamWriter(stream, Encoding.UTF8)) { writer.Write(requestContent); writer.Close(); } HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse(); using (var responseStream = response.GetResponseStream()) { var result = (IDictionary <string, object>)PlainJsonConvert.Parse(responseStream); responseStream.Close(); Assert.AreEqual(id, result["id"]); return(result); } }
public static byte[] InvokeJsonRpc(byte[] json) { Debug.Assert(json != null); var jreq = (IDictionary <string, object>)PlainJsonConvert.Parse(json); //执行调用 object id; if (!jreq.TryGetValue(JsonRpcProtocol.Id, out id)) { return(GenerateResponse(new JsonRpcResponse() { Error = JsonRpcError.InvalidRequest, })); } object methodNameObj; if (!jreq.TryGetValue(JsonRpcProtocol.Method, out methodNameObj)) { return(GenerateResponse(new JsonRpcResponse() { Id = id, Error = JsonRpcError.InvalidRpcMethod })); } string methodName = (string)methodNameObj; MethodInfo method; if (!s_methods.TryGetValue(methodName, out method) || method == null) { return(GenerateResponse(new JsonRpcResponse() { Id = id, Error = JsonRpcError.InvalidRpcMethod })); } JsonRpcError error = null; object result = null; object argsObj; if (!jreq.TryGetValue(JsonRpcProtocol.Params, out argsObj)) { return(GenerateResponse(new JsonRpcResponse() { Id = id, Error = JsonRpcError.InvalidRpcMethod })); } var args = (object[])argsObj; LoggerProvider.RpcLogger.Debug(() => string.Format("JSON-RPC: method=[{0}], params=[{1}]", methodName, args)); try { var startTime = Stopwatch.GetTimestamp(); try { result = method.Invoke(null, args); } catch (TargetInvocationException tiex) { throw tiex.InnerException; } catch { throw; } var endTime = Stopwatch.GetTimestamp(); var costTime = endTime - startTime; LoggerProvider.RpcLogger.Debug( () => String.Format("Transaction costed time: [{0:N0}ms]", costTime * 1000 / Stopwatch.Frequency)); } catch (FatalException fex) { LoggerProvider.EnvironmentLogger.FatalException("FatalError", fex); throw fex; //接着抛出异常,让系统结束运行 } catch (ArgumentException ex) { error = JsonRpcError.RpcArgumentError; LoggerProvider.EnvironmentLogger.ErrorException("ArgumentException", ex); } catch (ValidationException ex) { error = JsonRpcError.ValidationError; LoggerProvider.EnvironmentLogger.ErrorException("ValidationException", ex); } catch (SecurityException ex) { error = JsonRpcError.SecurityError; LoggerProvider.EnvironmentLogger.ErrorException("SecurityError", ex); } catch (ResourceNotFoundException ex) { error = JsonRpcError.ResourceNotFound; LoggerProvider.EnvironmentLogger.ErrorException("ResourceNotFoundException", ex); } catch (RecordNotFoundException ex) { error = JsonRpcError.ResourceNotFound; LoggerProvider.EnvironmentLogger.ErrorException("ResourceNotFoundException", ex); } catch (DataException ex) { error = JsonRpcError.BadData; LoggerProvider.EnvironmentLogger.ErrorException("BadData", ex); } catch (System.Data.Common.DbException ex) { error = JsonRpcError.DBError; LoggerProvider.EnvironmentLogger.ErrorException("DBError", ex); } catch (System.Exception ex) { error = JsonRpcError.ServerInternalError; LoggerProvider.EnvironmentLogger.ErrorException("RPCHandler Error", ex); throw ex; //未知异常,与致命异常同样处理,直接抛出,让系统结束运行 } var jresponse = new JsonRpcResponse() { Id = id, Error = error, Result = result }; return(GenerateResponse(jresponse)); }