json_string 0.0.5

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

json_string #

Pub

⚠️ Notice: until version 0.1.0 is released, API signature may change.

A simple and lightweight JSON data container for Dart and Flutter.

Configure #

Add json_string to pubspec.yaml under the dependencies subsection.

dependencies:
  json_string: ^0.0.5

Install #

Install packages from the command line (or using your favourite IDE).

with pub:

$ pub get

with flutter:

$ flutter packages get

Import #

In your library, add the following line:

import 'package:json_string/json_string.dart';

How to use it #

All you need is the JsonString class. This is where your JSON data is stored. When you create a new instance, your JSON data is evaluated and minified.

Overview #

Check if your string is a valid JSON, simply using the default constructor:

try {
    final jsonString = JsonString('{"username":"john_doe","password":"querty"}');
    // ...
} on JsonFormatException catch (e) {
    print('Invalid JSON format: $e');
}

Or just work with null values, if you prefer:

final jsonString = JsonString.orNull('{"username":"john_doe","password":"querty"}');

if(jsonString == null) {
    print('Invalid JSON format');
}
// ...

Then, access your data through the source property:

final source = jsonString.source;

print(source); // {"username":"john_doe","password":"querty"}

Or, read the equivalent decodedValue object, without using any other library:

final credentials = jsonString.decodedValue;

print(credentials['username']); // john_doe
print(credentials['password']); // querty

Encoding #

JsonString provide a set of different methods to encode Dart types, all implemented in a type-safe way and without any use of reflection or code generators.

Plain objects #

Mark as Jsonable your "json2dart" style class, and encode it with no effort:

// class declaration
class User with Jsonable {
  String username;
  String password;

  User({
    this.username,
    this.password,
  });

  Map<String, dynamic> toJson() => {
        'username': username,
        'password': password,
      };
}

// encoding
final user = User(username: 'john_doe', password: 'qwerty');
final jsonString = JsonString.encodeObject(user);

Or, provide an encoder to specify how to map your object:

final jsonString = JsonString.encodeObject(user, encoder: (u) => {
    'ba': btoa("${u.username}:${u.password}"),
});

Object lists #

In the same way, you can encode a list of objects:

final userList = [
    User(username: 'john_doe', password: 'qwerty'),
    User(username: 'clara_brothers', password: 'asdfgh')
];

// default encoding
final jsonString1 = JsonString.encodeObjectList(userList);

// custom encoding
final jsonString2 = JsonString.encodeObjectList(userList, encoder: (u) => {
    'ba': btoa("${u.username}:${u.password}"),
});

Primitive lists #

If you want to encode a list of primitive values (int, double, String or bool), use encodePrimitiveList():

// integers
final fibonacci = [1, 1, 2, 3, 5, 8, 13];
final jsonString = JsonString.encodePrimitiveList(fibonacci);

// strings
final message = ["h", "e", "l", "l", "o", "!"];
final jsonString = JsonString.encodePrimitiveList(message);

// doubles
final temperatures = [16.0, 18.0, 21.0, 19.0];
final jsonString = JsonString.encodePrimitiveList(temperatures);

// booleans
final flags = [false, false, true, false];
final jsonString = JsonString.encodePrimitiveList(flags);

Contribute #

If you find a bug or you would like to see a new feature, please create an issue.

0.0.5 #

  • Cache option added.

0.0.4 #

  • README updated.

0.0.3 #

  • README updated.

0.0.2 #

  • Pub maintenance score improved.
  • First simple example added.

0.0.1 #

  • Initial release.

example/json_string_example.dart

import 'package:json_string/json_string.dart';

void main() {
  const externalSource = '{"username":"john_doe","password":"querty"}';
  var jsonString = JsonString.orNull(externalSource);
  if(jsonString == null) {
    print("Bad source!");
  }
  final user1 = User.fromJson(jsonString.decodedValue);
  final user2 = User(username: 'clara_brothers', password: 'asdfgh');
  final userList = [user1, user2];
  
  jsonString = JsonString.encodeObjectList(userList);
  final internalSource = jsonString.source;
  print(internalSource);
}

class User with Jsonable {
  String username;
  String password;

  User({
    this.username,
    this.password,
  });

  static User fromJson(Map<String, dynamic> json) {
    return User(
      username: json['username'],
      password: json['password'],
    );
  }

  Map<String, dynamic> toJson() => {
        'username': username,
        'password': password,
      };
}

Use this package as a library

1. Depend on it

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


dependencies:
  json_string: ^0.0.5

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_string/json_string.dart';
  
Version Uploaded Documentation Archive
0.0.5 Jan 20, 2019 Go to the documentation of json_string 0.0.5 Download json_string 0.0.5 archive
0.0.4 Dec 11, 2018 Go to the documentation of json_string 0.0.4 Download json_string 0.0.4 archive
0.0.3 Dec 11, 2018 Go to the documentation of json_string 0.0.3 Download json_string 0.0.3 archive
0.0.2 Dec 5, 2018 Go to the documentation of json_string 0.0.2 Download json_string 0.0.2 archive
0.0.1 Dec 5, 2018 Go to the documentation of json_string 0.0.1 Download json_string 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
27
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13+1

Platforms

Detected platforms: Flutter, web, other

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

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test ^1.5.0