Yojson.t to/from Yojson.safe.t

Hi,

I just started to create a simple json/http service in OCaml using Opium, just for fun and learning. One issue i bump into is the mapping between Yojson.t and Yojson.Safe.t. I did find a Yojson.Safe.to_basic, but that one provides me a Yojson.Basic.t, not a Yojson.t`. And there is no to_safe or from_basic that gives me a Yojson.Safe.tfrom aYojson.Basic.t`.

I am quite new to OCaml, so i lack an understanding of common patterns. What is the usual approach for this?

This expression has type Yojson.Safe.t but an expression was expected of type Yojson.t

eelco

Hi, is there a specific reason you need to use Yojson.t? I’ve personally never had a use for that, I always use Yojson.Safe.t as the JSON data type.

1 Like

I would also suggest to use Yojson.Safe.t, since Basic struggles with representation of integers.

One of the challenges going forward for Yojson will be to reduce the confusion between different variants of the parser, but for now as long as you use Safe you should be, excuse the pun, safe.

3 Likes

I guess that was part of the lack of understanding of common patterns. Just updating my types to Safe fixed my issues.

Thanks!

2 Likes