pdata JSON/YAML/TOML for Dart

Basic Usage

Part of the Examples. Write the same data as JSON, YAML, and TOML, then read it back and use the typed accessors.

Source: example/pdata_example.dart

// Run this example from the package root:
//   dart run example/pdata_example.dart

// print() is fine in example scripts; avoid it in production code instead.
// ignore_for_file: avoid_print

import 'package:pdata/pdata.dart';

void main() {
  final config = <String, dynamic>{
    'name': 'my-app',
    'version': '1.0.0',
    'debug': false,
    'port': 8080,
    'tags': ['cli', 'tool'],
    'author': {'name': 'psdk', 'email': 'psdk@example.com'},
  };

  // Write the same data out as JSON, YAML, and TOML.
  pdataWriteFile('build/config.json', config);
  pdataWriteFile('build/config.yaml', config);
  pdataWriteFile('build/config.toml', config);

  // Format is guessed from the extension, so reading is symmetric.
  final fromYaml = pdataReadFile('build/config.yaml') as Map<String, dynamic>;
  print('Loaded from YAML:');
  print('  name: ${fromYaml.getString('name')}');
  print('  port: ${fromYaml.getInt('port')}');
  print('  debug: ${fromYaml.getBool('debug')}');
  print('  tags: ${fromYaml.getList('tags')}');

  // Typed accessors give a clear error (instead of a crash) for a missing
  // key, unless you supply a default.
  final timeout = fromYaml.getInt('timeout', defaultValue: 30);
  print('  timeout (default): $timeout');
}

Run it from the package root:

dart run example/pdata_example.dart