Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deriving converters for Yojson.Safe.t #7

Open
psteckler opened this issue May 14, 2021 · 3 comments
Open

Deriving converters for Yojson.Safe.t #7

psteckler opened this issue May 14, 2021 · 3 comments
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.

Comments

@psteckler
Copy link

I have a type that contains a use of Yojson.Safe.t, like:

type t = { x : Yojson.Safe.t; y : int } [@@deriving yojson]

In my editor using Merlin, Yojson.Safe.t is highlighted red, indicating there's no Yojson.Safe.t_of_yojson (which would be the identity function).

I'm using ppx_yojson_conv v0.13.0 with OCaml 4.11.2.

Is this a known issue? Is there a workaround?

Incidentally, ppx_deriving_yojson has no trouble with this code.

@github-iron github-iron added the forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system. label May 17, 2021
@kwohlfahrt
Copy link

kwohlfahrt commented Jun 12, 2021

I found the following worked for me, but I'm new to OCaml so there might be other issues with it:

module Safe = struct
  include Yojson.Safe

  let t_of_yojson x = x
  let yojson_of_t = x
end

type foo = {
  xs : Safe.t;
}[@@deriving yojson];;

@zbaylin
Copy link

zbaylin commented Sep 4, 2021

You can also do this using generic types, i.e.

type 'a t_ = {
  x : 'a;
  y : int
}
[@@deriving yojson_of]

type t = Yojson.Safe.t t_

let yojson_of_t = yojson_of_t_ Fun.id

although this is somewhat verbose

@zoj613
Copy link

zoj613 commented May 13, 2024

Is there any support for this yet? I'd like to have a record field be any valid json object. I thought using Yojson.Safe.t would work but it raises the same error as OP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.
Projects
None yet
Development

No branches or pull requests

5 participants