pdata library

A dependency-free reader/writer for JSON, YAML, and TOML config files, written in pure Dart.

import 'package:pdata/pdata.dart';

void main() {
  final config = pdataReadFile('config.yaml') as Map<String, dynamic>;
  print(config.getString('name'));

  pdataWriteFile('config.json', config);
}

See pdataReadFile / pdataReadFileAsync and pdataWriteFile / pdataWriteFileAsync for the file-based API (format is guessed from the file extension by default), pdataDecode / pdataEncode for working with strings directly, and PdataTypedAccess for typed getInt/getBool/etc. accessors on the resulting map.

Format-specific decoders/encoders (decodeJson/encodeJson, decodeYaml/encodeYaml, decodeToml/encodeToml) are also exported directly, along with the exceptions (PdataFileNotFoundException, PdataFormatException, PdataParseException) they can throw.

Enums

PdataFormat
The config-file formats pdata can read and write.

Extensions

PdataTypedAccess on Map<String, dynamic>
Typed convenience accessors for the Map<String, dynamic> returned by pdataReadFile, pdataReadFileAsync, decodeJson, decodeYaml, and decodeToml.

Functions

decodeJson(String source) → dynamic
Decodes a JSON source string into Dart objects: Map<String, dynamic>, List<dynamic>, String, num, bool, or null.
decodeToml(String source) Map<String, dynamic>
Decodes a TOML source string into a Map<String, dynamic>.
decodeYaml(String source) → dynamic
Decodes a YAML source string into Dart objects: Map<String, dynamic>, List<dynamic>, String, int, double, bool, or null.
encodeJson(dynamic data, {bool pretty = true, String indent = ' '}) String
Encodes data as a JSON string.
encodeToml(Map<String, dynamic> data) String
Encodes data as a TOML string.
encodeYaml(dynamic data) String
Encodes data as a YAML string, using two-space indentation and block style throughout (mappings and sequences are never written in flow style, even though decodeYaml can read flow style).
pdataDecode(String source, PdataFormat format) → dynamic
Decodes source as format into Dart objects.
pdataEncode(dynamic data, PdataFormat format, {bool pretty = true}) String
Encodes data as format.
pdataFormatFromExtension(String path) PdataFormat?
Guesses a PdataFormat from a file path's extension.
pdataReadFile(String path, {PdataFormat? format}) → dynamic
Reads and decodes the file at path.
pdataReadFileAsync(String path, {PdataFormat? format}) Future
Async equivalent of pdataReadFile, for callers that avoid synchronous file I/O.
pdataWriteFile(String path, dynamic data, {PdataFormat? format, bool pretty = true, bool createDirectories = true}) → void
Encodes data and writes it to the file at path.
pdataWriteFileAsync(String path, dynamic data, {PdataFormat? format, bool pretty = true, bool createDirectories = true}) Future<void>
Async equivalent of pdataWriteFile, for callers that avoid synchronous file I/O.

Exceptions / Errors

PdataFileNotFoundException
Thrown by pdataReadFile and pdataReadFileAsync when the requested file does not exist.
PdataFormatException
Thrown when a PdataFormat can't be determined from a file extension and none was explicitly supplied, or when a value can't be represented in the requested format (for example, null has no TOML equivalent).
PdataParseException
Thrown by the YAML and TOML parsers when the input text is malformed.