Improve interactions between flatten attribute and skip_serializing/skip_deserializing (#2795, thanks @Mingun)