Nucs / JsonSettings

This library simplifies creating configuration for your C# app/service by utilizing the serialization capabilities of Json.NET to serialize nested (custom) objects, dictionaries and lists as simply as by creating a POCO and inheriting JsonSettings class.
MIT License
76 stars 18 forks source link

Paths.NormalizePath gives bad results on linux #3

Closed InkyQuill closed 6 years ago

InkyQuill commented 6 years ago

As it replaces all / to \, path becomes wrong on linux: mobaxterm_2018-05-25_11-18-07

Nucs commented 6 years ago

It will take me 24h to upload the fix, for the time being add event handler before calling Load to BeforeSave and BeforeLoad which passes ref string destinition which is the path that is used for saving.

e.g.

private static Settings Settings { get; } = JsonSettings.Load<Settings>("memory.jsn", ConfigSettings);

private static void ConfigSettings(Settings settings) {
    settings.BeforeLoad += (ref string destinition) => destinition = destinition.Replace('\\', '/');
    settings.BeforeSave += (ref string destinition) => destinition = destinition.Replace('\\', '/');
}

Let me know if it worked, I haven't tested the library under Linux or OSX so feel free to flood me with issues and keep me posted ! Thanks !

InkyQuill commented 6 years ago

Well, I just forked your version and rewrote the function like this:


            path = path
                .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
            if (forEquality) {
#if CROSSPLATFORM
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    path = path.Replace('/', '\\');
                    path = path.ToUpperInvariant();
                }
#else
                path = path.ToUpperInvariant();
#endif
            }

            if (path.Contains("\\") || path.Contains("/"))
                if (Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute))
                    try {
                        path = Path.GetFullPath(new Uri(path).LocalPath);
                    } catch { }
#if CROSSPLATFORM
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                //is root, fix.
                if ((path.Length == 2) && (path[1] == ':') && char.IsLetter(path[0]))
                    path = path + "\\";
            }
#endif
            return path;
        }

Everything else works like it should as it is not platform-specific.

Nucs commented 6 years ago

I've modified your version a little and uploaded it to nuget as version 1.0.6. Thanks for helping 👍