PageRenderTime 31ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/SignalR/Hubs/HubRequestParser.cs

https://github.com/kpmrafeeq/SignalR
C# | 34 lines | 26 code | 7 blank | 1 comment | 2 complexity | 9936e3981ca24857c595f2e9a0a818e2 MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Newtonsoft.Json.Linq;
  5. namespace SignalR.Hubs
  6. {
  7. internal class HubRequestParser : IHubRequestParser
  8. {
  9. private static readonly IJsonValue[] _emptyArgs = new IJsonValue[0];
  10. public HubRequest Parse(string data)
  11. {
  12. var rawRequest = JObject.Parse(data);
  13. var request = new HubRequest();
  14. // TODO: Figure out case insensitivity in JObject.Parse, this should cover our clients for now
  15. request.Hub = rawRequest.Value<string>("hub") ?? rawRequest.Value<string>("Hub");
  16. request.Method = rawRequest.Value<string>("method") ?? rawRequest.Value<string>("Method");
  17. request.Id = rawRequest.Value<string>("id") ?? rawRequest.Value<string>("Id");
  18. var rawState = rawRequest["state"] ?? rawRequest["State"];
  19. request.State = rawState == null ? new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) :
  20. rawState.ToObject<IDictionary<string, object>>();
  21. var rawArgs = rawRequest["args"] ?? rawRequest["Args"];
  22. request.ParameterValues = rawArgs == null ? _emptyArgs :
  23. rawArgs.Children().Select(value => new JTokenValue(value)).ToArray();
  24. return request;
  25. }
  26. }
  27. }