Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Root.Core.Models.Configuration
- {
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Diagnostics;
- using Newtonsoft.Json;
- using Deliverystack.Core.Attributes;
- using Deliverystack.Core.Configuration;
- using Root.Core.Models.Fields;
- public class SerializationConfigurator : IConfigureSerialization
- {
- private List<JsonConverter> _jsonConverters = null;
- public void ConfigureSerialization(JsonSerializerSettings settings)
- {
- if (_jsonConverters == null)
- {
- _jsonConverters = new List<JsonConverter>();
- foreach (Type type in AutoLoadJsonConverter.GetTypesWithAttribute(typeof(AutoLoadJsonConverter)))
- {
- if (AutoLoadJsonConverter.IsEnabledForType(type))
- {
- _jsonConverters.Add((JsonConverter)Activator.CreateInstance(type));
- }
- }
- foreach (Type blockType in ModularBlockBase.Implementations)
- {
- //TODO: since they all inherit, this doesn't require reflection - just create them as the base class and call the method?
- MethodInfo methodInfo = blockType.GetRuntimeMethod("GetJsonConverter", new Type[0]);
- Trace.Assert(methodInfo != null, "GetJsonConverter() not found in " + blockType);
- object obj = Activator.CreateInstance(blockType);
- JsonConverter jsonConverter = methodInfo.Invoke(obj, new object[0]) as JsonConverter;
- Trace.Assert(jsonConverter != null, "No JsonConverter for " + blockType);
- _jsonConverters.Add(jsonConverter);
- }
- }
- foreach (JsonConverter jsonConverter in _jsonConverters)
- {
- settings.Converters.Add(jsonConverter);
- }
- }
- }
- }
RAW Paste Data