Line data Source code
1 : /// Used to avoid to serialize properties to json 2 1 : Null readonly(_) => null; 3 : 4 : /// Helper class for serialization to and from json 5 : class Serializer { 6 : /// Used to avoid to serialize properties to json 7 : static const Function readOnly = readonly; 8 : 9 : /// Takes values in `extra_data` key 10 : /// and puts them on the root level of the json map 11 7 : static Map<String, dynamic>? moveKeysToRoot( 12 : Map<String, dynamic>? json, 13 : List<String> topLevelFields, 14 : ) { 15 : if (json == null) { 16 : return json; 17 : } 18 7 : final clone = Map<String, dynamic>.from(json); 19 14 : clone['extra_data'] = <String, dynamic>{}; 20 : 21 21 : json.keys.forEach((key) { 22 7 : if (!topLevelFields.contains(key)) { 23 18 : clone['extra_data'][key] = clone.remove(key); 24 : } 25 : }); 26 : 27 : return clone; 28 : } 29 : 30 : /// Takes unknown json keys and puts them in the `extra_data` key 31 3 : static Map<String, dynamic> moveKeysToMapInPlace( 32 : Map<String, dynamic> intermediateMap, 33 : List<String> topLevelFields, 34 : ) { 35 3 : final clone = Map<String, dynamic>.from(intermediateMap); 36 3 : final Map<String, dynamic>? extraData = clone.remove('extra_data'); 37 : 38 3 : extraData?.keys.forEach((key) { 39 1 : if (!topLevelFields.contains(key)) { 40 2 : clone[key] = extraData[key]; 41 : } 42 : }); 43 : 44 : return clone; 45 : } 46 : }