JsonEditor class
JSON file manipulation helper for CLI commands.
Provides utilities for reading, writing, and merging JSON files using only
Dart's built-in dart:convert library — no external packages required.
Usage
// Read a JSON file
final data = JsonEditor.readJson('/path/to/manifest.json');
// Merge a single key (idempotent overwrite)
JsonEditor.mergeKey('/path/to/manifest.json', 'gcm_sender_id', '482941778795');
// Deep-merge two maps (nested translation files, configs, etc.)
final merged = JsonEditor.deepMerge(existing, incoming);
// Merge an incoming JSON file into an existing one (idempotent)
JsonEditor.mergeJsonFile('/app/assets/lang/en.json', '/stubs/en.json');
// Write a full map
JsonEditor.writeJson('/path/to/output.json', {'key': 'value'});
// Check if a key exists
if (!JsonEditor.hasKey('/path/to/manifest.json', 'gcm_sender_id')) {
JsonEditor.mergeKey(...);
}
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
deepMerge(
Map< String, dynamic> target, Map<String, dynamic> source, {bool additive = false}) → Map<String, dynamic> -
Deep-merge
sourceintotarget, returning a new map. -
hasKey(
String path, String key) → bool -
Check whether
keyexists as a top-level key in the JSON file. -
mergeJsonData(
String targetPath, Map< String, dynamic> sourceData, {bool additive = true, bool force = false}) → void -
Merge an in-memory
sourceDatamap into an existing JSON file. -
mergeJsonFile(
String targetPath, String sourcePath, {bool additive = true, bool force = false}) → void - Merge an incoming JSON file into an existing target JSON file.
-
mergeKey(
String path, String key, dynamic value) → void -
Read the JSON file at
path, setkeytovalue, and write it back. -
readJson(
String path) → Map< String, dynamic> -
Read and parse a JSON file from
path. -
writeJson(
String path, dynamic data, {int indent = 2}) → void -
Serialise
dataand write it topathas pretty-printed JSON.