SFMessage.fromJson constructor
Create from JSON (handles both string and byte-array peer ID formats)
Implementation
factory SFMessage.fromJson(Map<String, dynamic> json) {
try {
final recipientRaw = json['recipientPeerId'];
final senderRaw = json['senderPeerId'];
if (recipientRaw == null) {
throw FormatException('Missing recipientPeerId in JSON');
}
if (senderRaw == null) {
throw FormatException('Missing senderPeerId in JSON');
}
// Parse peer ID from string or byte array (backward compat)
PeerId parsePeerId(dynamic raw) {
if (raw is String) {
return PeerId.fromString(raw);
} else if (raw is List) {
return PeerId.fromBytes(Uint8List.fromList(raw.cast<int>()));
} else {
throw FormatException('Invalid peerId format: ${raw.runtimeType}');
}
}
return SFMessage(
messageId: json['messageId'] as String,
recipientPeerId: parsePeerId(recipientRaw),
senderPeerId: parsePeerId(senderRaw),
payload: base64Decode(json['payload'] as String),
priority: MessagePriority.fromValue(json['priority'] as int),
expiryTimestamp: json['expiryTimestamp'] as int,
hopCount: json['hopCount'] as int,
flags: SFMessageFlags(json['flags'] as int),
createdTimestamp: json['createdTimestamp'] as int,
folderPath: json['folderPath'] as String?,
sequenceNumber: json['sequenceNumber'] as int?,
messageFlags: json['messageFlags'] != null
? MessageFlags(json['messageFlags'] as int)
: null,
persistent: json['persistent'] as bool? ?? false,
);
} catch (e, stackTrace) {
throw FormatException('Failed to parse SFMessage from JSON: $e\nJSON: $json\nStack: $stackTrace');
}
}