ObjectPath 1.0.6

dotnet add package ObjectPath --version 1.0.6
NuGet\Install-Package ObjectPath -Version 1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ObjectPath" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ObjectPath --version 1.0.6
#r "nuget: ObjectPath, 1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ObjectPath as a Cake Addin
#addin nuget:?package=ObjectPath&version=1.0.6

// Install ObjectPath as a Cake Tool
#tool nuget:?package=ObjectPath&version=1.0.6

ObjectPath

ObjectPath: A simple and intuitive library for accessing object values using string path expressions in .NET.

Installation

Install the ObjectPath library via NuGet:

dotnet add package ObjectPath

Usage

ObjectPath allows you to access values within objects using string path expressions.

Basic Usage

using ObjectPathLibrary;

// Example object
var obj = new 
{
    Name = "John",
    Age = 30,
    Address = new 
    {
        City = "New York",
        Street = "123 Main St"
    }
};

// Access values using path expressions
var name = obj.GetValueByPath("Name"); // John
var city = obj.GetValueByPath("Address.City"); // New York

JSON Example

using System.Text.Json;
using ObjectPathLibrary;

// JSON string
var json = @"
{
    ""name"": ""John"",
    ""age"": 30,
    ""address"": {
        ""city"": ""New York"",
        ""street"": ""123 Main St""
    }
}";

var jsonDocument = JsonDocument.Parse(json);
var jsonElement = jsonDocument.RootElement;

// Access values using path expressions
var name = jsonElement.GetValueByPath("name"); // John
var street = jsonElement.GetValueByPath("address.street"); // 123 Main St

Array Example

ObjectPath supports accessing array and list elements using index notation.

using ObjectPathLibrary;

// Example object with arrays and lists
var obj = new 
{
    Numbers = new int[] { 1, 2, 3, 4, 5 },
    People = new[]
    {
        new { Name = "John", Age = 30 },
        new { Name = "Jane", Age = 25 }
    }
};

// Access array elements
var firstNumber = obj.GetValueByPath("Numbers[0]"); // 1
var secondPersonName = obj.GetValueByPath("People[1].Name"); // Jane

Case Sensitivity

By default, ObjectPath is case-insensitive. You can enable case sensitivity by setting ignoreCase to false.

var obj = new 
{
    Name = "John",
    age = 30
};

var name = ObjectPath.GetValue(obj, "Name", ignoreCase: false); // John
var age = ObjectPath.GetValue(obj, "age", ignoreCase: false); // 30

// Throws InvalidObjectPathException
var invalid = ObjectPath.GetValue(obj, "name", ignoreCase: false);

Dictionary Access

You can also access values in dictionaries using path expressions.

using ObjectPathLibrary;
using Xunit;

[Fact]
public void DictionaryAccessTest()
{
    var dic = new Dictionary<string, object>
    {
        ["Name"] = "John",
        ["Age"] = 30,
        ["Address"] = new Dictionary<string, object>
        {
            ["City"] = "New York",
            ["Street"] = "123 Main St"
        }
    };

    // Act
    var name = dic.GetValueByPath("Name");
    var age = dic.GetValueByPath("Age");
    var city = dic.GetValueByPath("Address.City");
    var street = dic.GetValueByPath("Address.Street");

    var address = dic.GetValueByPath("Address") as IDictionary<string, object>;
    var addressExpando = address!.ToExpando();

    // Assert
    Assert.Equal("John", name);
    Assert.Equal(30, age);
    Assert.Equal("New York", city);
    Assert.Equal("123 Main St", street);

    Assert.Equal("New York", address["City"]);
    Assert.Equal("123 Main St", address["Street"]);

    Assert.Equal("New York", addressExpando?.City);
    Assert.Equal("123 Main St", addressExpando?.Street);
}

Handling Exceptions

Use GetValueByPathOrNull to safely get values without throwing exceptions.

using ObjectPathLibrary;

var obj = new 
{
    Name = "John",
    Address = new 
    {
        City = "New York"
    }
};

// Access values using path expressions
var name = obj.GetValueByPathOrNull("Name"); // John
var nonExistent = obj.GetValueByPathOrNull("NonExistentProperty"); // null

ObjectPathExtensions.cs

namespace ObjectPathLibrary
{
    public static class ObjectPathExtensions
    {
        public static object? GetValueByPath(this object obj, string path)
        {
            return ObjectPath.GetValue(obj, path);
        }

        public static object? GetValueByPathOrNull(this object obj, string path)
        {
            try
            {
                return ObjectPath.GetValue(obj, path);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}

Features

  • Access nested properties and fields.
  • Supports JSON elements.
  • Supports array and list index access.
  • Case-insensitive by default, with an option for case sensitivity.
  • Handles complex nested structures.
  • Provides safe access with GetValueByPathOrNull.
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 77 6/5/2024
1.0.5 52 6/5/2024
1.0.4 52 6/5/2024
1.0.3 52 6/5/2024
1.0.2 62 6/5/2024
1.0.1 55 6/4/2024