Skip to content

Commit

Permalink
Websocket: Fix JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Schmitt committed Aug 1, 2016
1 parent 0736480 commit b731035
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions PoGo.NecroBot.CLI/WebSocketInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.WebSocket;
using System;

#endregion

Expand Down Expand Up @@ -159,12 +160,31 @@ public void Listen(IEvent evt, Session session)

private string Serialize(dynamic evt)
{
var jsonSerializerSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};
var jsonSerializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };

// Add custom seriaizer to convert uong to string (ulong shoud not appear to json according to json specs)
jsonSerializerSettings.Converters.Add(new IdToStringConverter());

return JsonConvert.SerializeObject(evt, Formatting.None, jsonSerializerSettings);
}
}

public class IdToStringConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken jt = JValue.ReadFrom(reader);
return jt.Value<long>();
}

public override bool CanConvert(Type objectType)
{
return typeof(System.Int64).Equals(objectType) || typeof(ulong).Equals(objectType);
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value.ToString());
}
}
}

0 comments on commit b731035

Please sign in to comment.