DefaultRpcResponseSerializer.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using EdjCase.JsonRpc.Core;
  2. using EdjCase.JsonRpc.Router.Abstractions;
  3. using Microsoft.Extensions.Options;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. namespace EdjCase.JsonRpc.Router.Defaults
  11. {
  12. public class DefaultRpcResponseSerializer : IRpcResponseSerializer
  13. {
  14. private IOptions<RpcServerConfiguration> serverConfig { get; }
  15. public DefaultRpcResponseSerializer(IOptions<RpcServerConfiguration> serverConfig)
  16. {
  17. this.serverConfig = serverConfig;
  18. }
  19. public string SerializeBulk(IEnumerable<RpcResponse> responses)
  20. {
  21. return this.SerializeInternal(responses, isBulkRequest: true);
  22. }
  23. public string Serialize(RpcResponse response)
  24. {
  25. return this.SerializeInternal(new[] { response }, isBulkRequest: false);
  26. }
  27. private string SerializeInternal(IEnumerable<RpcResponse> responses, bool isBulkRequest)
  28. {
  29. using (StringWriter textWriter = new StringWriter())
  30. {
  31. using (JsonTextWriter jsonWriter = new JsonTextWriter(textWriter))
  32. {
  33. if (isBulkRequest)
  34. {
  35. jsonWriter.WriteStartArray();
  36. foreach (RpcResponse response in responses)
  37. {
  38. this.SerializeResponse(response, jsonWriter);
  39. }
  40. jsonWriter.WriteEndArray();
  41. }
  42. else
  43. {
  44. this.SerializeResponse(responses.Single(), jsonWriter);
  45. }
  46. }
  47. return textWriter.ToString();
  48. }
  49. }
  50. private void SerializeResponse(RpcResponse response, JsonTextWriter jsonWriter)
  51. {
  52. jsonWriter.WriteStartObject();
  53. jsonWriter.WritePropertyName(JsonRpcContants.IdPropertyName);
  54. jsonWriter.WriteValue(response.Id.Value);
  55. jsonWriter.WritePropertyName(JsonRpcContants.VersionPropertyName);
  56. jsonWriter.WriteValue("2.0");
  57. if (!response.HasError)
  58. {
  59. jsonWriter.WritePropertyName(JsonRpcContants.ResultPropertyName);
  60. this.SerializeValue(response.Result, jsonWriter);
  61. }
  62. else
  63. {
  64. jsonWriter.WritePropertyName(JsonRpcContants.ErrorPropertyName);
  65. jsonWriter.WriteStartObject();
  66. jsonWriter.WritePropertyName(JsonRpcContants.ErrorCodePropertyName);
  67. jsonWriter.WriteValue(response.Error.Code);
  68. jsonWriter.WritePropertyName(JsonRpcContants.ErrorMessagePropertyName);
  69. jsonWriter.WriteValue(response.Error.Message);
  70. jsonWriter.WritePropertyName(JsonRpcContants.ErrorDataPropertyName);
  71. this.SerializeValue(response.Error.Data, jsonWriter);
  72. }
  73. jsonWriter.WriteEndObject();
  74. }
  75. private void SerializeValue(object value, JsonTextWriter jsonWriter)
  76. {
  77. if (value != null)
  78. {
  79. string valueJson = JsonConvert.SerializeObject(value, this.serverConfig.Value.JsonSerializerSettings);
  80. jsonWriter.WriteRawValue(valueJson);
  81. }
  82. else
  83. {
  84. jsonWriter.WriteNull();
  85. }
  86. }
  87. }
  88. }