Coder Social home page Coder Social logo

json_resolver's Introduction

json resolver

Description

json_resolver is a Dart library that helps accessing property in deep json object easily using keypath.

Given the following deeply nested json:

{
    "movie": "isFun",
    "earth": 199999,
    "dc": [
      {"name": "Superman"},
      {"name": "Flash"},
      {"name": "Wonder Woman"}
    ],
    "marvel": [
      {
        "name": "Thor",
        "weapon": "Mjolnir",
        "appear": [
          {"year": 2011, "title": "Thor"},
          {"year": 2013, "title": "The Dark World"},
          {"year": 2017, "title": "Ragnarok"}
        ]
      }
    ]
  }

We can access using keypath. The library ensures safe type checking and casting. You can provide default value to ensure that return value is not null (if you provide default value you may use bang operator ! to to cast the return to a non null value).

final String? byPropertyNullable = resolve(json: json, path: "movie");
expect(byPropertyNullable, "isFun");

final String byProperty = resolve(json: json, path: "movie", defaultValue: "error")!;
expect(byProperty, "isFun");

final int byInt = resolve(json: json, path: "earth", defaultValue: 0)!;
expect(byInt, 199999);

final String byIndex = resolve(json: json, path: "dc.2.name", defaultValue: "error")!;
expect(byIndex, "Wonder Woman");

final String byIndexThenProperty = resolve(json: json, path: "marvel.0.appear.1.title", defaultValue: "error")!;
expect(byIndexThenProperty, "The Dark World");

final int? byIntNullable = resolve(json: json, path: "movie");
expect(byIntNullable, null);

final int byInt = resolve(json: json, path: "movie", defaultValue: 0)!;
expect(byInt, 0);

Installation

First of all add the following dependencies to your pubspec.yaml:

dependencies:
  json_resolver: ^2.0.0

License

json_resolver is available under the MIT license. See the LICENSE file for more info.

json_resolver's People

Contributors

onmyway133 avatar spooky12 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.