Browse Source

Fixed a few json settings (such as dates deserializing to UTC), and use cached jsonserializers in more places.

tags/docs-0.9
RogueException 9 years ago
parent
commit
d6342b725d
3 changed files with 21 additions and 9 deletions
  1. +6
    -1
      src/Discord.Net.Audio/Net/VoiceSocket.cs
  2. +9
    -7
      src/Discord.Net/Net/Rest/JsonRestClient.cs
  3. +6
    -1
      src/Discord.Net/Net/WebSockets/GatewaySocket.cs

+ 6
- 1
src/Discord.Net.Audio/Net/VoiceSocket.cs View File

@@ -10,6 +10,7 @@ using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@@ -402,7 +403,11 @@ namespace Discord.Net.WebSockets
protected override async Task ProcessMessage(string json) protected override async Task ProcessMessage(string json)
{ {
await base.ProcessMessage(json).ConfigureAwait(false); await base.ProcessMessage(json).ConfigureAwait(false);
var msg = JsonConvert.DeserializeObject<WebSocketMessage>(json);

WebSocketMessage msg;
using (var reader = new JsonTextReader(new StringReader(json)))
msg = _serializer.Deserialize(reader, typeof(WebSocketMessage)) as WebSocketMessage;
var opCode = (OpCodes)msg.Operation; var opCode = (OpCodes)msg.Operation;
switch (opCode) switch (opCode)
{ {


+ 9
- 7
src/Discord.Net/Net/Rest/JsonRestClient.cs View File

@@ -1,22 +1,23 @@
using Discord.Logging; using Discord.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.IO;


namespace Discord.Net.Rest namespace Discord.Net.Rest
{ {
public class JsonRestClient : RestClient public class JsonRestClient : RestClient
{ {
private JsonSerializerSettings _deserializeSettings;
private JsonSerializer _serializer;


public JsonRestClient(DiscordConfig config, string baseUrl, Logger logger) public JsonRestClient(DiscordConfig config, string baseUrl, Logger logger)
: base(config, baseUrl, logger) : base(config, baseUrl, logger)
{ {
_deserializeSettings = new JsonSerializerSettings();
_serializer = new JsonSerializer();
#if TEST_RESPONSES #if TEST_RESPONSES
_deserializeSettings.CheckAdditionalContent = true;
_deserializeSettings.MissingMemberHandling = MissingMemberHandling.Error;
_serializer.CheckAdditionalContent = true;
_serializer.MissingMemberHandling = MissingMemberHandling.Error;
#else #else
_deserializeSettings.CheckAdditionalContent = false;
_deserializeSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
_serializer.CheckAdditionalContent = false;
_serializer.MissingMemberHandling = MissingMemberHandling.Ignore;
#endif #endif
} }


@@ -31,7 +32,8 @@ namespace Discord.Net.Rest
if (string.IsNullOrEmpty(json)) if (string.IsNullOrEmpty(json))
throw new Exception("API check failed: Response is empty."); throw new Exception("API check failed: Response is empty.");
#endif #endif
return JsonConvert.DeserializeObject<T>(json, _deserializeSettings);
using (var reader = new JsonTextReader(new StringReader(json)))
return (T)_serializer.Deserialize(reader, typeof(T));
} }
} }
} }

+ 6
- 1
src/Discord.Net/Net/WebSockets/GatewaySocket.cs View File

@@ -7,6 +7,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;


@@ -103,7 +104,11 @@ namespace Discord.Net.WebSockets
protected override async Task ProcessMessage(string json) protected override async Task ProcessMessage(string json)
{ {
base.ProcessMessage(json).GetAwaiter().GetResult(); //This is just a CompletedTask, and we need to avoid asyncs in here base.ProcessMessage(json).GetAwaiter().GetResult(); //This is just a CompletedTask, and we need to avoid asyncs in here
var msg = JsonConvert.DeserializeObject<WebSocketMessage>(json);

WebSocketMessage msg;
using (var reader = new JsonTextReader(new StringReader(json)))
msg = _serializer.Deserialize(reader, typeof(WebSocketMessage)) as WebSocketMessage;
if (msg.Sequence.HasValue) if (msg.Sequence.HasValue)
_lastSequence = msg.Sequence.Value; _lastSequence = msg.Sequence.Value;




Loading…
Cancel
Save