Pregunta

As of my understanding this {"xxx": 1000000000000000} is valid right?

Not sure how to parse it with Yojson.Safe. I'm looking for something like _ `Int64 of int64 _ , but none provided, there are only _ `Int of int _ and _ `Intlit of string _ on the api.

Edit, this is my problem

let x = "{\"xxx\": 10000000000000}"
let json = Yojson.Safe.from_string x
match json with `Assoc [("xxx", `Intlit yyy)] -> yyy | _ -> assert false

It won't match because the type of json is

val json : Yojson.Safe.json = `Assoc [("xxx", `Int 10000000000000)]
¿Fue útil?

Solución

It seems Yojson returns Int if it fits in an OCaml int and `Intlit otherwise so you need to treat all the cases:

match json with 
| `Assoc [("xxx", `Intlit lit)] -> Int64.of_string lit
| `Assoc [("xxx", `Int i)] -> Int64.of_int i
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top