Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better constructor support for deserialization #32

Merged

Conversation

tcortega
Copy link
Contributor

@tcortega tcortega commented Jul 10, 2023

The behaviour in the mechanism is based on the behaviour observed on the System.Text.Json library, when attempting to deserialize JSON strings.

  • Introduced a mechanism for dynamically identifying and invoking the most suitable constructor for a given type, making it able to call any constructor with N parameters. This specially useful when dealing with records. Because most people declare records as following: record MyRecord(string MyString, ...)
  • If there are matching keys between the TOML string and the constructor's parameters, it initializes the properties from the parameters.
  • Introduced a TomlSerializerOptions class with an OverrideConstructorValues option.

if there is anything wrong with it, let me know or feel free to change it yourself.

@tcortega tcortega force-pushed the feat/constructor-initialization branch from 22a4cce to 74ec918 Compare July 10, 2023 05:19
@coveralls
Copy link

coveralls commented Jul 12, 2023

Coverage Status

coverage: 92.162% (-0.2%) from 92.402% when pulling 619ebc0 on tcortega:feat/constructor-initialization into 729f57b on SamboyCoding:master.

Tomlet/Extensions/ReflectionExtensions.cs Outdated Show resolved Hide resolved
Tomlet/Extensions/StringExtensions.cs Outdated Show resolved Hide resolved
Tomlet/GenericExtensions.cs Outdated Show resolved Hide resolved
Tomlet/TomlCompositeDeserializer.cs Outdated Show resolved Hide resolved
Tomlet/TomlCompositeDeserializer.cs Outdated Show resolved Hide resolved
Tomlet/TomlCompositeSerializer.cs Outdated Show resolved Hide resolved
Tomlet/TomlSerializationMethods.cs Outdated Show resolved Hide resolved
Tomlet/TomlSerializationMethods.cs Outdated Show resolved Hide resolved
Tomlet/TomlSerializationMethods.cs Show resolved Hide resolved
Tomlet/TomletMain.cs Outdated Show resolved Hide resolved
The behaviour in the mechanism is based on the behaviour observed on the System.Text.Json library, when attempting to deserialize JSON strings.

Introduced a mechanism for dynamically identifying and invoking the most suitable constructor for a given type, making it able to call any constructor with N parameters. This specially useful when dealing with records. Because most people declare records as following: record MyRecord(string MyString, ...)
If there are matching keys between the TOML string and the constructor's parameters, it initializes the properties from the parameters.
Introduced a TomlSerializerOptions class with an OverrideConstructorValues option.
@tcortega tcortega force-pushed the feat/constructor-initialization branch from 74ec918 to 619ebc0 Compare July 16, 2023 16:57
@tcortega
Copy link
Contributor Author

When I moved the GenericExtensions as you said, to the Extensions folder, there seemed to be an issue with using the Extension methods in the TomlCompositeDeserializer.cs, an error saying.

Ambiguous invocation:   Tomlet.Attributes.TomlNonSerializedAttribute GetCustomAttribute<TomlNonSerializedAttribute>(this System.Reflection.MemberInfo) (in class CustomAttributeExtensions)   Tomlet.Attributes.TomlNonSerializedAttribute? GetCustomAttribute<TomlNonSerializedAttribute>(this System.Reflection.MemberInfo) (in class GenericExtensions) match

Therefore it is now calling the method in the static class directly.

@SamboyCoding SamboyCoding merged commit 811b323 into SamboyCoding:master Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants