From fdf8c06c24293efb60ecbf3900e97b0ec82b7ef5 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 1 Jun 2018 18:57:53 +0200 Subject: [PATCH] Add Note serializers This way we don't only need to use json. --- lib/note.dart | 16 ++++++++-- lib/serializers.dart | 60 ++++++++++++++++++++++++++++++++++++++ pubspec.lock | 2 +- pubspec.yaml | 2 +- test/serializers_test.dart | 29 ++++++++++++++++++ 5 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 lib/serializers.dart create mode 100644 test/serializers_test.dart diff --git a/lib/note.dart b/lib/note.dart index 4977099b..34897052 100644 --- a/lib/note.dart +++ b/lib/note.dart @@ -4,14 +4,24 @@ typedef NoteUpdator(Note note); class Note implements Comparable { String id; - final DateTime createdAt; - final String body; + DateTime createdAt; + String body; Note({this.createdAt, this.body, this.id}); factory Note.fromJson(Map json) { + String id; + if (json.containsKey("id")) { + var val = json["id"]; + if (val.runtimeType == String) { + id = val; + } else { + id = val.toString(); + } + } + return new Note( - id: json['id'], + id: id, createdAt: DateTime.parse(json['createdAt']), body: json['body'], ); diff --git a/lib/serializers.dart b/lib/serializers.dart new file mode 100644 index 00000000..89884a05 --- /dev/null +++ b/lib/serializers.dart @@ -0,0 +1,60 @@ +import 'dart:convert'; + +import 'package:yaml/yaml.dart'; +import 'package:journal/note.dart'; + +abstract class NoteSerializer { + String encode(Note note); + Note decode(String str); +} + +class JsonNoteSerializer implements NoteSerializer { + @override + Note decode(String str) { + final json = JsonDecoder().convert(str); + return new Note.fromJson(json); + } + + @override + String encode(Note note) { + return JsonEncoder().convert(note.toJson()); + } +} + +class MarkdownYAMLSerializer implements NoteSerializer { + @override + Note decode(String str) { + if (str.startsWith("---\n")) { + var parts = str.split("---\n"); + + var yamlMap = loadYaml(parts[1]); + Map map = new Map(); + yamlMap.forEach((key, value) { + map[key] = value; + }); + map['body'] = parts[2].trimLeft(); + + return new Note.fromJson(map); + } + + return new Note(body: str); + } + + @override + String encode(Note note) { + const serparator = '---\n'; + var str = ""; + str += serparator; + + var metadata = note.toJson(); + metadata.remove('body'); + metadata.forEach((key, value) { + str += key + ": " + value + "\n"; + }); + str += serparator; + str += '\n'; + str += note.body; + + return str; + } +} diff --git a/pubspec.lock b/pubspec.lock index cd8b50f1..87164986 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -374,7 +374,7 @@ packages: source: hosted version: "1.0.7" yaml: - dependency: transitive + dependency: "direct main" description: name: yaml url: "https://pub.dartlang.org" diff --git a/pubspec.yaml b/pubspec.yaml index de210180..0c664b4b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,7 +9,7 @@ dependencies: path: "^1.5.1" path_provider: "^0.4.0" uuid: "^1.0.0" - + yaml: "^2.1.13" dev_dependencies: flutter_test: diff --git a/test/serializers_test.dart b/test/serializers_test.dart new file mode 100644 index 00000000..d666ba58 --- /dev/null +++ b/test/serializers_test.dart @@ -0,0 +1,29 @@ +import 'dart:io'; + +import 'package:journal/note.dart'; +import 'package:journal/serializers.dart'; + +import 'package:test/test.dart'; + +main() { + group('Serializers', () { + var note = + Note(id: "2", body: "This is the body", createdAt: new DateTime.now()); + + test('JSON Serializer', () { + var jsonSerializer = new JsonNoteSerializer(); + var str = jsonSerializer.encode(note); + var note2 = jsonSerializer.decode(str); + + expect(note2, note); + }); + + test('Markdown Serializer', () { + var jsonSerializer = new MarkdownYAMLSerializer(); + var str = jsonSerializer.encode(note); + var note2 = jsonSerializer.decode(str); + + expect(note2, note); + }); + }); +}