JsonExts.JsonPath 1.0.1

Adds support to Newtonsoft.Json for mapping properties using JSONPath expresions.

Install-Package JsonExts.JsonPath -Version 1.0.1
dotnet add package JsonExts.JsonPath --version 1.0.1
<PackageReference Include="JsonExts.JsonPath" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonExts.JsonPath --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Add [JsonPath(string)] to properties of your model and add [JsonConverter(typeof(JsonPathObjectConverter))] to your model.

Here's an example model:

[JsonConverter(typeof(JsonPathObjectConverter))]
public class Book
{
    [JsonPath("price")]
    public double Price { get; set; }

    [JsonPath("info.author")]
    public string Author { get; set; }

    [JsonPath("info.related[0].title")]
    public string FirstRelatedTitle { get; set; }

    [JsonPath("info.related[*].title")]
    public List<string> RelatedTitles { get; set; }
}

Example JSON being deserialized:

{
    "price": 10.0,
    "info": {
        "title": "Leviathan Wakes",
        "author": "James S. A. Corey",
        "related": [
            { "title": "Caliban's War" },
            { "title": "Abaddon's Gate" },
            { "title": "Cibola Burn" },
            { "title": "Nemesis Games" },
            { "title": "Babylon's Ashes" },
            { "title": "Persepolis Rising" },
            { "title": "Tiamat's Wrath" }
        ]
    }
}

Deserialize the JSON to your model like normal using JsonConvert.DeserializeObject() or a JsonSerializer.

var book = JsonConvert.DeserializeObject<Book>(json);

Alternatively you can leave out the [JsonConverter] attribute on the model and instead specify it when deserializing (or on JsonSerializerSettings).

public class Book 
{
    ...
}

// Add JsonPathObjectConverter to the collection of converters used
var book = JsonConvert.DeserializeObject<Book>(json, new JsonPathObjectConverter())

Serializing book back to JSON again will then result in this:

{
    "price": 10.0,
    "author": "James S. A. Corey",
    "firstRelatedTitle": "Caliban's War",
    "relatedTitles": [
        "Caliban's War", 
        "Abaddon's Gate", 
        "Cibola Burn", 
        "Nemesis Games", 
        "Babylon's Ashes", 
        "Persepolis Rising", 
        "Tiamat's Wrath"
    ]
}

Supported Features

  • [JsonConverter]

    • Supported on both regular properties and combined with [JsonPath].
  • [DefaultValue]

    • Supported on both regular properties and combined with [JsonPath].
  • [JsonIgnore]

    • Only supported on regular properties, not supported combined with [JsonPath].
  • [JsonProperty] features supported when combined with [JsonPath]:

    • [X] ItemConverterType
    • [X] NullValueHandling
    • [X] DefaultValueHandling
    • [ ] NamingStrategyType
    • [ ] MissingMemberHandling
    • [ ] ReferenceLoopHandling
    • [ ] ObjectCreationHandling
    • [ ] TypeNameHandling
    • [ ] Required

Add [JsonPath(string)] to properties of your model and add [JsonConverter(typeof(JsonPathObjectConverter))] to your model.

Here's an example model:

[JsonConverter(typeof(JsonPathObjectConverter))]
public class Book
{
    [JsonPath("price")]
    public double Price { get; set; }

    [JsonPath("info.author")]
    public string Author { get; set; }

    [JsonPath("info.related[0].title")]
    public string FirstRelatedTitle { get; set; }

    [JsonPath("info.related[*].title")]
    public List<string> RelatedTitles { get; set; }
}

Example JSON being deserialized:

{
    "price": 10.0,
    "info": {
        "title": "Leviathan Wakes",
        "author": "James S. A. Corey",
        "related": [
            { "title": "Caliban's War" },
            { "title": "Abaddon's Gate" },
            { "title": "Cibola Burn" },
            { "title": "Nemesis Games" },
            { "title": "Babylon's Ashes" },
            { "title": "Persepolis Rising" },
            { "title": "Tiamat's Wrath" }
        ]
    }
}

Deserialize the JSON to your model like normal using JsonConvert.DeserializeObject() or a JsonSerializer.

var book = JsonConvert.DeserializeObject<Book>(json);

Alternatively you can leave out the [JsonConverter] attribute on the model and instead specify it when deserializing (or on JsonSerializerSettings).

public class Book 
{
    ...
}

// Add JsonPathObjectConverter to the collection of converters used
var book = JsonConvert.DeserializeObject<Book>(json, new JsonPathObjectConverter())

Serializing book back to JSON again will then result in this:

{
    "price": 10.0,
    "author": "James S. A. Corey",
    "firstRelatedTitle": "Caliban's War",
    "relatedTitles": [
        "Caliban's War", 
        "Abaddon's Gate", 
        "Cibola Burn", 
        "Nemesis Games", 
        "Babylon's Ashes", 
        "Persepolis Rising", 
        "Tiamat's Wrath"
    ]
}

Supported Features

  • [JsonConverter]

    • Supported on both regular properties and combined with [JsonPath].
  • [DefaultValue]

    • Supported on both regular properties and combined with [JsonPath].
  • [JsonIgnore]

    • Only supported on regular properties, not supported combined with [JsonPath].
  • [JsonProperty] features supported when combined with [JsonPath]:

    • [X] ItemConverterType
    • [X] NullValueHandling
    • [X] DefaultValueHandling
    • [ ] NamingStrategyType
    • [ ] MissingMemberHandling
    • [ ] ReferenceLoopHandling
    • [ ] ObjectCreationHandling
    • [ ] TypeNameHandling
    • [ ] Required

Release Notes

Fixed issue with non-value type properties when path or parts of path was missing

NuGet packages (1)

Showing the top 1 NuGet packages that depend on JsonExts.JsonPath:

Package Downloads
ErgastApiClient
A client for querying the Ergast Developer API (http://ergast.com/mrd/)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.1 103 4/14/2020
1.0.0 205 7/16/2019