platform/packages/serialize/angel_serialize/lib/angel3_serialize.dart

149 lines
4.1 KiB
Dart
Raw Normal View History

2019-04-08 15:00:04 +00:00
export 'dart:convert' show json, Codec, Converter;
2021-05-15 14:20:56 +00:00
export 'package:angel3_model/angel3_model.dart';
2019-01-25 14:48:23 +00:00
export 'package:collection/collection.dart';
export 'package:meta/meta.dart' show required, Required;
2021-05-02 05:23:44 +00:00
export 'package:quiver/core.dart' show hashObjects;
2018-11-03 07:13:19 +00:00
2017-06-17 01:26:19 +00:00
/// Excludes a field from being excluded.
2019-01-25 14:44:58 +00:00
class Exclude extends SerializableField {
2019-10-05 07:13:33 +00:00
const Exclude({bool canDeserialize = false, bool canSerialize = false})
2019-10-05 07:10:29 +00:00
: super(
exclude: true,
canDeserialize: canDeserialize,
canSerialize: canSerialize);
2017-06-17 01:26:19 +00:00
}
2019-04-08 04:41:58 +00:00
/// No longer necessary, as this is the default.
@deprecated
2019-10-05 07:13:33 +00:00
const SerializableField nullable = SerializableField(isNullable: true);
2017-06-17 01:26:19 +00:00
2019-04-08 04:41:58 +00:00
/// Marks a field as not accepting `null` values.
2019-10-05 07:13:33 +00:00
const SerializableField notNull = SerializableField(isNullable: false);
2019-04-08 04:41:58 +00:00
2019-10-05 07:13:33 +00:00
const Exclude exclude = Exclude();
2019-01-06 23:50:33 +00:00
2019-01-25 14:44:58 +00:00
/// Shorthand for [SerializableField].
class DefaultsTo extends SerializableField {
const DefaultsTo(value) : super(defaultValue: value);
}
2018-05-15 19:54:32 +00:00
2019-01-25 14:44:58 +00:00
/// Shorthand for [SerializableField].
class HasAlias extends SerializableField {
const HasAlias(String name) : super(alias: name);
2018-05-15 19:54:32 +00:00
}
2019-01-06 23:50:33 +00:00
/// Attaches options to a field.
class SerializableField {
/// An alternative name for this field.
2021-05-02 05:23:44 +00:00
final String? alias;
2019-01-06 23:50:33 +00:00
/// A default for this field.
final defaultValue;
/// A custom serializer for this field.
2021-05-02 05:23:44 +00:00
final Symbol? serializer;
2019-01-06 23:50:33 +00:00
/// A custom serializer for this field.
2021-05-02 05:23:44 +00:00
final Symbol? deserializer;
2019-01-06 23:50:33 +00:00
2019-01-07 00:56:05 +00:00
/// An error message to be printed when the provided value is invalid.
2021-05-02 05:23:44 +00:00
final String? errorMessage;
2019-01-07 00:56:05 +00:00
/// Whether this field can be set to `null`.
final bool isNullable;
/// Whether to exclude this field from serialization. Defaults to `false`.
final bool exclude;
/// Whether this field can be serialized, if [exclude] is `true`. Defaults to `false`.
final bool canDeserialize;
2019-01-06 23:50:33 +00:00
2019-01-07 00:56:05 +00:00
/// Whether this field can be serialized, if [exclude] is `true`. Defaults to `false`.
final bool canSerialize;
2019-01-09 19:25:05 +00:00
/// May be used with [serializer] and [deserializer].
///
/// Specifies the [Type] that this field serializes to.
///
/// Ex. If you have a field that serializes to a JSON string,
/// specify `serializesTo: String`.
2021-05-02 05:23:44 +00:00
final Type? serializesTo;
2019-01-09 19:25:05 +00:00
2019-01-07 00:56:05 +00:00
const SerializableField(
2019-01-06 23:50:33 +00:00
{this.alias,
this.defaultValue,
this.serializer,
this.deserializer,
2019-01-07 00:56:05 +00:00
this.errorMessage,
2019-10-05 07:13:33 +00:00
this.isNullable = true,
this.exclude = false,
this.canDeserialize = false,
this.canSerialize = false,
2019-01-09 19:25:05 +00:00
this.serializesTo});
2019-01-06 23:50:33 +00:00
}
2017-06-17 01:26:19 +00:00
/// Marks a class as eligible for serialization.
class Serializable {
const Serializable(
2019-10-05 07:13:33 +00:00
{this.serializers = const [Serializers.map, Serializers.json],
this.autoSnakeCaseNames = true,
2019-04-30 15:44:01 +00:00
// ignore: deprecated_member_use_from_same_package
@deprecated this.autoIdAndDateFields = true,
2019-10-05 07:13:33 +00:00
this.includeAnnotations = const []});
/// A list of enabled serialization modes.
///
/// See [Serializers].
final List<int> serializers;
/// Overrides the setting in `SerializerGenerator`.
final bool autoSnakeCaseNames;
/// Overrides the setting in `JsonModelGenerator`.
2019-01-09 19:25:05 +00:00
@deprecated
final bool autoIdAndDateFields;
2019-01-07 00:56:05 +00:00
/// A list of constant members to affix to the generated class.
final List includeAnnotations;
2017-06-17 01:26:19 +00:00
}
2019-10-05 07:13:33 +00:00
const Serializable serializable = Serializable();
2017-06-17 01:26:19 +00:00
/// Used by `package:angel_serialize_generator` to reliably identify generated models.
class GeneratedSerializable {
const GeneratedSerializable();
}
2019-10-05 07:13:33 +00:00
const GeneratedSerializable generatedSerializable = GeneratedSerializable();
/// The supported serialization types.
abstract class Serializers {
/// All supported serialization types.
2019-10-05 07:13:33 +00:00
static const List<int> all = [map, json, typescript];
/// Enable `fromMap` and `toMap` methods on the model class.
2018-02-28 00:47:42 +00:00
static const int map = 0;
/// Enable a `toJson` method on the model class.
2018-02-28 00:47:42 +00:00
static const int json = 1;
2018-03-29 19:58:36 +00:00
/// Generate a TypeScript definition file (`.d.ts`) for use on the client-side.
static const int typescript = 2;
}
2019-01-25 14:44:58 +00:00
@deprecated
class DefaultValue {
final value;
const DefaultValue(this.value);
}
2019-01-06 23:50:33 +00:00
@deprecated
/// Prefer [SerializableField] instead.
2017-06-17 01:26:19 +00:00
class Alias {
final String name;
2017-06-17 01:26:19 +00:00
const Alias(this.name);
}