diff --git a/CHANGELOG.md b/CHANGELOG.md index 09975ec..8c69be3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ ## [Unreleased] +## [0.0.0+dev.3] - 2020-07-28 +### Added +- Partial implementation of bracket field notation + ## [0.0.0+dev.2] - 2020-07-28 ### Added - Recursive selector @@ -13,6 +17,7 @@ ### Added - Basic design draft -[Unreleased]: https://github.com/f3ath/jessie/compare/0.0.0+dev.2...HEAD +[Unreleased]: https://github.com/f3ath/jessie/compare/0.0.0+dev.3...HEAD +[0.0.0+dev.3]: https://github.com/f3ath/jessie/compare/0.0.0+dev.2...0.0.0+dev.3 [0.0.0+dev.2]: https://github.com/f3ath/jessie/compare/0.0.0+dev.1...0.0.0+dev.2 [0.0.0+dev.1]: https://github.com/f3ath/jessie/compare/0.0.0+dev.0...0.0.0+dev.1 \ No newline at end of file diff --git a/lib/src/state.dart b/lib/src/state.dart index 08d1a72..fbafb38 100644 --- a/lib/src/state.dart +++ b/lib/src/state.dart @@ -42,8 +42,11 @@ class Ready implements State { final node = nodes.single; if (node.value == '*') return AllInArray(); if (node.isNumber) return Index(int.parse(nodes.first.value)); + if (node.value.startsWith("'")) return Field(_unquote(node.value)); throw StateError('Unexpected bracket expression'); } + + String _unquote(String s) => s.substring(1, s.length - 1); } class AwaitingField implements State { diff --git a/pubspec.yaml b/pubspec.yaml index 98f4ec5..763e0f7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: json_path -version: 0.0.0+dev.2 +version: 0.0.0+dev.3 description: JSONPath for Dart. JSONPath is XPath for JSON. It is a path in a JSON document. homepage: "https://github.com/f3ath/jessie" diff --git a/test/json_path_test.dart b/test/json_path_test.dart index e523298..fd52f48 100644 --- a/test/json_path_test.dart +++ b/test/json_path_test.dart @@ -27,6 +27,20 @@ void main() { expect(store.select(json).single.value, json['store']); expect(store.select(json).single.path, r"$['store']"); }); + + test('Single field in bracket notation', () { + final store = JsonPath(r"$['store']"); + expect(store.toString(), r"$['store']"); + expect(store.select(json).single.value, json['store']); + expect(store.select(json).single.path, r"$['store']"); + }); + + test('Mixed brackets and fields', () { + final store = JsonPath(r"$['store'].bicycle['price']"); + expect(store.toString(), r"$['store']['bicycle']['price']"); + expect(store.select(json).single.value, json['store']['bicycle']['price']); + expect(store.select(json).single.path, r"$['store']['bicycle']['price']"); + }); }); group('Wildcards', () {