Skip to content

Commit

Permalink
Merge pull request #76 from mario-bermonti:mario-bermonti/issue70
Browse files Browse the repository at this point in the history
Add functionality for automatically converting base models to json (Map<String, dynamic>)
  • Loading branch information
mario-bermonti committed Apr 1, 2024
2 parents 2ac21a3 + 8b14f57 commit 5cda062
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/models/device.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';

part 'device.g.dart';

/// Represents the characteristics of the device on which the data
/// was collected. It serves as metadata about the data collected.
@JsonSerializable()
class Device {
/// Unique identifier for the participant
final String participantID;
Expand Down Expand Up @@ -46,4 +50,9 @@ class Device {
"platform: $platform, height: $height, width: $width, "
"aspectRatio: $aspectRatio)";
}

/// Convert the [Device] object to its json representation.
/// This method is particularly useful when uploading data to Firebase and
/// similar no-sql dbs.
Map<String, dynamic> toJson() => _$DeviceToJson(this);
}
25 changes: 25 additions & 0 deletions lib/models/device.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions lib/models/session.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import 'package:json_annotation/json_annotation.dart';

part 'session.g.dart';

/// Represents the metadata for data collection session.
@JsonSerializable()
class Session {
/// Unique identifier for the participant
final String participantID;
Expand All @@ -24,4 +29,9 @@ class Session {
return "Session(participantID: $participantID, sessionID: $sessionID, "
"startTime: $startTime, endTime: $endTime)";
}

/// Convert the [Session] object to its json representation.
/// This method is particularly useful when uploading data to Firebase and
/// similar no-sql dbs.
Map<String, dynamic> toJson() => _$SessionToJson(this);
}
21 changes: 21 additions & 0 deletions lib/models/session.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions lib/models/trial.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:json_annotation/json_annotation.dart';

import 'trial_type.dart';

part 'trial.g.dart';

/// Represents the data of a single trial.
@JsonSerializable()
class Trial {
/// Unique identifier for the participant
final String participantID;
Expand Down Expand Up @@ -34,4 +39,9 @@ class Trial {
return "Trial(participantID: $participantID, sessionID: $sessionID, "
"trialType: $trialType, stim: $stim, response: $response)";
}

/// Convert the [Trial] object to its json representation.
/// This method is particularly useful when uploading data to Firebase and
/// similar no-sql dbs.
Map<String, dynamic> toJson() => _$TrialToJson(this);
}
28 changes: 28 additions & 0 deletions lib/models/trial.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
sqlite3_flutter_libs: ^0.5.0
firebase_core: ^2.8.0
cloud_firestore: ^4.4.5
json_annotation: ^4.8.1

dev_dependencies:
flutter_test:
Expand All @@ -22,6 +23,7 @@ dev_dependencies:
drift_dev: ^2.16.0
build_runner: ^2.1.11
test: ^1.21.1
json_serializable: ^6.7.1
# fake_cloud_firestore: ^2.1.0

flutter:
11 changes: 11 additions & 0 deletions test/models/device_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,15 @@ void main() {

expect(device.toString(), strRep);
});

test(
"Device.toJson returns a valid json representation",
() {
final Device device = Device(participantID: '101', sessionID: '001');

final Map<String, dynamic> deviceJson = device.toJson();

expect(deviceJson, isMap);
},
);
}
16 changes: 16 additions & 0 deletions test/models/session_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,20 @@ void main() {
"endTime: ${session.endTime})";
expect(session.toString(), strRep);
});

test(
"Session.toJson() returns a valid json representation",
() {
final Session session = Session(
participantID: '101',
sessionID: '001',
startTime: DateTime.now(),
endTime: DateTime.now(),
);

final Map<String, dynamic> sessionJson = session.toJson();

expect(sessionJson, isMap);
},
);
}
17 changes: 17 additions & 0 deletions test/models/trial_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,21 @@ void main() {
"response: ${trial.response})";
expect(trial.toString(), strRep);
});

test(
"Trial.toJson returns a valid json representation",
() {
final Trial trial = Trial(
participantID: '101',
sessionID: '001',
trialType: TrialType.practice,
stim: 'stimuli',
response: 'participant response',
);

final Map<String, dynamic> trialJson = trial.toJson();

expect(trialJson, isMap);
},
);
}

0 comments on commit 5cda062

Please sign in to comment.