You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lazily resolve JsonSerializers in JsonNetSerializer
The current JsonNetSerializer implementation creates the default
serializers
within its constructor, which then calls ModifyJsonSerializerSettings.
This
means that a subclass of JsonNetSerializer cannot modify the
JsonSerializerSettings with fields set by the constructor, because the
order of
operations is:
1. Call subclass constructor with parameter A, which will set subclass
field A; field A will be used in ModifyJsonSerializerSettings.
2. Subclass constructor calls JsonNetSerializer constructor with
:base()
3. JsonNetSerializer constructor calls ModifyJsonSerializerSettings
as part of JsonSerializer.Create
4. ModifyJsonSerializerSettings makes use of default value for
field A, because it has not been set yet.
5. JsonSerializer constructor exits
6. Subclass constructor sets value of field A from parameter
A.
By using the Lazy<T> class, we can ensure that ModifyJsonSerializerSettings is
called after the subclass constructor has exited, which allows fields to
be set and thereby used in ModifyJsonSerializerSettings. The unit test
in Connection.doc.cs has been expanded to demonstrate this capability
with a (fairly trivial) use case.
protectedoverridevoidModifyJsonSerializerSettings(JsonSerializerSettingssettings)=>++CallToModify;//<1> Override ModifyJsonSerializerSettings if you need access to `JsonSerializerSettings`
0 commit comments