Yojson解析int64(ocaml)

时间:2014-03-07 09:45:29

标签: json ocaml bigint int64

据我所知,这个{“xxx”:1000000000000000}有效吗?

不知道如何用Yojson.Safe解析它。我正在寻找类似于_ int64的int64 _,但没有提供,在api上只有_` int of int _和_`Intlit of string _。

编辑,这是我的问题

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

它不匹配,因为json的类型是

val json : Yojson.Safe.json = `Assoc [("xxx", `Int 10000000000000)]

1 个答案:

答案 0 :(得分:3)

如果Yojson适合OCaml Intint,那么它似乎会返回`Intlit,否则您需要处理所有情况:

match json with 
| `Assoc [("xxx", `Intlit lit)] -> Int64.of_string lit
| `Assoc [("xxx", `Int i)] -> Int64.of_int i
相关问题