TestBrowserPlatform.fromJson constructor

  1. @visibleForTesting
TestBrowserPlatform.fromJson(
  1. String jsonText
)

Creates a new TestBrowserPlatform with properties from a JSON string.

The jsonText must be a valid JSON string representing a JSON object, and the values for the BrowserPlatform properties are extracted from the object's values for keys with the properties' names.

Example:

var testBrowserPlatform = TestBrowserPlatform.fromJson('''{
  "userAgent": "Banana/1.0 (BananaOS 0.9.1)",
}''')'

The JSON object's value for such a key must be either a string or null.

Throws a FormatException if the jsonText string is not valid JSON text, the JSON text does not represent a JSON object, or a property's value is not a string. (The property values must be strings because all properties of TestBrowserPlatform are strings).

Other keys are ignored. Missing property keys, or keys with a null value, leave the property undefined.

The toJson method will leave out undefined properties, so TestBrowserPlatform.fromJson(TestBrowserPlatform.toJson()) can create an exact copy at a later time. (To create a copy right now, TestBrowserPlatform.copyWith() is easier and more efficient.)

Implementation

@visibleForTesting
factory TestBrowserPlatform.fromJson(String jsonText) {
  var json = jsonDecode(jsonText);
  if (json is! Map<String, Object?>) {
    throw FormatException('Not a JSON map', jsonText);
  }
  return TestBrowserPlatform(
    version: _getJsonProperty<String>(json, json_key.version, jsonText),
    userAgent: _getJsonProperty<String>(json, json_key.userAgent, jsonText),
  );
}