Skip to content

Commit

Permalink
v1.2.16
Browse files Browse the repository at this point in the history
- removed old *Mono* patches for JSON formatting
  • Loading branch information
genemars committed Sep 6, 2023
1 parent d892c9d commit 886c386
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 65 deletions.
2 changes: 1 addition & 1 deletion MIG/Gateways/WebSocketGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
65 changes: 1 addition & 64 deletions MIG/Utility/Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}

}
}

0 comments on commit 886c386

Please sign in to comment.