Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Frends.JSON.Query/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.2.0] - 2026-07-08
### Fixed
- Fixed issue where Options.ErrorWhenNotMatched did not throw an exception when a JSONPath filter expression (e.g. `[?(...)]`) matched no results.

## [1.1.0] - 2024-08-20
### Updated
- Updated Newtonsoft.Json library to the latest version 13.0.3.
Expand Down
18 changes: 18 additions & 0 deletions Frends.JSON.Query/Frends.JSON.Query.UnitTests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Frends.JSON.Query.Definitions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Frends.JSON.Query.UnitTests;
Expand Down Expand Up @@ -79,6 +80,23 @@ public void QueryShouldWorkWithJTokenInput()
Assert.AreEqual("Anvil", result.Data.First().ToString());
}

[TestMethod]
public void QueryShouldThrowIfOptionSetAndFilterMatchesNothing()
{
var input = new Input()
{
Json = jsonString,
Query = "$..Products[?(@.Price >= 1000)].Name"
};

var options = new Options()
{
ErrorWhenNotMatched = true,
};

Assert.ThrowsException<JsonException>(() => JSON.Query(input, options));
}

[TestMethod]
public void QueryShouldNotThrowIfOptionNotSetAndNothingIsFound()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down
9 changes: 8 additions & 1 deletion Frends.JSON.Query/Frends.JSON.Query/Query.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Frends.JSON.Query.Definitions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;

Expand Down Expand Up @@ -31,7 +33,12 @@ static JSON()
public static Result Query([PropertyTab] Input input, [PropertyTab] Options options)
{
JToken jToken = GetJTokenFromInput(input.Json);
return new Result(true, jToken.SelectTokens(input.Query, options.ErrorWhenNotMatched));
var tokens = jToken.SelectTokens(input.Query, options.ErrorWhenNotMatched).ToList();

if (tokens.Count == 0 && options.ErrorWhenNotMatched)
throw new JsonException($"No matches found for query '{input.Query}'.");

return new Result(true, tokens);
}

private static object GetJTokenFromInput(dynamic json)
Expand Down
4 changes: 4 additions & 0 deletions Frends.JSON.QuerySingle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.3.0] - 2026-07-08
### Fixed
- Fixed issue where Options.ErrorWhenNotMatched did not throw an exception when a JSONPath filter expression (e.g. `[?(...)]`) matched no results.

## [1.2.0] - 2024-11-07
### Fixed
- Fixed issue with result dotnotation by changing the result Data object type to dynamic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ public void QueryShouldThrowIfOptionSetAndNothingIsFound()
Assert.IsTrue(ex.Message.Contains("Property 'Manufacturer' does not exist on JObject."));
}

[TestMethod]
public void QuerySingleShouldThrowIfOptionSetAndFilterMatchesNothing()
{
var input = new Input()
{
Json = jsonString,
Query = "$.Manufacturers[?(@.Name == 'Nonexistent Co')]"
};

var options = new Options()
{
ErrorWhenNotMatched = true,
};

Assert.ThrowsException<JsonException>(() => JSON.QuerySingle(input, options));
}

[TestMethod]
public void QuerySingleShouldNotThrowIfOptionNotSetAndNothingIsFound()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Frends.JSON.QuerySingle.Definitions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.ComponentModel;
using System.IO;
Expand Down Expand Up @@ -31,7 +32,12 @@ static JSON()
public static Result QuerySingle([PropertyTab] Input input, [PropertyTab] Options options)
{
JToken jToken = GetJTokenFromInput(input.Json);
return new Result(true, jToken.SelectToken(input.Query, options.ErrorWhenNotMatched));
JToken result = jToken.SelectToken(input.Query, options.ErrorWhenNotMatched);

if (result == null && options.ErrorWhenNotMatched)
throw new JsonException($"No matches found for query '{input.Query}'.");

return new Result(true, result);
}

private static object GetJTokenFromInput(dynamic json)
Expand Down
Loading