json_object_lite 2.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 82

json_object_lite

Build Status

json_object_lite allows dot notation access to JSON.parse'd objects, for Dart, lite version.

This package is a re-implementation of the json_object package by Chris Buckett except it doesn't use mirrors and hence is a much lighter version. You of course lose the JSON serialization/deserialization of the original package, however there are more advanced packages you can use for this purpose, e.g dson.

Other than that this is a faithful, lighter, re-implementation intended as a drop in replacement for the original JsonObject. Please see the json_object documentation for further details.

Note that for compatibility with Dart 2.0 only construction from strings is now supported, there are also a few other more minor updates, see the example for more details.

2.0.0<br> Dart 2.0 updates, issue 2

1.1.2<br> Pub updates

1.1.1<br> Strong mode updates for pub

1.1.0<br> Fix immutability to follow that of json_object, not reverse it

1.0.2<br> Fix failing test in Chrome

1.0.1<br> Issue 1, library name bug

1.0.0<br> Initial Release

example/example.dart

///
/// This example is taken from the following article :-
/// http://www.dartlang.org/articles/json-web-service/
///

import 'package:json_object_lite/json_object_lite.dart';

/// The Dart 2.0 update has changed how JsonObjectLite is used, previously you could do this :-
///
/// JsonObjectLite o = new JsonObjectLite.fromJSonString('........');
/// print(o.bob);
/// print(o.fred);
/// ....
///
/// If you do this now the o.bob line will be flagged by the analyser as 'the getter bob is not defined.....'
/// which is correct, its not. This was suppressed in Dart 1.0 by annotating the class with 'proxy'.
/// This annotation has been deprecated in Dart 2.0 hence the error.
///
/// The fix for Dart 2.0 is to do this :-
/// dynamic o = new JsonObjectLite.fromJSonString('........');
///
/// Declaring the created object as dynamic gives the analyzer no clues about the
/// objects structure and hence it produces no errors.
///
/// This is unfortunate in the fact the user code has to change to accommodate this but
/// in the long run its cleaner as we now know that we don't know the object structure if
/// you see what I mean.
///
/// An example taken from the dartlang article for JSON object, found here
/// https://webdev.dartlang.org/articles/get-data/json-web-service.

class LanguageWebsite extends JsonObjectLite {
  LanguageWebsite(); // default constructor (empty) implementation

  factory LanguageWebsite.fromJsonString(String json) {
    // from JSON constructor implementation
    final languageWebsite =
    new LanguageWebsite(); // create an empty instance of this class
    // Create an instance of JsonObjectLite, populated with the json string and
    // injecting the _LanguageWebsite instance.
    final jsonObject = new JsonObjectLite.fromJsonString(json, languageWebsite);
    return jsonObject; // return the populated JsonObject instance
  }

  factory LanguageWebsite.fromJsonObject(JsonObjectLite jsonObject) {
    return JsonObjectLite.toTypedJsonObjectLite(
        jsonObject, new LanguageWebsite());
  }
}

class Language extends JsonObjectLite {
  Language(); // empty, default constructor

  factory Language.fromJsonString(String json) {
    // from JSON constructor implementation
    return new JsonObjectLite.fromJsonString(
        json, new Language()); // as _LangaugeWebsite, return an instance
    // of JsonObjectLite, containing the json string and
    // injecting a _Language instance
  }
}

int main() {
  final responseText = """
{
  "language": "dart", 
  "targets": ["dartium","javascript"], 
  "website": {
    "homepage": "www.dartlang.org",
    "api": "api.dartlang.org"
  }
}""";
  final dynamic data = new Language.fromJsonString(responseText);

  // tools can now validate the property access
  print(data.language); // should be dart
  print(data.targets[0]); // should be dartium

  // nested types are also strongly typed
  final dynamic website = new LanguageWebsite.fromJsonObject(
      data.website); // contains a JsonObjectLite
  print(website.homepage);
  website.isImmutable = false; // Now we can extend it
  website.homepage = "http://www.dartlang.org";
  print(website.homepage);

  return 0;
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  json_object_lite: ^2.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:json_object_lite/json_object_lite.dart';
  
Version Uploaded Documentation Archive
2.0.0 Aug 30, 2018 Go to the documentation of json_object_lite 2.0.0 Download json_object_lite 2.0.0 archive
1.1.2 Feb 19, 2018 Go to the documentation of json_object_lite 1.1.2 Download json_object_lite 1.1.2 archive
1.1.1 Dec 11, 2017 Go to the documentation of json_object_lite 1.1.1 Download json_object_lite 1.1.1 archive
1.1.0 Oct 16, 2017 Go to the documentation of json_object_lite 1.1.0 Download json_object_lite 1.1.0 archive
1.0.2 Oct 13, 2017 Go to the documentation of json_object_lite 1.0.2 Download json_object_lite 1.0.2 archive
1.0.1 Oct 2, 2017 Go to the documentation of json_object_lite 1.0.1 Download json_object_lite 1.0.1 archive
1.0.0 Sep 26, 2017 Go to the documentation of json_object_lite 1.0.0 Download json_object_lite 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
63
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:json_object_lite/json_object_lite.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
Dev dependencies
build_runner ^0.10.0
build_test ^0.10.2
build_web_compilers ^0.4.0
coverage ^0.12.2
dart_style ^1.1.3
dartdoc ^0.20.3
test ^1.3.0