decode static method
Reads text: a file written by export --to, the output of
export --json, or a bare {"storage": ..., "backpack": ...} object.
Throws a FormatException saying what's wrong.
Implementation
static LiveSnapshot decode(String text) {
Object? decoded;
try {
decoded = jsonDecode(text);
} on FormatException catch (e) {
throw FormatException('isn\'t valid JSON (${e.message})');
}
// export --json prints [{device, ok, result}].
if (decoded is List) {
if (decoded.length != 1 || decoded.single is! Map) {
throw FormatException(
'holds ${decoded.length} apps\' results; export one app at a time',
);
}
decoded = (decoded.single as Map)['result'];
}
if (decoded is Map &&
decoded['result'] is Map &&
!decoded.containsKey('storage')) {
decoded = decoded['result'];
}
if (decoded is! Map ||
!decoded.containsKey('storage') && !decoded.containsKey('backpack')) {
throw const FormatException(
'should be a JSON object with "storage" and "backpack"',
);
}
final Object? version = decoded['nylo'];
if (version is num && version > formatVersion) {
throw FormatException(
'uses snapshot format $version, newer than this Metro '
'(format $formatVersion). Update nylo_framework',
);
}
return LiveSnapshot(
storage: _stringKeyed(decoded['storage'], 'storage'),
backpack: _stringKeyed(decoded['backpack'], 'backpack'),
app: _text(decoded['app']),
env: _text(decoded['env']),
device: _text(decoded['device']),
route: _text(decoded['route']),
exportedAt: DateTime.tryParse(_text(decoded['exportedAt']) ?? ''),
);
}