Edit : I have re-framed my question since it received no answer (meaning it might be unintelligible) and I have made some progress.
Objective
Overriding the serialization/deserialization functions of a string record member using ppx_yojson_conv deriving plugin.
type otp = {
secret : string ; (* <= need a custom serialization/deserialization function here *)
date : string ;
}
Why
Because this member is made of random characters. I want an hexadecimal representation in my json file, instead of random bytes.
\u0017q\u001az vX\u0002py\u001ccۨE\u0015 => ugly
96e5a017711a7a20765802b78 => nice
How
For this I first tried record member derivation overriding as described in the ppx_deriving_yojson deriving plugin.
This is the code below :
let tohex byte =
`String (Cryptokit.transform_string (Cryptokit.Hexa.encode ()) byte)
let fromhex hex = match hex with
| `String h -> Ok (Cryptokit.transform_string (Cryptokit.Hexa.decode ()) h)
| _ -> Error ("error")
type otp = {
secret : string [@to_yojson tohex][@of_yojson fromhex];
date : string;
} [@@deriving yojson]
let input = {|{"secret":"4162436445664768496a4b6c4d6e4f70","date":"01/01/2025"}|}
let otp = match (otp_of_yojson (Yojson.Safe.from_string input)) with
| Ok o -> o
| Error e -> print_endline e;{secret="unknown"; date="1/1/1970"}
let () = Printf.printf "%s\n" (Yojson.Safe.to_string (otp_to_yojson otp))
let () = Printf.printf " \"secret\":%s\n" (otp.secret)
output :
{"secret":"4162436445664768496a4b6c4d6e4f70","date":"01/01/2025"}
"secret":AbCdEfGhIjKlMnOp
OK but…
the documentation invites us to move from ppx_deriving_yojson to ppx_yojson_conv in its introduction. So I want to use that modern (?) plugin instead, and also because that’s the one I use for other parts of this piece of code (which has been removed in this post). I initially thought one could specialize record member serialization/deserialization functions as in the ppx_deriving_yojson plugin but it seams we cannot. At least this is not mentioned in the documentation and all my attempts to do so failed so far.
Question
Is it possible to override record member’s serialization/deserialization functions using the ppx_yojson_conv plugin, as in its father’s plugin ?
If this is not possible, what are my options ?