-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAINTROID - Implement JSON Serialization via Code Generation
serialize catrobat image
- Loading branch information
1 parent
bc991b2
commit 4b8ddb5
Showing
10 changed files
with
193 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
import 'package:paintroid/command/src/implementation/command/graphic/draw_path_command.dart'; | ||
|
||
abstract class Command with EquatableMixin { | ||
const Command(); | ||
|
||
Map<String, dynamic> toJson(); | ||
|
||
factory Command.fromJson(Map<String, dynamic> json) { | ||
throw UnimplementedError( | ||
'Subclasses must implement a factory constructor for deserialization.'); | ||
return DrawPathCommand.fromJson(json); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:image/image.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
import 'package:paintroid/io/src/json_serializer/serializer_versions.dart'; | ||
|
||
class ImageConverter implements JsonConverter<Image?, Map<String, dynamic>?> { | ||
const ImageConverter(); | ||
|
||
@override | ||
Image? fromJson(Map<String, dynamic>? json) { | ||
if (json == null) { | ||
return null; | ||
} | ||
|
||
if (json['bytes'] != null) { | ||
final bytesAsString = json['bytes'] as String; | ||
final bytes = base64Decode(bytesAsString); | ||
return decodeImage(bytes); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@override | ||
Map<String, dynamic>? toJson(Image? image) { | ||
if (image == null) { | ||
return null; | ||
} | ||
final bytes = encodePng(image); | ||
final encodedBytes = base64Encode(bytes); | ||
|
||
return { | ||
'version': SerializerVersions.PATH_ACTION_SERIALIZER_VERSION, | ||
'bytes': encodedBytes | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 78 additions & 78 deletions
156
lib/io/src/serialization/serializer/catrobat_image_serializer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,78 @@ | ||
import 'dart:typed_data'; | ||
import 'dart:ui'; | ||
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart' show Provider; | ||
import 'package:paintroid/command/command.dart' show Command, DrawPathCommand; | ||
import 'package:paintroid/io/io.dart' show CatrobatImage, IImageService; | ||
import 'package:paintroid/io/serialization.dart'; | ||
|
||
class CatrobatImageSerializer extends ProtoSerializerWithVersioning< | ||
CatrobatImage, SerializableCatrobatImage> { | ||
final DrawPathCommandSerializer _drawPathCommandSerializer; | ||
final IImageService _imageService; | ||
|
||
const CatrobatImageSerializer( | ||
super.version, this._imageService, this._drawPathCommandSerializer); | ||
|
||
static final provider = Provider.family( | ||
(ref, int ver) => CatrobatImageSerializer( | ||
ver, | ||
ref.watch(IImageService.provider), | ||
ref.watch(DrawPathCommandSerializer.provider(ver)), | ||
), | ||
); | ||
|
||
@override | ||
Future<SerializableCatrobatImage> serializeWithLatestVersion( | ||
CatrobatImage object) async { | ||
Uint8List? backgroundImageData; | ||
if (object.backgroundImage != null) { | ||
final result = await _imageService.exportAsPng(object.backgroundImage!); | ||
backgroundImageData = | ||
result.unwrapOrElse((failure) => throw failure.message); | ||
} | ||
return SerializableCatrobatImage() | ||
..magicValue = CatrobatImage.magicValue | ||
..version = CatrobatImage.latestVersion | ||
..width = object.width | ||
..height = object.height | ||
..backgroundImage = | ||
(backgroundImageData != null) ? backgroundImageData : Uint8List(0) | ||
..commands.addAll(await Future.wait(object.commands.map((command) async { | ||
if (command is DrawPathCommand) { | ||
return Any.pack( | ||
await _drawPathCommandSerializer | ||
.serializeWithLatestVersion(command), | ||
typeUrlPrefix: ProtoSerializerWithVersioning.urlPrefix, | ||
); | ||
} else { | ||
throw 'Invalid command type'; | ||
} | ||
}))); | ||
} | ||
|
||
@override | ||
Future<CatrobatImage> deserializeWithLatestVersion( | ||
SerializableCatrobatImage data) async { | ||
final commands = <Command>[]; | ||
for (final cmd in data.commands) { | ||
if (cmd.canUnpackInto(SerializableDrawPathCommand.getDefault())) { | ||
final unpacked = cmd.unpackInto(SerializableDrawPathCommand()); | ||
commands.add(await _drawPathCommandSerializer.deserialize(unpacked)); | ||
} else { | ||
throw 'Invalid command type'; | ||
} | ||
} | ||
Image? image; | ||
if (data.hasBackgroundImage()) { | ||
final result = | ||
await _imageService.import(Uint8List.fromList(data.backgroundImage)); | ||
image = result.unwrapOrElse((failure) => throw failure.message); | ||
} | ||
return CatrobatImage(commands, data.width, data.height, image, | ||
version: data.version); | ||
} | ||
|
||
@override | ||
final fromBytesToSerializable = SerializableCatrobatImage.fromBuffer; | ||
} | ||
// import 'dart:typed_data'; | ||
// import 'dart:ui'; | ||
// | ||
// import 'package:flutter_riverpod/flutter_riverpod.dart' show Provider; | ||
// import 'package:paintroid/command/command.dart' show Command, DrawPathCommand; | ||
// import 'package:paintroid/io/io.dart' show CatrobatImage, IImageService; | ||
// import 'package:paintroid/io/serialization.dart'; | ||
// | ||
// class CatrobatImageSerializer extends ProtoSerializerWithVersioning< | ||
// CatrobatImage, SerializableCatrobatImage> { | ||
// final DrawPathCommandSerializer _drawPathCommandSerializer; | ||
// final IImageService _imageService; | ||
// | ||
// const CatrobatImageSerializer( | ||
// super.version, this._imageService, this._drawPathCommandSerializer); | ||
// | ||
// static final provider = Provider.family( | ||
// (ref, int ver) => CatrobatImageSerializer( | ||
// ver, | ||
// ref.watch(IImageService.provider), | ||
// ref.watch(DrawPathCommandSerializer.provider(ver)), | ||
// ), | ||
// ); | ||
// | ||
// @override | ||
// Future<SerializableCatrobatImage> serializeWithLatestVersion( | ||
// CatrobatImage object) async { | ||
// Uint8List? backgroundImageData; | ||
// if (object.backgroundImage != null) { | ||
// final result = await _imageService.exportAsPng(object.backgroundImage!); | ||
// backgroundImageData = | ||
// result.unwrapOrElse((failure) => throw failure.message); | ||
// } | ||
// return SerializableCatrobatImage() | ||
// ..magicValue = CatrobatImage.magicValue | ||
// ..version = CatrobatImage.latestVersion | ||
// ..width = object.width | ||
// ..height = object.height | ||
// ..backgroundImage = | ||
// (backgroundImageData != null) ? backgroundImageData : Uint8List(0) | ||
// ..commands.addAll(await Future.wait(object.commands.map((command) async { | ||
// if (command is DrawPathCommand) { | ||
// return Any.pack( | ||
// await _drawPathCommandSerializer | ||
// .serializeWithLatestVersion(command), | ||
// typeUrlPrefix: ProtoSerializerWithVersioning.urlPrefix, | ||
// ); | ||
// } else { | ||
// throw 'Invalid command type'; | ||
// } | ||
// }))); | ||
// } | ||
// | ||
// @override | ||
// Future<CatrobatImage> deserializeWithLatestVersion( | ||
// SerializableCatrobatImage data) async { | ||
// final commands = <Command>[]; | ||
// for (final cmd in data.commands) { | ||
// if (cmd.canUnpackInto(SerializableDrawPathCommand.getDefault())) { | ||
// final unpacked = cmd.unpackInto(SerializableDrawPathCommand()); | ||
// commands.add(await _drawPathCommandSerializer.deserialize(unpacked)); | ||
// } else { | ||
// throw 'Invalid command type'; | ||
// } | ||
// } | ||
// Image? image; | ||
// if (data.hasBackgroundImage()) { | ||
// final result = | ||
// await _imageService.import(Uint8List.fromList(data.backgroundImage)); | ||
// image = result.unwrapOrElse((failure) => throw failure.message); | ||
// } | ||
// return CatrobatImage(commands, data.width, data.height, image, | ||
// version: data.version); | ||
// } | ||
// | ||
// @override | ||
// final fromBytesToSerializable = SerializableCatrobatImage.fromBuffer; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.