public void DeserializesNewTypesWithSuitableConstructors(TomlObject input, object expectedValue) { var options = TomlSerializerOptions.Default.AddNewTypeConverter(); var value = TomlSerializer.Deserialize(input, expectedValue.GetType(), options); Assert.Equal(expectedValue, value); }
public void DeserializingATypeWithMultipleConstructorsMarkedAsTomlConstructorThrows() { var input = TomlTable.Empty .Add("A", new TomlString("Foo")) .Add("B", new TomlString("Bar")); Assert.Throws <TomlException>(() => TomlSerializer.Deserialize <ClassWithMultipleConstructorsMarkedAsTomlConstructor>(input)); }
public void DeserializingTypeWithoutSettersFromTableWithMissingKeysThrowsWhenAbsentValuesAreDisallowed() { var options = TomlSerializerOptions.Default.WithAbsentValuesPolicy(TomlAbsentValuesPolicy.Disallow); var input = TomlTable.Empty .Add(nameof(Person.FirstName), new TomlString("Foo")); Assert.Throws <TomlException>(() => TomlSerializer.Deserialize <Person>(input, options)); }
public void ListOfObjectCanBeDeserialized() { const string toml = "Dynamic = [10, 20, 30, 40]"; Assert.Equal( new object[] { 10L, 20L, 30L, 40L }, (IEnumerable <object>)TomlSerializer.Deserialize <Document>(toml).Dynamic); }
public void TypeWithoutSettersCanBeDeserializedFromTable() { var value = TomlSerializer.Deserialize <Person>( TomlTable.Empty .Add(nameof(Person.FirstName), new TomlString("Foo")) .Add(nameof(Person.LastName), new TomlString("Bar"))); Assert.Equal(new Person("Foo", "Bar"), value); }
public void FirstConstructorIsUsedWhenDeserializingTypeWithoutSettersAndMultipleConstructorsOfSameArity() { var input = TomlTable.Empty .Add(nameof(ClassWithMultipleConstructorsOfSameArity.FirstName), new TomlString("Foo")) .Add(nameof(ClassWithMultipleConstructorsOfSameArity.LastName), new TomlString("Bar")); Assert.Equal( new ClassWithMultipleConstructorsOfSameArity("Foo", "Bar"), TomlSerializer.Deserialize <ClassWithMultipleConstructorsOfSameArity>(input)); }
public void NamingPolicyIsRespectedWhenDeserializingTypeWithoutSetters() { var options = TomlSerializerOptions.Default.WithPropertyNamingPolicy(TomlNamingPolicy.SnakeCase); var value = TomlSerializer.Deserialize <Person>( TomlTable.Empty .Add("first_name", new TomlString("Foo")) .Add("last_name", new TomlString("Bar")), options); Assert.Equal(new Person("Foo", "Bar"), value); }
public void DefaultImplementationIsRespected() { var options = TomlSerializerOptions.Default.AddDefaultImplementation <IInterface, Person>(); var value = TomlSerializer.Deserialize <IInterface>( TomlTable.Empty .Add(nameof(Person.FirstName), new TomlString("Foo")) .Add(nameof(Person.LastName), new TomlString("Bar")), options); Assert.Equal(new Person("Foo", "Bar"), value); }
private static void Main() { var options = TomlSerializerOptions.Default .WithPropertyNamingPolicy(TomlNamingPolicy.SnakeCase) .WithAbsentValuesPolicy(TomlAbsentValuesPolicy.Disallow) .AddTaggedUnionConverter(TomlTaggedUnionOptions.Default.WithTagNamingPolicy(TomlNamingPolicy.SnakeCase)); const string toml = "[[email_delivery]]\n" + "type = \"null\"\n" + "[[email_delivery]]\n" + "type = \"pickup\"\ndirectory_path = \"/foo/bar\""; var configuration = TomlSerializer.Deserialize <Configuration>(toml, options); }
public void DeserializingATaggedUnionFromTomlWithInvalidTagThrows() { var options = TomlSerializerOptions.Default.AddTaggedUnionConverter(); Assert.Throws <TomlException>(() => TomlSerializer.Deserialize <EmailDelivery>("Type = \"Custom\"", options)); }
public void DeserializingATaggedUnionFromTomlWithMissingTagKeyThrows() { var options = TomlSerializerOptions.Default.AddTaggedUnionConverter(); Assert.Throws <TomlException>(() => TomlSerializer.Deserialize <EmailDelivery>(string.Empty, options)); }
public void EmptyObjectCanBeDeserializedFromEmptyTable() { var empty = TomlSerializer.Deserialize <Empty>(TomlTable.Empty); Assert.Equal(new Empty(), empty); }
public void TypeWithoutSettersCanBeDeserializedFromEmptyTable() { var value = TomlSerializer.Deserialize <Person>(TomlTable.Empty); Assert.Equal(new Person(null !, null !), value); }
public void DeserializingATypeWithNoPublicConstructorsAndNoSettersThrows() { Assert.Throws <TomlException>(() => TomlSerializer.Deserialize <TypeWithNoPublicConstructor>(TomlTable.Empty)); }
public void DeserializesTaggedUnionWithDefaultOptions(EmailDelivery expected, string toml) { var options = TomlSerializerOptions.Default.AddTaggedUnionConverter(); Assert.Equal(expected, TomlSerializer.Deserialize <EmailDelivery>(toml, options)); }
public void DeserializingATaggedUnionFromTomlWhereTagIsNotStringThrows() { var options = TomlSerializerOptions.Default.AddTaggedUnionConverter(); Assert.Throws <TomlException>(() => TomlSerializer.Deserialize <EmailDelivery>("Type = 3", options)); }
public void PrimitiveTypesCanBeDeserializedAsObject(string toml, object expected) { var value = TomlSerializer.Deserialize <Document>(toml).Dynamic; Assert.Equal(expected, value); }