k-paxian / dart-json-mapper

Serialize / Deserialize Dart Objects to / from JSON
https://pub.dev/packages/dart_json_mapper
Other
399 stars 33 forks source link

How to ignore unknown types during serialization #173

Closed ramjke closed 2 years ago

ramjke commented 2 years ago
import 'dart:io';
import 'package:dart_json_mapper/dart_json_mapper.dart';

@jsonSerializable
class Folder {
  Folder({this.file});

  @JsonProperty(ignore: true)
  final File? file;
}

then JsonMapper.serialize(new Folder(file: localFile)) throws:

Unhandled Exception: It seems your class '_File' has not been annotated with @jsonSerializable

Expected: Serialize ignores file field on serialize

k-paxian commented 2 years ago

It's not an intended usage of ignore flag. Instead you would like to ignore unknown type, so for that to work please use following example

JsonMapper.serialize(new Folder(file: localFile), SerializationOptions(ignoreUnknownTypes: true));