// Copyright 2024 The Flutter Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd. typedef FromJson = T Function(Map json); /// Mixin to declare a class as serializable. /// /// Classes that implement this mixin should also implement a `fromJson` factory /// constructor. /// See https://docs.flutter.dev/data-and-backend/serialization/json#serializing-json-inside-model-classes. mixin Serializable { Map toJson(); } /// Deserializes an object if it is serialized. // ignore: avoid-dynamic, serialization is exception for the rule. T deserialize(dynamic json, FromJson deserializer) { if (json is T) return json; return deserializer(json); } /// Deserializes an object if it is serialized. /// /// Returns null if the json is null. // ignore: avoid-dynamic, serialization is exception for the rule. T? deserializeNullable(dynamic json, FromJson deserializer) { if (json == null) return null; return deserialize(json, deserializer); }