PolicySubject.fromJson constructor

PolicySubject.fromJson(
  1. String id,
  2. Map<String, dynamic> json
)

Returns a PolicySubject with the id and enriches it with the given information in json.

Throws an InvalidJsonSchemaException if json contains an "expiry" key which could not be parsed by DateTime.parse() OR if json contains an "type" key which isn't a String.

Implementation

factory PolicySubject.fromJson(String id, Map<String, dynamic> json) {
  final PolicySubject pS = PolicySubject(id);
  try {
    if (json.containsKey(DittoKeys.expiry)) {
      pS.expiringTimestamp = DateTime.parse(json[DittoKeys.expiry] as String);
    }
    pS.type = json[DittoKeys.type] as String;
  } on FormatException catch (e) {
    //datetime parsing failed
    throw InvalidJsonSchemaException(e.message, json.toString());
  } on TypeError catch (e) {
    throw InvalidJsonSchemaException(
        e.stackTrace.toString(), json.toString());
  }
  return pS;
}