From bdb648b919f9f4d43ecacadf2a6fd009075c66ad Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Tue, 4 Mar 2014 22:23:49 +0100 Subject: [PATCH] JSONRPC4NET no time for comments --- Mono/RemoteAgents/GTKLinux.sln | 2 ++ jsonrpc4net/jsonrpc4net/ExceptionResolver.cs | 5 ++-- jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs | 33 ++++++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Mono/RemoteAgents/GTKLinux.sln b/Mono/RemoteAgents/GTKLinux.sln index 4a9f35d..33a3f8f 100644 --- a/Mono/RemoteAgents/GTKLinux.sln +++ b/Mono/RemoteAgents/GTKLinux.sln @@ -174,6 +174,8 @@ Global $28.inheritsSet = Mono $28.inheritsScope = text/x-csharp $28.scope = text/x-csharp + $0.VersionControlPolicy = $29 + $29.inheritsSet = Mono description = @Little program to connect with remote applications\nthrough JSON API. EndGlobalSection EndGlobal diff --git a/jsonrpc4net/jsonrpc4net/ExceptionResolver.cs b/jsonrpc4net/jsonrpc4net/ExceptionResolver.cs index 4b302c5..c00e6cd 100644 --- a/jsonrpc4net/jsonrpc4net/ExceptionResolver.cs +++ b/jsonrpc4net/jsonrpc4net/ExceptionResolver.cs @@ -48,7 +48,7 @@ namespace GumartinM.JsonRPC4NET return new JsonRpcClientException( errorToken.Value("code") ?? 0, errorToken.Value("message"), - errorToken.SelectToken("data")); + errorToken["data"]); } /// @@ -59,7 +59,8 @@ namespace GumartinM.JsonRPC4NET /// Message. private Exception CreateException(string exceptionTypeName, string message) { - return new Exception("Remote exception: " + exceptionTypeName + "Message: " + message); + return new Exception("Remote exception: " + exceptionTypeName + + " Remote message: " + message); } } } diff --git a/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs b/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs index 2eaf5f2..50ad4fb 100644 --- a/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs +++ b/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs @@ -8,6 +8,7 @@ using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using System.IO; namespace GumartinM.JsonRPC4NET { @@ -29,7 +30,7 @@ namespace GumartinM.JsonRPC4NET private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings { - Error = delegate(object sender, ErrorEventArgs args) + Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) { _logger.Error(args.ErrorContext.Error.Message); args.ErrorContext.Handled = true; @@ -84,9 +85,12 @@ namespace GumartinM.JsonRPC4NET if (response.StatusCode == HttpStatusCode.OK) { - byte[] jsonBytes = await response.Content.ReadAsByteArrayAsync(); + //byte[] jsonBytes = await response.Content.ReadAsByteArrayAsync(); + Stream stream = await response.Content.ReadAsStreamAsync(); + + //return this.ReadResponse(jsonBytes); + return await this.ReadResponseAsync(stream); - return this.ReadResponse(jsonBytes); } throw new Exception("Unexpected response code: " + response.StatusCode); @@ -103,6 +107,29 @@ namespace GumartinM.JsonRPC4NET { string json = System.Text.Encoding.UTF8.GetString(jsonBytes, 0, jsonBytes.Length); + return this.ReadResponse(json); + } + + /// + /// Reads the response. + /// + /// The response. + /// Stream. + /// The 1st type parameter. + async private Task> ReadResponseAsync(Stream stream) + { + using (StreamReader streamReader = new StreamReader (stream, System.Text.Encoding.UTF8)) + { + // This line makes this method useless (IMHO it is the same as the one working with bytes) + // How could I work with JSON saving memory? + string json = await streamReader.ReadToEndAsync(); + + return this.ReadResponse(json); + } + } + + private POSTResult ReadResponse(string json) + { JObject jsonObjects = JObject.Parse(json); IDictionary jsonTokens = jsonObjects; -- 2.1.4