public CollectionConverter( CollectionCreator createCollection, ITomlSerializerContext context) { _createCollection = createCollection; _itemConverter = context.GetConverter <TItem>(); }
public TomlObject ConvertTo(object value, ITomlSerializerContext context) { // TODO: null handling var converter = context.GetConverter(value.GetType()); return(converter.ConvertTo(value, context)); }
public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context) { var(instanceType, valueType) = GetTypes(typeToConvert); return((ITomlConverter)Activator.CreateInstance( typeof(DictionaryConverter <,>).MakeGenericType(valueType, typeToConvert), instanceType, context) !); }
public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context) { var metadata = GetMetadata(typeToConvert, context); return((ITomlConverter)Activator.CreateInstance( typeof(NewTypeConverter <,>).MakeGenericType(typeToConvert, metadata.InnerType), metadata) !); }
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 !)); }
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"); }
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(); }
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()) !); }
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); }
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) !); }
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)); }
public TomlObject ConvertTo(TDictionary value, ITomlSerializerContext context) => throw new NotImplementedException();
public TDictionary ConvertFrom(TomlObject value, ITomlSerializerContext context) => value is TomlTable tomlTable
public DictionaryConverter(Type instanceType, ITomlSerializerContext context) { _valueConverter = context.GetConverter <TValue>(); _instanceType = instanceType; }
public NullableConverter(ITomlSerializerContext context) => _valueConverter = context.GetConverter <T>();
public T ConvertFrom(TomlObject value, ITomlSerializerContext context) => value is TomlValue <T> tomlValue
public TomlObject ConvertTo(T value, ITomlSerializerContext context) => ToTomlObject(value);
public Guid ConvertFrom(TomlObject value, ITomlSerializerContext context) => Guid.Parse(context.GetConverter <string>().ConvertFrom(value, context));
public TNewType ConvertFrom(TomlObject value, ITomlSerializerContext context) => _constructor(_innerConverter.ConvertFrom(value, context));
public T ConvertFromAbsent <T>(ITomlSerializerContext context) => throw new TomlException();
public ITomlConverter CreateConverter(Type optionType, ITomlSerializerContext context) => (ITomlConverter)Activator.CreateInstance( typeof(OptionConverter <>).MakeGenericType(optionType.GetGenericArguments()), context) !;
public T ConvertFromAbsent <T>(ITomlSerializerContext context) => default;
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)); }
private T ConvertFrom(TomlTable table, ITomlSerializerContext context) { var parameters = _parameterConverters.Select(p => ConvertParameter(table, p, context)).ToArray(); return((T)_constructor.Invoke(parameters)); }
public OptionConverter(ITomlSerializerContext context) => _itemConverter = context.GetConverter <TItem>();
public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context) => (ITomlConverter)Activator.CreateInstance( typeof(NullableConverter <>).MakeGenericType(typeToConvert.GetGenericArguments()), context) !;
public TCollection ConvertFromAbsent(ITomlSerializerContext context) => (TCollection)_createCollection(Enumerable.Empty <TItem>());
public TomlObject ConvertTo(Guid value, ITomlSerializerContext context) => new TomlString(value.ToString());
private static CachedParameterInfo GetCachedParameterInfo(ParameterInfo info, ITomlSerializerContext context) { var converter = context.GetConverter(info.ParameterType); return(new CachedParameterInfo(info, converter)); }
public TCollection ConvertFrom(TomlObject value, ITomlSerializerContext context) => value is TomlArray tomlArray