{"id":25002088,"url":"https://github.com/siberaindustries/extensions.dictionary","last_synced_at":"2025-04-12T11:06:33.654Z","repository":{"id":64523739,"uuid":"208294027","full_name":"SiberaIndustries/Extensions.Dictionary","owner":"SiberaIndustries","description":"A library to convert a dictionary out of a given object or an object out of a dictionary.","archived":false,"fork":false,"pushed_at":"2023-07-29T00:33:57.000Z","size":156,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-03T18:52:46.025Z","etag":null,"topics":["converter","csharp","dictionary","extensions","netstandard"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SiberaIndustries.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-13T15:33:15.000Z","updated_at":"2025-01-18T16:30:04.000Z","dependencies_parsed_at":"2023-02-18T01:31:24.516Z","dependency_job_id":null,"html_url":"https://github.com/SiberaIndustries/Extensions.Dictionary","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SiberaIndustries%2FExtensions.Dictionary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SiberaIndustries%2FExtensions.Dictionary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SiberaIndustries%2FExtensions.Dictionary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SiberaIndustries%2FExtensions.Dictionary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SiberaIndustries","download_url":"https://codeload.github.com/SiberaIndustries/Extensions.Dictionary/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237197251,"owners_count":19270549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["converter","csharp","dictionary","extensions","netstandard"],"created_at":"2025-02-04T21:23:40.211Z","updated_at":"2025-02-04T21:23:41.309Z","avatar_url":"https://github.com/SiberaIndustries.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Extensions.Dictionary\n\n[![NuGet](https://img.shields.io/nuget/v/Extensions.Dictionary.svg)](https://www.nuget.org/packages/Extensions.Dictionary)\n[![.NET Core](https://github.com/SiberaIndustries/Extensions.Dictionary/workflows/.NET%20Core/badge.svg)](https://github.com/SiberaIndustries/Extensions.Dictionary/actions?query=workflow%3A%22.NET+Core%22)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=SiberaIndustries_Extensions.Dictionary\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=SiberaIndustries_Extensions.Dictionary)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=SiberaIndustries_Extensions.Dictionary\u0026metric=coverage)](https://sonarcloud.io/dashboard?id=SiberaIndustries_Extensions.Dictionary)\n\n## Introduction\n\nThe easiest way to start using `Extensions.Dictionary` is to install it’s Nuget package. In Visual Studio open Package Manager console and type the following:\n\n```cs\nInstall-Package Extensions.Dictionary\n```\n\nIn the source file where you will be using Dictionary Extensions import the namespace:\n\n```cs\nusing Extensions.Dictionary;\n```\n\nSample class:\n\n```cs\npublic class Person\n{\n    [DataMember(Name = \"Name1\")]\n    public string Firstname { get; set; }\n\n    [IgnoreDataMember]\n    public string Lastname { get; set; }\n}\n```\n\nConvert an instance to a dictionary:\n\n```cs\nvar person = new Person { Firstname = \"foo\", Lastname = \"bar\" };\n\n// Option 1: Convert all public properties and instance fields\nvar dictionary1 = person.ToDictionary(); \n\n// Option 2: Same as option 1 + respect DataContract attributes (DataMember / IgnoreDataMember)\nvar settings = new ConverterSettings { Resolver = new DataContractResolver() };\nvar dictionary2 = person.ToDictionary(settings);\n\n// dictionary1: \u003cFirstname = foo; Lastname = bar\u003e\n// dictionary2: \u003cName1 = foo\u003e\n```\n\nConvert a dictionary back to it's typed instance:\n\n```cs\n// Option 1\nvar person1 = dictionary.ToInstance\u003cPerson\u003e();\n\n// Option 2\nvar settings = new ConverterSettings { Resolver = new DataContractResolver() };\nvar person2 = dictionary.ToInstance\u003cPerson\u003e(settings);\n```\n\n## Extensibility\n\n### Custom MemberConverter\n\nThis sample creates a custom converter from `MemberConverter\u003cT\u003e` that overrides conversion for the `System.Numerics.Vector4` class.\n\n```cs\npublic class Vector4Converter : MemberConverter\u003cVector4\u003e\n{\n    public override IDictionary\u003cstring, object\u003e ToDictionary(Vector4 value, ConverterSettings settings)\n    {\n        return new Dictionary\u003cstring, object\u003e(4)\n        {\n            { nameof(Vector4.W), value.W },\n            { nameof(Vector4.X), value.X },\n            { nameof(Vector4.Y), value.Y },\n            { nameof(Vector4.Z), value.Z },\n        };\n    }\n\n    public override Vector4 ToInstance(IDictionary\u003cstring, object?\u003e value, ConverterSettings settings)\n    {\n        return new Vector4\n        {\n            W = float.Parse(value[nameof(Vector4.W)]?.ToString(), settings.Culture),\n            X = float.Parse(value[nameof(Vector4.X)]?.ToString(), settings.Culture),\n            Y = float.Parse(value[nameof(Vector4.Y)]?.ToString(), settings.Culture),\n            Z = float.Parse(value[nameof(Vector4.Z)]?.ToString(), settings.Culture),\n        };\n    }\n}\n```\n\n```cs\n// Usage\nvar plane = new Plane(1f, 1f, 1f, .1f);\n\nvar settings = new ConverterSettings();\nsettings.Converters.Add(new Vector4Converter());\n\nvar dict = plane.ToDictionary(settings);\n```\n\n### Custom Resolver\n\nIt is possible to customize the member name / value extraction. Just implement the provided `ISerializerResolver` interface. The following code snipped shows an example of a [Json.NET](https://www.newtonsoft.com/json) resolver:\n\n```cs\npublic class JsonNetResolver : ISerializerResolver\n{\n    public string GetMemberName(MemberInfo memberInfo)\n    {\n        if (memberInfo == null)\n        {\n            throw new ArgumentNullException(nameof(memberInfo));\n        }\n\n        var attribute = memberInfo.GetCustomAttribute\u003cJsonPropertyAttribute\u003e();\n        return attribute?.PropertyName ?? memberInfo.Name;\n    }\n\n    public object GetMemberValue(MemberInfo memberInfo, object instance)\n    {\n        switch (memberInfo?.MemberType)\n        {\n            case MemberTypes.Property:\n                return ((PropertyInfo)memberInfo).GetValue(instance);\n            case MemberTypes.Field:\n                return ((FieldInfo)memberInfo).GetValue(instance);\n            case null:\n                throw new ArgumentNullException(nameof(memberInfo));\n            default:\n                throw new NotSupportedException(memberInfo.MemberType + \" not supported.\");\n        }\n    }\n\n    public MemberInfo[] GetMemberInfos(Type type)\n    {\n        return type?\n            .GetProperties(BindingFlags.Instance | BindingFlags.Public)\n            .Cast\u003cMemberInfo\u003e()\n            .Concat(type.GetFields(BindingFlags.Instance | BindingFlags.Public))\n            .Where(x =\u003e !x.GetCustomAttributes\u003cJsonIgnoreAttribute\u003e().Any())\n            ?? Array.Empty\u003cType\u003e();\n    }\n}\n```\n\n## Benchmarks\n\nThe following benchmarks are showing the differences between converting from dictionary to instance and from instance to dictionary. SPOILER: You get the best efficiency with the `DefaultResolver`.\n\n**Test environment**\n\n``` ini\nBenchmarkDotNet=v0.12.0, OS=Windows 10.0.18362\nIntel Core i7-6700HQ CPU 2.60GHz (Skylake), 1 CPU, 8 logical and 8 physical cores\n.NET Core SDK=3.0.100\n  [Host]   : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), X64 RyuJIT\n  ShortRun : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), X64 RyuJIT \n```\n\n### Convert to dictionary\n\n|                              Method |   N |      Mean |     Error |    StdDev | Ratio | RatioSD | Rank |  Gen 0 | Gen 1 | Gen 2 | Allocated |\n|------------------------------------ |---- |----------:|----------:|----------:|------:|--------:|-----:|-------:|------:|------:|----------:|\n|                     DefaultResolver |   1 |  6.277 us | 12.242 us | 0.6710 us |  1.00 |    0.00 |    1 | 0.5875 |     - |     - |   1.81 KB |\n|                DataContractResolver |   1 | 16.106 us | 18.827 us | 1.0320 us |  2.59 |    0.35 |    3 | 0.9918 |     - |     - |   3.07 KB |\n| DataContractResolver-IgnoreAncestors |   1 | 12.773 us |  8.038 us | 0.4406 us |  2.05 |    0.15 |    2 | 0.9766 |     - |     - |   3.07 KB |\n|                     JsonNetResolver |   1 | 50.861 us | 56.199 us | 3.0805 us |  8.14 |    0.56 |    4 | 5.8594 |     - |     - |     18 KB |\n|                                     |     |           |           |           |       |         |      |        |       |       |           |\n|                     DefaultResolver |  10 |  5.572 us |  1.225 us | 0.0672 us |  1.00 |    0.00 |    1 | 0.5875 |     - |     - |   1.81 KB |\n|                DataContractResolver |  10 | 14.726 us | 33.944 us | 1.8606 us |  2.64 |    0.32 |    3 | 0.9918 |     - |     - |   3.07 KB |\n| DataContractResolver-IgnoreAncestors |  10 | 12.287 us |  1.493 us | 0.0818 us |  2.21 |    0.03 |    2 | 0.9918 |     - |     - |   3.07 KB |\n|                     JsonNetResolver |  10 | 52.918 us | 89.842 us | 4.9246 us |  9.50 |    1.00 |    4 | 5.8594 |     - |     - |     18 KB |\n|                                     |     |           |           |           |       |         |      |        |       |       |           |\n|                     DefaultResolver | 100 |  6.063 us |  4.733 us | 0.2595 us |  1.00 |    0.00 |    1 | 0.5875 |     - |     - |   1.81 KB |\n|                DataContractResolver | 100 | 12.419 us |  4.004 us | 0.2195 us |  2.05 |    0.06 |    2 | 0.9918 |     - |     - |   3.07 KB |\n| DataContractResolver-IgnoreAncestors | 100 | 12.413 us | 13.096 us | 0.7179 us |  2.05 |    0.20 |    2 | 0.9918 |     - |     - |   3.07 KB |\n|                     JsonNetResolver | 100 | 54.870 us | 36.022 us | 1.9745 us |  9.06 |    0.50 |    3 | 5.8594 |     - |     - |     18 KB |\n\n### Convert to instance\n\n|                              Method |   N |        Mean |        Error |     StdDev |  Ratio | RatioSD | Rank |   Gen 0 | Gen 1 | Gen 2 | Allocated |\n|------------------------------------ |---- |------------:|-------------:|-----------:|-------:|--------:|-----:|--------:|------:|------:|----------:|\n|                     DefaultResolver |   1 |    10.76 us |     0.491 us |   0.027 us |   1.00 |    0.00 |    1 |  0.7019 |     - |     - |   2.18 KB |\n|                DataContractResolver |   1 |    53.33 us |    61.631 us |   3.378 us |   4.96 |    0.32 |    3 |  3.7842 |     - |     - |  11.72 KB |\n| DataContractResolver-IgnoreAncestors |   1 |    54.74 us |    99.851 us |   5.473 us |   5.09 |    0.51 |    4 |  3.7842 |     - |     - |  11.72 KB |\n|                     JsonNetResolver |   1 |   161.86 us |    94.736 us |   5.193 us |  15.05 |    0.46 |    5 | 16.6016 |     - |     - |  51.52 KB |\n|                         PureJsonNet |   1 |    18.55 us |    24.079 us |   1.320 us |   1.72 |    0.12 |    2 |  2.4414 |     - |     - |   7.49 KB |\n|                                     |     |             |              |            |        |         |      |         |       |       |           |\n|                     DefaultResolver |  10 |    11.64 us |    18.704 us |   1.025 us |   1.00 |    0.00 |    1 |  0.7019 |     - |     - |   2.18 KB |\n|                DataContractResolver |  10 |    64.32 us |   118.677 us |   6.505 us |   5.53 |    0.42 |    3 |  3.7842 |     - |     - |  11.72 KB |\n| DataContractResolver-IgnoreAncestors |  10 |    54.44 us |    22.665 us |   1.242 us |   4.70 |    0.40 |    2 |  3.7842 |     - |     - |  11.72 KB |\n|                     JsonNetResolver |  10 |   169.81 us |   208.025 us |  11.403 us |  14.66 |    1.68 |    5 | 16.6016 |     - |     - |  51.52 KB |\n|                         PureJsonNet |  10 |   163.40 us |   185.507 us |  10.168 us |  14.10 |    1.48 |    4 | 11.2305 |     - |     - |  35.09 KB |\n|                                     |     |             |              |            |        |         |      |         |       |       |           |\n|                     DefaultResolver | 100 |    13.51 us |    13.977 us |   0.766 us |   1.00 |    0.00 |    1 |  0.7019 |     - |     - |   2.18 KB |\n|                DataContractResolver | 100 |    64.19 us |    64.332 us |   3.526 us |   4.76 |    0.36 |    3 |  3.7842 |     - |     - |  11.72 KB |\n| DataContractResolver-IgnoreAncestors | 100 |    56.80 us |    92.891 us |   5.092 us |   4.20 |    0.28 |    2 |  3.7842 |     - |     - |  11.72 KB |\n|                     JsonNetResolver | 100 |   138.36 us |    34.188 us |   1.874 us |  10.26 |    0.44 |    4 | 16.6016 |     - |     - |  51.52 KB |\n|                         PureJsonNet | 100 | 1,858.79 us | 6,022.705 us | 330.125 us | 137.79 |   24.91 |    5 | 93.7500 |     - |     - | 292.02 KB |\n\n## Open Source License Acknowledgements and Third-Party Copyrights\n\n- Icon made by [Freepik](https://www.flaticon.com/authors/freepik)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiberaindustries%2Fextensions.dictionary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsiberaindustries%2Fextensions.dictionary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiberaindustries%2Fextensions.dictionary/lists"}