serialization - SignalR not serializing NodaTime types despite JsonSerializer settings -
i having serialization issues (exceptions) nodatime types , signalr parameters such
error converting value type 'nodatime.zoneddatetime error converting value \"2016-06-01t18:33:36.7279685+01 europe/london\" type 'nodatime.zoneddatetime'. path '[0].datecreated', line 1, position 79.
this despite following docs , replacing default jsonserializer , using nodatime extension methods , json.net nuget package e.g.
jsonserializersettings js = new jsonserializersettings(); js.configurefornodatime(datetimezoneproviders.tzdb); var serializer = jsonserializer.create(js); globalhost.dependencyresolver.register(typeof(jsonserializer), () => serializer);
happily found workaround here brannonking
essentially uses customer resolver signalr parameters uses correct serializer instead of creating default.
also referenced on so here of course found once had started post own question ;)
reposting here others googling (the excellent) nodatime specifically, , share other serialization fixes needed, such :
newtonsoft.json.jsonserializationexception: self referencing loop detected property x type y path z
server startup
public void configuration(iappbuilder app) { jsonserializersettings js = new jsonserializersettings(); js.configurefornodatime(datetimezoneproviders.tzdb); js.dateparsehandling = dateparsehandling.none; js.referenceloophandling = referenceloophandling.serialize; js.preservereferenceshandling = preservereferenceshandling.objects; var serializer = jsonserializer.create(js); globalhost.dependencyresolver.register(typeof(jsonserializer), () => serializer); var resolver = new resolver(serializer); globalhost.dependencyresolver.register(typeof(iparameterresolver), () => resolver); }
custom parameter resolver
public class resolver : defaultparameterresolver { private readonly jsonserializer _serializer; public resolver(jsonserializer serializer) { _serializer = serializer; } private fieldinfo _valuefield; public override object resolveparameter(parameterdescriptor descriptor, microsoft.aspnet.signalr.json.ijsonvalue value) { if(value.gettype() == descriptor.parametertype) { return value; } if(_valuefield == null) _valuefield = value.gettype().getfield("_value", bindingflags.instance | bindingflags.nonpublic); var json = (string)_valuefield.getvalue(value); using(var reader = new stringreader(json)) return _serializer.deserialize(reader, descriptor.parametertype); } }
many brannon !