diff --git a/MIG/Gateways/WebSocketGateway.cs b/MIG/Gateways/WebSocketGateway.cs index a823d86..8f783e2 100644 --- a/MIG/Gateways/WebSocketGateway.cs +++ b/MIG/Gateways/WebSocketGateway.cs @@ -202,7 +202,7 @@ public void ProcessRequest(MessageEventArgs message, WebSocketContext context) { if (responseEvent.Value is string == false) { - responseEvent.Value = JsonConvert.SerializeObject(responseEvent.Value); + responseEvent.Value = MigService.JsonSerialize(responseEvent.Value); } context.WebSocket.Send(MigService.Pack(responseEvent)); } diff --git a/MIG/Utility/Serialization.cs b/MIG/Utility/Serialization.cs index 927a386..911d2cf 100644 --- a/MIG/Utility/Serialization.cs +++ b/MIG/Utility/Serialization.cs @@ -16,11 +16,7 @@ You may obtain a copy of the License at limitations under the License. */ -using System; using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; -using System.Reflection; -using System.Globalization; namespace MIG.Utility { @@ -30,68 +26,9 @@ public static string JsonSerialize(object data, bool indent = false) { JsonSerializerSettings settings = new JsonSerializerSettings(); settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; - settings.ContractResolver = new CustomResolver(); - if (indent) - settings.Formatting = Formatting.Indented; - settings.Converters.Add(new FormattedDecimalConverter(CultureInfo.InvariantCulture)); + if (indent) settings.Formatting = Formatting.Indented; return JsonConvert.SerializeObject(data, settings); } - // Work around for "Input string was not in the correct format" when running on some mono-arm platforms - class FormattedDecimalConverter : JsonConverter - { - private CultureInfo culture; - - public FormattedDecimalConverter(CultureInfo culture) - { - this.culture = culture; - } - - public override bool CanConvert(Type objectType) - { - return (objectType == typeof(decimal) || - objectType == typeof(double) || - objectType == typeof(float)); - } - - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - writer.WriteRawValue(Convert.ToString(value, culture)); - } - - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - throw new NotImplementedException(); - } - } - - class CustomResolver : DefaultContractResolver - { - protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) - { - JsonProperty property = base.CreateProperty(member, memberSerialization); - - property.ShouldSerialize = instance => - { - try - { - PropertyInfo prop = (PropertyInfo)member; - if (prop.CanRead) - { - prop.GetValue(instance, null); - return true; - } - } - catch - { - } - return false; - }; - - return property; - } - } - } } -