@@ -12,6 +12,16 @@ class BenchmarkScore {
1212 required this .value,
1313 });
1414
15+ /// Deserializes a JSON object to create a [BenchmarkScore] object.
16+ factory BenchmarkScore .parse (Map <String , Object ?> json) {
17+ final String metric = json[_metricKey]! as String ;
18+ final double value = (json[_valueKey]! as num ).toDouble ();
19+ return BenchmarkScore (metric: metric, value: value);
20+ }
21+
22+ static const String _metricKey = 'metric' ;
23+ static const String _valueKey = 'value' ;
24+
1525 /// The name of the metric that this score is categorized under.
1626 ///
1727 /// Scores collected over time under the same name can be visualized as a
@@ -22,10 +32,10 @@ class BenchmarkScore {
2232 final num value;
2333
2434 /// Serializes the benchmark metric to a JSON object.
25- Map <String , dynamic > toJson () {
26- return < String , dynamic > {
27- 'metric' : metric,
28- 'value' : value,
35+ Map <String , Object ? > toJson () {
36+ return < String , Object ? > {
37+ _metricKey : metric,
38+ _valueKey : value,
2939 };
3040 }
3141}
@@ -35,22 +45,30 @@ class BenchmarkResults {
3545 /// Constructs a result containing scores from a single run benchmark run.
3646 BenchmarkResults (this .scores);
3747
48+ /// Deserializes a JSON object to create a [BenchmarkResults] object.
49+ factory BenchmarkResults .parse (Map <String , Object ?> json) {
50+ final Map <String , List <BenchmarkScore >> results =
51+ < String , List <BenchmarkScore >> {};
52+ for (final String key in json.keys) {
53+ final List <BenchmarkScore > scores = (json[key]! as List <Object ?>)
54+ .cast <Map <String , Object ?>>()
55+ .map (BenchmarkScore .parse)
56+ .toList ();
57+ results[key] = scores;
58+ }
59+ return BenchmarkResults (results);
60+ }
61+
3862 /// Scores collected in a benchmark run.
3963 final Map <String , List <BenchmarkScore >> scores;
4064
4165 /// Serializes benchmark metrics to JSON.
42- Map <String , List <Map <String , dynamic >>> toJson () {
43- return scores.map <String , List <Map <String , dynamic >>>(
66+ Map <String , List <Map <String , Object ? >>> toJson () {
67+ return scores.map <String , List <Map <String , Object ? >>>(
4468 (String benchmarkName, List <BenchmarkScore > scores) {
45- return MapEntry <String , List <Map <String , dynamic >>>(
69+ return MapEntry <String , List <Map <String , Object ? >>>(
4670 benchmarkName,
47- scores
48- .map <Map <String , dynamic >>(
49- (BenchmarkScore score) => < String , dynamic > {
50- 'metric' : score.metric,
51- 'value' : score.value,
52- })
53- .toList (),
71+ scores.map ((BenchmarkScore score) => score.toJson ()).toList (),
5472 );
5573 });
5674 }
0 commit comments