Skip to content
/ apjo Public

Applicative-based Parser for JSON in OCaml

Notifications You must be signed in to change notification settings

cvarad/apjo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build & Test

APJO: Applicative-based Parser for JSON in OCaml

APJO is a JSON parsing library written in pure OCaml using immutable data structures. At its core, it is built using the concept of Applicatives and Functional Parsing.

Note: This parser was developed as a means to get used to OCaml, and to learn more about functional parsing.

Usage

The library uses the following ADT to hold the JSON values:

type js_value =
  | JSString of string
  | JSNumber of float
  | JSObject of (string * js_value) list
  | JSArray of js_value list
  | JSBool of bool
  | JSNull

To parse a string with JSON in it, use the load_string function. Use the load_file function to read a file and parse its JSON content.

load_string "{\"hello\": 5345}";;
(* js_value = JSObject [("hello", JSNumber 5345.)] *)

load_file "hello.json";;
(* js_value = JSObject [("hello", JSNumber 5345.)] *)

Similarly, to create a string out of a JSON value, use the dump_string (or dump_file) function.

dump_string @@ JSObject [ "hello", JSNumber 5345. ];;
(* string = "{\"hello\": 5345}" *)

dump_file (JSObject [ "hello", JSNumber 5345. ]) "hello.json";;
(* unit = () *)