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
pdatacan 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
sourcestring into Dart objects:Map<String, dynamic>,List<dynamic>,String,num,bool, ornull. -
decodeToml(
String source) → Map< String, dynamic> -
Decodes a TOML
sourcestring into aMap<String, dynamic>. -
decodeYaml(
String source) → dynamic -
Decodes a YAML
sourcestring into Dart objects:Map<String, dynamic>,List<dynamic>,String,int,double,bool, ornull. -
encodeJson(
dynamic data, {bool pretty = true, String indent = ' '}) → String -
Encodes
dataas a JSON string. -
encodeToml(
Map< String, dynamic> data) → String -
Encodes
dataas a TOML string. -
encodeYaml(
dynamic data) → String -
Encodes
dataas 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
sourceasformatinto Dart objects. -
pdataEncode(
dynamic data, PdataFormat format, {bool pretty = true}) → String -
Encodes
dataasformat. -
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
dataand writes it to the file atpath. -
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,
nullhas no TOML equivalent). - PdataParseException
- Thrown by the YAML and TOML parsers when the input text is malformed.