CID.fromJson constructor

CID.fromJson(
  1. Map<String, dynamic> json
)

Returns the new instance of CID based on json.

Accepts both JSON link shapes:

  • {"/": "<base32 cid>"} — the IPLD DAG-JSON convention, which is also what toJson emits.
  • {"$link": "<base32 cid>"} — the atproto data model convention (https://atproto.com/specs/data-model), so atproto JSON round-trips through this factory.

Throws InvalidCidError when neither key holds a CID string.

Implementation

factory CID.fromJson(final Map<String, dynamic> json) {
  final value = json[_defaultJsonKey] ?? json[_atprotoJsonKey];
  if (value is! String) {
    throw InvalidCidError(
      r'JSON must contain a "/" or "$link" key with a string CID',
    );
  }

  return CID.parse(value);
}