Coder Social home page Coder Social logo

ygoe / deepconvert Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 52 KB

Converts a data type to another data type, smarter than the standard Convert class, including collections and their items as well as object properties (duck typing). With special support for DateTime conversions.

License: MIT License

C# 100.00%
conversion convert csharp dotnet dotnet-core duck-typing iconvertible type-conversion

deepconvert's People

Contributors

ygoe avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

bubdm

deepconvert's Issues

Convert byte arrays and strings

DeepConvert should be able to convert between byte collections and strings.

This requires the specification of an Encoding. Since the ChangeType method would get too many parameters, all of them will be moved to a separate DeepConvertSettings (or similar) class that contains a property for each parameter.

The default encoding is UTF-8, like everywhere else in .NET.

Auto-detect date/time format for current dates

Each supported date/time format has a characteristic value range for "current" dates. If a date is known or expected to be current, the date format may be detected from the range of the provided numeric value.

Define what "current" means. Examples:

  • Now ± 10 years, if that's practical.
  • Interpret the number as all known formats and choose the format closest to now.

Add support for new BCL types

.NET 5 adds the following new types:

  • Half

.NET 6 adds the following new types:

  • DateOnly
  • TimeOnly

.NET 7 adds the following new types:

  • Int128
  • UInt128

Convert objects

DeepConvert should be able to convert data between the following types:

  • Dictionary<string, object> – class { … } (with same dictionary keys and properties/fields)
  • class { … } – class { … } (with same property/field names)

Property/field names should be matched case-insensitively, unless that would be ambiguous. If a property/field is itself of a composite type, the conversion recurses into that property/field.

Only public properties/fields are regarded, but the type itself should not necessarily be public.

Reflection data about the involved types should be cached for better performance. This might be a with compiled expressions or Reflection.Emit.

JsonElement support

Investigate how JsonElement instances (of System.Text.Json) can be regarded as dictionaries and converted similarly.

Add specialised ToX methods

Like the Convert class, DeepConvert should have specialised ToX methods like ToInt32, ToString, ToDictionary<TKey, TValue> etc. These methods are shortcuts to the ChangeType method to make the API a bit friendlier. Each of these methods accepts the value to convert, and optionally also a Settings object with further options (see #5).

Add more ISO 8601 date/time formats

Verify which ISO 8601 formats and variants .NET DateTime already parses. The separators ("-", ":") are optional. Additional formats include the week, day of week, and day of year notations.

Add support for TimeSpan from a period notation or a range notation of two date/time values.

Add Try… methods

If the conversion fails, an exception is thrown. As an alternative, new Try… methods should return a bool value that indicates the success of the conversion.

Use implicit/explicit cast operator if present

If the converter is running out of options, it could look for a public static op_Implicit or op_Explicit method on the source type that returns the destination type and accepts a single parameter of the source type, then call it.

See also: https://stackoverflow.com/a/32025393

The reflection result should be cached, and it should be possible to clear the cache on demand to free memory.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.