artifact 1.0.2
artifact: ^1.0.2 copied to clipboard
Data Modeling for the local madman
example/lib/example.dart
import 'dart:convert';
import 'package:artifact/artifact.dart';
import 'package:example/gen/artifacts.gen.dart';
@artifact
class AllFields {
final AnyEnum? e;
@codec(RemoteCodec())
final Remote remoteClass;
final int x;
final String? str;
final double doub;
const AllFields({
required this.remoteClass,
this.e = AnyEnum.a,
required this.x,
this.str,
this.doub = 3,
});
}
class Remote {
final int i;
const Remote(this.i);
}
class RemoteCodec extends ArtifactCodec<int, Remote> {
const RemoteCodec();
@override
Remote? decode(int? value) => value == null ? null : Remote(value);
@override
int? encode(Remote? value) => value?.i;
}
enum AnyEnum { a, b, c, d }
void main() {
print(
jsonEncode(
jsonDecode(
AllFields(x: -6, remoteClass: Remote(7), e: AnyEnum.c).toJson(),
),
),
);
}