Skip to content

Commit 137cde4

Browse files
authored
Use external JSON Pointer (#19)
1 parent 04ada88 commit 137cde4

37 files changed

+217
-652
lines changed

CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
- `JsonPathMatch.context` contains the matching context. It is intended to be used in named filters.
44
- `JsonPathMatch.parent` contains the parent match.
55
- `JsonPathMatch.pointer` contains the RFC 6901 JSON Pointer to the match.
6-
- JsonPointer implementation
76

87
### Changed
98
- Named filters argument renamed from `filter` to `filters`

example/example.dart

-5
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,4 @@ void main() {
5757
.read(document)
5858
.map((match) => '${match.pointer}:\t${match.value}')
5959
.forEach(print);
60-
61-
/// Modifying all prices in-place:
62-
prices.read(document).map((match) => match.pointer).forEach((pointer) {
63-
pointer.transform(document, (value) => value - 1);
64-
});
6560
}

lib/json_path.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// JSONPath for Dart
22
library json_path;
33

4-
export 'package:json_path/src/path/filter_not_found.dart';
5-
export 'package:json_path/src/path/json_path.dart';
6-
export 'package:json_path/src/path/json_path_match.dart';
7-
export 'package:json_path/src/path/matching_context.dart';
4+
export 'package:json_path/src/filter_not_found.dart';
5+
export 'package:json_path/src/json_path.dart';
6+
export 'package:json_path/src/json_path_match.dart';
7+
export 'package:json_path/src/matching_context.dart';

lib/json_pointer.dart

-5
This file was deleted.

lib/src/child_match.dart

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import 'package:json_path/src/json_path_match.dart';
2+
import 'package:json_path/src/matching_context.dart';
3+
import 'package:json_path/src/quote.dart';
4+
import 'package:rfc_6901/rfc_6901.dart';
5+
6+
/// Creates a match for a child element
7+
class ChildMatch implements JsonPathMatch {
8+
/// Child match for an array element
9+
ChildMatch.index(int index, this.parent)
10+
: value = parent.value[index],
11+
path = parent.path + '[' + index.toString() + ']',
12+
pointer = JsonPointerSegment(index.toString(), parent.pointer);
13+
14+
/// Child match for an object child
15+
ChildMatch.child(String key, this.parent)
16+
: value = parent.value[key],
17+
path = parent.path + '[' + quote(key) + ']',
18+
pointer = JsonPointerSegment(key, parent.pointer);
19+
20+
/// The value
21+
@override
22+
final value;
23+
24+
/// JSONPath to this match
25+
@override
26+
final String path;
27+
28+
/// JSON Pointer (RFC 6901) to this match
29+
@override
30+
final JsonPointer pointer;
31+
32+
@override
33+
MatchingContext get context => parent.context;
34+
35+
@override
36+
final JsonPathMatch parent;
37+
}
File renamed without changes.

lib/src/path/grammar.dart lib/src/grammar.dart

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import 'package:json_path/src/path/selector/array_index.dart';
2-
import 'package:json_path/src/path/selector/array_slice.dart';
3-
import 'package:json_path/src/path/selector/field.dart';
4-
import 'package:json_path/src/path/selector/named_filter.dart';
5-
import 'package:json_path/src/path/selector/recursion.dart';
6-
import 'package:json_path/src/path/selector/selector.dart';
7-
import 'package:json_path/src/path/selector/sequence.dart';
8-
import 'package:json_path/src/path/selector/union.dart';
9-
import 'package:json_path/src/path/selector/wildcard.dart';
1+
import 'package:json_path/src/selector.dart';
2+
import 'package:json_path/src/selector/array_index.dart';
3+
import 'package:json_path/src/selector/array_slice.dart';
4+
import 'package:json_path/src/selector/field.dart';
5+
import 'package:json_path/src/selector/named_filter.dart';
6+
import 'package:json_path/src/selector/recursion.dart';
7+
import 'package:json_path/src/selector/sequence.dart';
8+
import 'package:json_path/src/selector/union.dart';
9+
import 'package:json_path/src/selector/wildcard.dart';
1010
import 'package:petitparser/petitparser.dart';
1111

1212
final minus = char('-');

lib/src/path/json_path.dart lib/src/json_path.dart

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import 'package:json_path/src/path/grammar.dart' as grammar;
2-
import 'package:json_path/src/path/json_path_match.dart';
3-
import 'package:json_path/src/path/match_factory.dart';
4-
import 'package:json_path/src/path/selector/selector.dart';
1+
import 'package:json_path/src/grammar.dart' as grammar;
2+
import 'package:json_path/src/json_path_match.dart';
3+
import 'package:json_path/src/root_match.dart';
4+
import 'package:json_path/src/selector.dart';
55

66
/// A JSONPath expression
77
class JsonPath {
@@ -22,8 +22,7 @@ class JsonPath {
2222
/// Reads the given [document] object returning an Iterable of all matches found.
2323
Iterable<JsonPathMatch> read(document,
2424
{Map<String, CallbackFilter> filters = const {}}) =>
25-
_selector
26-
.read(rootMatch(document, expression, {...this.filters, ...filters}));
25+
_selector.apply(RootMatch(document, {...this.filters, ...filters}));
2726

2827
/// Reads the given [json] object returning an Iterable of all values found.
2928
Iterable readValues(json) => read(json).map((_) => _.value);

lib/src/path/json_path_match.dart lib/src/json_path_match.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import 'package:json_path/json_pointer.dart';
2-
import 'package:json_path/src/path/matching_context.dart';
1+
import 'package:json_path/src/matching_context.dart';
2+
import 'package:rfc_6901/rfc_6901.dart';
33

44
/// A named filter function
55
typedef CallbackFilter = bool Function(JsonPathMatch match);
66

7-
abstract class JsonPathMatch<T> {
7+
abstract class JsonPathMatch {
88
/// The value
9-
T get value;
9+
dynamic get value;
1010

1111
/// JSONPath to this match
1212
String get path;

lib/src/matching_context.dart

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'package:json_path/src/json_path_match.dart';
2+
3+
class MatchingContext {
4+
const MatchingContext(this.filters);
5+
6+
/// Named callback filters
7+
final Map<String, CallbackFilter> filters;
8+
}

lib/src/path/any_match.dart

-30
This file was deleted.

lib/src/path/build_parser.dart

-7
This file was deleted.

lib/src/path/match_factory.dart

-92
This file was deleted.

lib/src/path/matching_context.dart

-11
This file was deleted.

lib/src/path/selector/array_index.dart

-19
This file was deleted.

lib/src/path/selector/field.dart

-16
This file was deleted.

lib/src/path/selector/recursion.dart

-17
This file was deleted.

lib/src/path/selector/selector.dart

-6
This file was deleted.

lib/src/path/selector/union.dart

-12
This file was deleted.

lib/src/path/selector/wildcard.dart

-17
This file was deleted.

lib/src/pointer/invalid_route.dart

-9
This file was deleted.

0 commit comments

Comments
 (0)