示例#1
0
 public CollectionConverter(
     CollectionCreator createCollection,
     ITomlSerializerContext context)
 {
     _createCollection = createCollection;
     _itemConverter    = context.GetConverter <TItem>();
 }
示例#2
0
        public TomlObject ConvertTo(object value, ITomlSerializerContext context)
        {
            // TODO: null handling
            var converter = context.GetConverter(value.GetType());

            return(converter.ConvertTo(value, context));
        }
示例#3
0
 public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
 {
     var(instanceType, valueType) = GetTypes(typeToConvert);
     return((ITomlConverter)Activator.CreateInstance(
                typeof(DictionaryConverter <,>).MakeGenericType(valueType, typeToConvert),
                instanceType, context) !);
 }
示例#4
0
        public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
        {
            var metadata = GetMetadata(typeToConvert, context);

            return((ITomlConverter)Activator.CreateInstance(
                       typeof(NewTypeConverter <,>).MakeGenericType(typeToConvert, metadata.InnerType),
                       metadata) !);
        }
示例#5
0
        private static NewTypeMetadata GetMetadata(Type typeToConvert, ITomlSerializerContext context)
        {
            var constructor = FindConstructor(typeToConvert);
            var innerType   = constructor.GetParameters()[0].ParameterType;
            var converter   = context.GetConverter(innerType);

            return(new NewTypeMetadata(innerType, converter, constructor, null !));
        }
示例#6
0
        public T ConvertFrom(TomlObject value, ITomlSerializerContext context)
        {
            if (value is TomlTable tomlTable)
            {
                return(ConvertFrom(tomlTable, context));
            }

            throw new TomlException("Objects can only be converted from tables");
        }
示例#7
0
        public int ConvertFrom(TomlObject value, ITomlSerializerContext context)
        {
            if (value is TomlInteger tomlInteger)
            {
                // TODO: what happens if the value overflows?
                return((int)tomlInteger.Value);
            }

            throw new TomlException();
        }
示例#8
0
        public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
        {
            var typeToCreate        = context.GetDefaultImplementation(typeToConvert) ?? typeToConvert;
            var constructor         = GetConstructorForDeserialization(typeToCreate);
            var parameterConverters = constructor.GetParameters().Select(p => GetCachedParameterInfo(p, context));

            return((ITomlConverter)Activator.CreateInstance(
                       typeof(ObjectConverter <>).MakeGenericType(typeToConvert),
                       constructor,
                       parameterConverters.ToImmutableList()) !);
        }
示例#9
0
        private TDictionary ConvertFrom(TomlTable table, ITomlSerializerContext context)
        {
            var dictionary = (TDictionary)Activator.CreateInstance(_instanceType) !;

            foreach (var key in table.Keys)
            {
                var value = table.TryGetValue(key, out var dictionaryValue)
                    ? _valueConverter.ConvertFrom(dictionaryValue, context)
                    : throw new InvalidOperationException("This should never happen");
                dictionary[key] = value;
            }

            return(dictionary);
        }
示例#10
0
        public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
        {
            var itemType = IsIEnumerable(typeToConvert)
                ? typeToConvert.GetGenericArguments()[0]
                : typeToConvert.GetInterfaces().Single(IsIEnumerable).GetGenericArguments()[0];

            var typeToCreate = context.GetDefaultImplementation(typeToConvert) ?? typeToConvert;

            var factory = CollectionCreatorFactories.FirstOrDefault(f => f.CanCreate(typeToCreate, itemType))
                          ?? throw new TomlException($"No creator found for collection {typeToCreate}");

            return((ITomlConverter)Activator.CreateInstance(
                       typeof(CollectionConverter <,>).MakeGenericType(typeToConvert, itemType),
                       factory.CreateCollectionCreator(typeToCreate, itemType),
                       context) !);
        }
示例#11
0
        public object ConvertFrom(TomlObject value, ITomlSerializerContext context)
        {
            var converter = value switch
            {
                TomlArray => (ITomlConverter)context.GetConverter <IEnumerable <object> >(),
                TomlString => context.GetConverter <string>(),
                TomlInteger => context.GetConverter <long>(),
                TomlBoolean => context.GetConverter <bool>(),
                TomlFloat => context.GetConverter <double>(),
                TomlDateTime => context.GetConverter <DateTime>(),
                TomlTable => throw new NotImplementedException(),
                      _ => throw new NotSupportedException(),
            };

            return(converter.ConvertFrom(value, context));
        }
示例#12
0
 public TomlObject ConvertTo(TDictionary value, ITomlSerializerContext context) => throw new NotImplementedException();
示例#13
0
 public TDictionary ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => value is TomlTable tomlTable
示例#14
0
 public DictionaryConverter(Type instanceType, ITomlSerializerContext context)
 {
     _valueConverter = context.GetConverter <TValue>();
     _instanceType   = instanceType;
 }
示例#15
0
 public NullableConverter(ITomlSerializerContext context) => _valueConverter = context.GetConverter <T>();
示例#16
0
 public T ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => value is TomlValue <T> tomlValue
示例#17
0
 public TomlObject ConvertTo(T value, ITomlSerializerContext context) => ToTomlObject(value);
示例#18
0
 public Guid ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => Guid.Parse(context.GetConverter <string>().ConvertFrom(value, context));
示例#19
0
 public TNewType ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => _constructor(_innerConverter.ConvertFrom(value, context));
示例#20
0
 public T ConvertFromAbsent <T>(ITomlSerializerContext context) => throw new TomlException();
示例#21
0
 public ITomlConverter CreateConverter(Type optionType, ITomlSerializerContext context)
 => (ITomlConverter)Activator.CreateInstance(
     typeof(OptionConverter <>).MakeGenericType(optionType.GetGenericArguments()),
     context) !;
示例#22
0
 public T ConvertFromAbsent <T>(ITomlSerializerContext context) => default;
示例#23
0
        private static object?ConvertParameter(TomlTable table, CachedParameterInfo parameter, ITomlSerializerContext context)
        {
            var parameterName = parameter.Info.Name ?? throw new TomlException("Constructor parameter without name");
            var propertyName  = context.ConvertPropertyName(parameterName);

            // TODO: support nullable reference types

            return(table.TryGetValue(propertyName, out var tomlObject)
                ? parameter.Converter.ConvertFrom(tomlObject, context)
                : parameter.Converter.ConvertFromAbsent(context));
        }
示例#24
0
        private T ConvertFrom(TomlTable table, ITomlSerializerContext context)
        {
            var parameters = _parameterConverters.Select(p => ConvertParameter(table, p, context)).ToArray();

            return((T)_constructor.Invoke(parameters));
        }
示例#25
0
 public OptionConverter(ITomlSerializerContext context) => _itemConverter = context.GetConverter <TItem>();
示例#26
0
 public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
 => (ITomlConverter)Activator.CreateInstance(
     typeof(NullableConverter <>).MakeGenericType(typeToConvert.GetGenericArguments()),
     context) !;
示例#27
0
 public TCollection ConvertFromAbsent(ITomlSerializerContext context)
 => (TCollection)_createCollection(Enumerable.Empty <TItem>());
示例#28
0
 public TomlObject ConvertTo(Guid value, ITomlSerializerContext context)
 => new TomlString(value.ToString());
示例#29
0
        private static CachedParameterInfo GetCachedParameterInfo(ParameterInfo info, ITomlSerializerContext context)
        {
            var converter = context.GetConverter(info.ParameterType);

            return(new CachedParameterInfo(info, converter));
        }
示例#30
0
 public TCollection ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => value is TomlArray tomlArray