Skip to main content

checked_json 0.1.0 copy "checked_json: ^0.1.0" to clipboard
checked_json: ^0.1.0 copied to clipboard

A JSON object type you only get by checking a value, with typed readers that name the field that is absent or the wrong shape.

example/checked_json_example.dart

import 'dart:convert';

import 'package:checked_json/checked_json.dart';

void main() {
  // Wire text comes in through the door that checks it.
  final user = JsonObject.decode(
    '{"name": "Ada", "born": 1815.0, "tags": ["maths"], '
    '"home": {"city": "London"}}',
  );

  print(user.string('name')); // Ada
  print(user.integer('born')); // 1815, though written 1815.0
  print(user.strings('tags')); // [maths]
  print(user.object('home').string('city')); // London

  // An absent field is null to an `OrNull` reader; the default is yours.
  print(user.stringOrNull('email') ?? 'no email'); // no email

  // A field of the wrong shape is refused, and the refusal names it.
  try {
    user.integer('name');
  } on JsonFieldException catch (e) {
    print(e.message); // `name` is not an integer
  }

  // A value already decoded, whose kind is not known until it is looked at.
  final Object? decoded = jsonDecode('[1, 2]');
  print(JsonObject.parse(decoded)); // null: a list, not an object

  // An object we build ourselves, for a `toJson`.
  const reply = JsonObject.of({'ok': true});
  print(jsonEncode(reply)); // {"ok":true}
}
0
likes
0
points
0
downloads

Publisher

verified publisherenspyr.co

Weekly Downloads

A JSON object type you only get by checking a value, with typed readers that name the field that is absent or the wrong shape.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on checked_json