ppx_deriving_yojson icon indicating copy to clipboard operation
ppx_deriving_yojson copied to clipboard

Exclude key from serialization

Open haskellcamargo opened this issue 6 years ago • 1 comments

I'd like to omit some keys (with complex values) from being serialized/deserialized and managed by ppx_deriving_yojson.

type t = {
  class_name: string;
  long_name: bool;
  data_variables: unit declaration Naming.Long_map.t [@default Naming.Long_map.empty];
  methods: method_ declaration Naming.Long_map.t [@default Naming.Long_map.empty]
}
[@@deriving make, to_yojson]

In this situation, I want to serialize class_name and long_name, but I'd like to omit data_variables and methods. How can I force it to not handle them (in a way other than creating a dummy *_to_yojson implementation)?

My suggestion is something like [@opaque] for visitors ppx.

haskellcamargo avatar Dec 03 '18 18:12 haskellcamargo

Just FYI, my work-around has been to use [@default …] on those fields like you, and to pass a masked copy of the records of interest to Yojson, with those fields set to their default values. They won't be output.

(Of course, Base/Core redefine (=) to be integer-specific which limits ppx_deriving_yojson's checks for default values (#79), so we have to work around that too.)

vphantom avatar Apr 17 '19 19:04 vphantom