fix: Make fromJson* functions work when reading JSON from a String

This commit is contained in:
2022-09-15 13:41:33 +02:00
parent 49c847a96b
commit 96d9c55c87
4 changed files with 58 additions and 61 deletions

View File

@@ -94,11 +94,20 @@ class OmemoDoubleRatchet {
]
}
*/
final mkSkipped = <SkippedKey, List<int>>{};
for (final entry in data['mkskipped']! as List<Map<String, dynamic>>) {
final key = SkippedKey.fromJson(entry);
mkSkipped[key] = base64.decode(entry['key']! as String);
}
// NOTE: Dart has some issues with just casting a List<dynamic> to List<Map<...>>, as
// such we need to convert the items by hand.
final mkSkipped = Map<SkippedKey, List<int>>.fromEntries(
(data['mkskipped']! as List<dynamic>).map<MapEntry<SkippedKey, List<int>>>(
(entry) {
final map = entry as Map<String, dynamic>;
final key = SkippedKey.fromJson(map);
return MapEntry(
key,
base64.decode(map['key']! as String),
);
},
),
);
return OmemoDoubleRatchet(
OmemoKeyPair.fromBytes(

View File

@@ -49,14 +49,23 @@ class Device {
]
}
*/
final opks = <int, OmemoKeyPair>{};
for (final opk in data['opks']! as List<Map<String, dynamic>>) {
opks[opk['id']! as int] = OmemoKeyPair.fromBytes(
base64.decode(opk['public']! as String),
base64.decode(opk['private']! as String),
KeyPairType.x25519,
);
}
// NOTE: Dart has some issues with just casting a List<dynamic> to List<Map<...>>, as
// such we need to convert the items by hand.
final opks = Map<int, OmemoKeyPair>.fromEntries(
(data['opks']! as List<dynamic>).map<MapEntry<int, OmemoKeyPair>>(
(opk) {
final map = opk as Map<String, dynamic>;
return MapEntry(
map['id']! as int,
OmemoKeyPair.fromBytes(
base64.decode(map['public']! as String),
base64.decode(map['private']! as String),
KeyPairType.x25519,
),
);
},
),
);
return Device(
data['jid']! as String,

View File

@@ -37,10 +37,23 @@ class OmemoSessionManager {
/// Deserialise the OmemoSessionManager from JSON data [data] that does not contain
/// the ratchet sessions.
factory OmemoSessionManager.fromJsonWithoutSessions(Map<String, dynamic> data, Map<RatchetMapKey, OmemoDoubleRatchet> ratchetMap, TrustManager trustManager) {
factory OmemoSessionManager.fromJsonWithoutSessions(
Map<String, dynamic> data,
Map<RatchetMapKey, OmemoDoubleRatchet> ratchetMap,
TrustManager trustManager,
) {
// NOTE: Dart has some issues with just casting a List<dynamic> to List<Map<...>>, as
// such we need to convert the items by hand.
return OmemoSessionManager(
Device.fromJson(data['device']! as Map<String, dynamic>),
data['devices']! as Map<String, List<int>>,
(data['devices']! as Map<String, dynamic>).map<String, List<int>>(
(key, value) {
return MapEntry(
key,
(value as List<dynamic>).map<int>((i) => i as int).toList(),
);
}
),
ratchetMap,
trustManager,
);