├── android ├── local.properties └── app │ └── src │ └── main │ └── java │ └── io │ └── flutter │ └── plugins │ └── GeneratedPluginRegistrant.java ├── ios ├── Runner │ ├── GeneratedPluginRegistrant.m │ └── GeneratedPluginRegistrant.h └── Flutter │ └── Generated.xcconfig ├── pubspec.yaml ├── lib └── json_resolve.dart ├── .gitignore ├── LICENSE ├── test └── json_resolve_test.dart └── README.md /android/local.properties: -------------------------------------------------------------------------------- 1 | sdk.dir=/Users/khoa/Library/Android/sdk 2 | flutter.sdk=/Users/khoa/flutter -------------------------------------------------------------------------------- /ios/Runner/GeneratedPluginRegistrant.m: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | #import "GeneratedPluginRegistrant.h" 6 | 7 | @implementation GeneratedPluginRegistrant 8 | 9 | + (void)registerWithRegistry:(NSObject*)registry { 10 | } 11 | 12 | @end 13 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: json_resolve 2 | description: Easy resolving deep json using keypath 3 | version: 1.0.0 4 | author: Khoa Pham 5 | homepage: https://github.com/onmyway133/json_resolve 6 | 7 | environment: 8 | sdk: '>=1.20.1 <3.0.0' 9 | 10 | dev_dependencies: 11 | test: '>=0.12.42 <2.0.0' -------------------------------------------------------------------------------- /ios/Flutter/Generated.xcconfig: -------------------------------------------------------------------------------- 1 | // This is a generated file; do not edit or check into version control. 2 | FLUTTER_ROOT=/Users/khoa/flutter 3 | FLUTTER_APPLICATION_PATH=/Users/khoa/XcodeProject2/json_resolve 4 | FLUTTER_TARGET=lib/main.dart 5 | FLUTTER_BUILD_DIR=build 6 | SYMROOT=${SOURCE_ROOT}/../build/ios 7 | FLUTTER_FRAMEWORK_DIR=/Users/khoa/flutter/bin/cache/artifacts/engine/ios 8 | FLUTTER_BUILD_NAME=1.0.0 9 | -------------------------------------------------------------------------------- /ios/Runner/GeneratedPluginRegistrant.h: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | #ifndef GeneratedPluginRegistrant_h 6 | #define GeneratedPluginRegistrant_h 7 | 8 | #import 9 | 10 | @interface GeneratedPluginRegistrant : NSObject 11 | + (void)registerWithRegistry:(NSObject*)registry; 12 | @end 13 | 14 | #endif /* GeneratedPluginRegistrant_h */ 15 | -------------------------------------------------------------------------------- /lib/json_resolve.dart: -------------------------------------------------------------------------------- 1 | /// 2 | /// json_resolve 3 | /// Created by Khoa Pham 4 | /// 5 | 6 | T resolve({Map json, String path, T defaultValue}) { 7 | try { 8 | dynamic current = json; 9 | path.split('.').forEach((segment) { 10 | final maybeInt = int.tryParse(segment); 11 | 12 | if (maybeInt != null && current is List) { 13 | current = current[maybeInt]; 14 | } else if (current is Map ) { 15 | current = current[segment]; 16 | } 17 | }); 18 | 19 | return (current as T) ?? defaultValue; 20 | } catch(error) { 21 | print(error); 22 | return defaultValue; 23 | } 24 | } -------------------------------------------------------------------------------- /android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java: -------------------------------------------------------------------------------- 1 | package io.flutter.plugins; 2 | 3 | import io.flutter.plugin.common.PluginRegistry; 4 | 5 | /** 6 | * Generated file. Do not edit. 7 | */ 8 | public final class GeneratedPluginRegistrant { 9 | public static void registerWith(PluginRegistry registry) { 10 | if (alreadyRegisteredWith(registry)) { 11 | return; 12 | } 13 | } 14 | 15 | private static boolean alreadyRegisteredWith(PluginRegistry registry) { 16 | final String key = GeneratedPluginRegistrant.class.getCanonicalName(); 17 | if (registry.hasPlugin(key)) { 18 | return true; 19 | } 20 | registry.registrarFor(key); 21 | return false; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/dart 2 | # Edit at https://www.gitignore.io/?templates=dart 3 | 4 | ### Dart ### 5 | # See https://www.dartlang.org/guides/libraries/private-files 6 | 7 | # Files and directories created by pub 8 | .dart_tool/ 9 | .packages 10 | build/ 11 | # If you're building an application, you may want to check-in your pubspec.lock 12 | pubspec.lock 13 | 14 | # Directory created by dartdoc 15 | # If you don't generate documentation locally you can remove this line. 16 | doc/api/ 17 | 18 | # Avoid committing generated Javascript files: 19 | *.dart.js 20 | *.info.json # Produced by the --dump-info flag. 21 | *.js # When generated by dart2js. Don't specify *.js if your 22 | # project includes source files written in JavaScript. 23 | *.js_ 24 | *.js.deps 25 | *.js.map 26 | 27 | # End of https://www.gitignore.io/api/dart 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Licensed under the **MIT** license 2 | 3 | > Copyright (c) 2019 Khoa Pham 4 | > 5 | > Permission is hereby granted, free of charge, to any person obtaining 6 | > a copy of this software and associated documentation files (the 7 | > "Software"), to deal in the Software without restriction, including 8 | > without limitation the rights to use, copy, modify, merge, publish, 9 | > distribute, sublicense, and/or sell copies of the Software, and to 10 | > permit persons to whom the Software is furnished to do so, subject to 11 | > the following conditions: 12 | > 13 | > The above copyright notice and this permission notice shall be 14 | > included in all copies or substantial portions of the Software. 15 | > 16 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | > EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | > MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | > IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | > CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | > TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | > SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /test/json_resolve_test.dart: -------------------------------------------------------------------------------- 1 | /// 2 | /// json_resolve 3 | /// Created by Khoa Pham 4 | /// 5 | 6 | import 'package:test/test.dart'; 7 | import 'package:json_resolve/json_resolve.dart'; 8 | 9 | void main() { 10 | final Map json = { 11 | "movie": "isFun", 12 | "earth": 199999, 13 | "dc": [ 14 | { 15 | "name": "Superman" 16 | }, 17 | { 18 | "name": "Flash" 19 | }, 20 | { 21 | "name": "Wonder Woman" 22 | } 23 | ], 24 | "marvel": [ 25 | { 26 | "name": "Thor", 27 | "weapon": "Mjolnir", 28 | "appear": [ 29 | { 30 | "year": 2011, 31 | "title": "Thor" 32 | }, 33 | { 34 | "year": 2013, 35 | "title": "The Dark World" 36 | }, 37 | { 38 | "year": 2017, 39 | "title": "Ragnarok" 40 | } 41 | ] 42 | } 43 | ] 44 | }; 45 | 46 | test('resolve', () { 47 | final String byProperty = resolve(json: json, path: "movie", defaultValue: "error"); 48 | expect(byProperty, "isFun"); 49 | 50 | final int byInt = resolve(json: json, path: "earth", defaultValue: 0); 51 | expect(byInt, 199999); 52 | 53 | final String byIndex = resolve(json: json, path: "dc.2.name", defaultValue: "error"); 54 | expect(byIndex, "Wonder Woman"); 55 | 56 | final String byIndexThenProperty = resolve(json: json, path: "marvel.0.appear.1.title", defaultValue: "error"); 57 | expect(byIndexThenProperty, "The Dark World"); 58 | }); 59 | 60 | test("wrong type", () { 61 | final int byInt = resolve(json: json, path: "marvel.0.name", defaultValue: 999); 62 | expect(byInt, 999); 63 | }); 64 | 65 | test("not found", () { 66 | final String byString = resolve(json: json, path: "marvel.0.skill", defaultValue: "error"); 67 | expect(byString, "error"); 68 | }); 69 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # json resolve 2 | 3 | ❤️ Support my apps ❤️ 4 | 5 | - [Push Hero - pure Swift native macOS application to test push notifications](https://onmyway133.com/pushhero) 6 | - [PastePal - Pasteboard, note and shortcut manager](https://onmyway133.com/pastepal) 7 | - [Quick Check - smart todo manager](https://onmyway133.com/quickcheck) 8 | - [Alias - App and file shortcut manager](https://onmyway133.com/alias) 9 | - [My other apps](https://onmyway133.com/apps/) 10 | 11 | ❤️❤️😇😍🤘❤️❤️ 12 | 13 | - Available at https://pub.dartlang.org/packages/json_resolve 14 | - [How to resolve deep json object in Dart](https://dev.to/onmyway133/how-to-resolve-deep-json-object-in-dart-5c5l) 15 | 16 | ## Description 17 | json_resolve is a Dart library that helps accessing property in deep json object easily using keypath 18 | 19 | Given the following deeply nested json 20 | 21 | ```json 22 | { 23 | "movie": "isFun", 24 | "earth": 199999, 25 | "dc": [ 26 | { 27 | "name": "Superman" 28 | }, 29 | { 30 | "name": "Flash" 31 | }, 32 | { 33 | "name": "Wonder Woman" 34 | } 35 | ], 36 | "marvel": [ 37 | { 38 | "name": "Thor", 39 | "weapon": "Mjolnir", 40 | "appear": [ 41 | { 42 | "year": 2011, 43 | "title": "Thor" 44 | }, 45 | { 46 | "year": 2013, 47 | "title": "The Dark World" 48 | }, 49 | { 50 | "year": 2017, 51 | "title": "Ragnarok" 52 | } 53 | ] 54 | } 55 | ] 56 | } 57 | ``` 58 | 59 | We can access using keypath. The library ensures safe type checking and casting, and use provided default value as last resort 60 | 61 | ```dart 62 | final String byProperty = resolve(json: json, path: "movie", defaultValue: "error"); 63 | expect(byProperty, "isFun"); 64 | 65 | final int byInt = resolve(json: json, path: "earth", defaultValue: 0); 66 | expect(byInt, 199999); 67 | 68 | final String byIndex = resolve(json: json, path: "dc.2.name", defaultValue: "error"); 69 | expect(byIndex, "Wonder Woman"); 70 | 71 | final String byIndexThenProperty = resolve(json: json, path: "marvel.0.appear.1.title", defaultValue: "error"); 72 | expect(byIndexThenProperty, "The Dark World"); 73 | ``` 74 | 75 | ## Installation 76 | First of all add the following dependencies to your `pubspec.yaml`: 77 | 78 | ``` 79 | dependencies: 80 | json_resolve: ^1.0.0 81 | ``` 82 | 83 | ## Author 84 | 85 | Khoa Pham, onmyway133@gmail.com 86 | 87 | ## License 88 | 89 | **json_resolve** is available under the MIT license. See the [LICENSE](https://github.com/onmyway133/json_resolve/blob/master/LICENSE) file for more info. 90 | --------------------------------------------------------------------------------