Skip to content

Commit

Permalink
Use external JSON Pointer (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath authored Jan 29, 2021
1 parent 04ada88 commit 137cde4
Show file tree
Hide file tree
Showing 37 changed files with 217 additions and 652 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
- `JsonPathMatch.context` contains the matching context. It is intended to be used in named filters.
- `JsonPathMatch.parent` contains the parent match.
- `JsonPathMatch.pointer` contains the RFC 6901 JSON Pointer to the match.
- JsonPointer implementation

### Changed
- Named filters argument renamed from `filter` to `filters`
Expand Down
5 changes: 0 additions & 5 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,4 @@ void main() {
.read(document)
.map((match) => '${match.pointer}:\t${match.value}')
.forEach(print);

/// Modifying all prices in-place:
prices.read(document).map((match) => match.pointer).forEach((pointer) {
pointer.transform(document, (value) => value - 1);
});
}
8 changes: 4 additions & 4 deletions lib/json_path.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// JSONPath for Dart
library json_path;

export 'package:json_path/src/path/filter_not_found.dart';
export 'package:json_path/src/path/json_path.dart';
export 'package:json_path/src/path/json_path_match.dart';
export 'package:json_path/src/path/matching_context.dart';
export 'package:json_path/src/filter_not_found.dart';
export 'package:json_path/src/json_path.dart';
export 'package:json_path/src/json_path_match.dart';
export 'package:json_path/src/matching_context.dart';
5 changes: 0 additions & 5 deletions lib/json_pointer.dart

This file was deleted.

37 changes: 37 additions & 0 deletions lib/src/child_match.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:json_path/src/json_path_match.dart';
import 'package:json_path/src/matching_context.dart';
import 'package:json_path/src/quote.dart';
import 'package:rfc_6901/rfc_6901.dart';

/// Creates a match for a child element
class ChildMatch implements JsonPathMatch {
/// Child match for an array element
ChildMatch.index(int index, this.parent)
: value = parent.value[index],
path = parent.path + '[' + index.toString() + ']',
pointer = JsonPointerSegment(index.toString(), parent.pointer);

/// Child match for an object child
ChildMatch.child(String key, this.parent)
: value = parent.value[key],
path = parent.path + '[' + quote(key) + ']',
pointer = JsonPointerSegment(key, parent.pointer);

/// The value
@override
final value;

/// JSONPath to this match
@override
final String path;

/// JSON Pointer (RFC 6901) to this match
@override
final JsonPointer pointer;

@override
MatchingContext get context => parent.context;

@override
final JsonPathMatch parent;
}
File renamed without changes.
18 changes: 9 additions & 9 deletions lib/src/path/grammar.dart → lib/src/grammar.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:json_path/src/path/selector/array_index.dart';
import 'package:json_path/src/path/selector/array_slice.dart';
import 'package:json_path/src/path/selector/field.dart';
import 'package:json_path/src/path/selector/named_filter.dart';
import 'package:json_path/src/path/selector/recursion.dart';
import 'package:json_path/src/path/selector/selector.dart';
import 'package:json_path/src/path/selector/sequence.dart';
import 'package:json_path/src/path/selector/union.dart';
import 'package:json_path/src/path/selector/wildcard.dart';
import 'package:json_path/src/selector.dart';
import 'package:json_path/src/selector/array_index.dart';
import 'package:json_path/src/selector/array_slice.dart';
import 'package:json_path/src/selector/field.dart';
import 'package:json_path/src/selector/named_filter.dart';
import 'package:json_path/src/selector/recursion.dart';
import 'package:json_path/src/selector/sequence.dart';
import 'package:json_path/src/selector/union.dart';
import 'package:json_path/src/selector/wildcard.dart';
import 'package:petitparser/petitparser.dart';

final minus = char('-');
Expand Down
11 changes: 5 additions & 6 deletions lib/src/path/json_path.dart → lib/src/json_path.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:json_path/src/path/grammar.dart' as grammar;
import 'package:json_path/src/path/json_path_match.dart';
import 'package:json_path/src/path/match_factory.dart';
import 'package:json_path/src/path/selector/selector.dart';
import 'package:json_path/src/grammar.dart' as grammar;
import 'package:json_path/src/json_path_match.dart';
import 'package:json_path/src/root_match.dart';
import 'package:json_path/src/selector.dart';

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

/// Reads the given [json] object returning an Iterable of all values found.
Iterable readValues(json) => read(json).map((_) => _.value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:json_path/json_pointer.dart';
import 'package:json_path/src/path/matching_context.dart';
import 'package:json_path/src/matching_context.dart';
import 'package:rfc_6901/rfc_6901.dart';

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

abstract class JsonPathMatch<T> {
abstract class JsonPathMatch {
/// The value
T get value;
dynamic get value;

/// JSONPath to this match
String get path;
Expand Down
8 changes: 8 additions & 0 deletions lib/src/matching_context.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:json_path/src/json_path_match.dart';

class MatchingContext {
const MatchingContext(this.filters);

/// Named callback filters
final Map<String, CallbackFilter> filters;
}
30 changes: 0 additions & 30 deletions lib/src/path/any_match.dart

This file was deleted.

7 changes: 0 additions & 7 deletions lib/src/path/build_parser.dart

This file was deleted.

92 changes: 0 additions & 92 deletions lib/src/path/match_factory.dart

This file was deleted.

11 changes: 0 additions & 11 deletions lib/src/path/matching_context.dart

This file was deleted.

19 changes: 0 additions & 19 deletions lib/src/path/selector/array_index.dart

This file was deleted.

16 changes: 0 additions & 16 deletions lib/src/path/selector/field.dart

This file was deleted.

17 changes: 0 additions & 17 deletions lib/src/path/selector/recursion.dart

This file was deleted.

6 changes: 0 additions & 6 deletions lib/src/path/selector/selector.dart

This file was deleted.

12 changes: 0 additions & 12 deletions lib/src/path/selector/union.dart

This file was deleted.

17 changes: 0 additions & 17 deletions lib/src/path/selector/wildcard.dart

This file was deleted.

9 changes: 0 additions & 9 deletions lib/src/pointer/invalid_route.dart

This file was deleted.

Loading

0 comments on commit 137cde4

Please sign in to comment.