An LFE library which provides a unified JSON experience
Introduction ↟
This library was educated by Chiron to avenge the crimes against JSON and its heirs in the Erlang world. It is destined to search for the Golden Macro, as revealed by the Cloud Goddess.
Dependencies ↟
As of version 0.4.0, this project assumes that you have
rebar3 installed somwhere in your $PATH
.
It no longer uses the old version of rebar. If you do not wish to use rebar3,
you may use the most recent rebar2-compatible release of ljson: 0.3.1.
Installation ↟
Just add it to your rebar.config
deps:
{deps, [
...
{ljson, {git, "[email protected]:lfex/ljson.git", "master"}}
]}.
And then do the usual:
$ make compile
Usage ↟
The following usage examples are all done from the LFE REPL:
$ make repl-no-deps
Starting an LFE REPL ...
Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:4:4] [async-threads:10] ...
LFE Shell V6.2 (abort with ^G)
>
Encode simple LFE data to JSON:
> (ljson:encode 'a)
#"\"a\""
ok
> (ljson:encode "a")
#"[97]"
ok
> (ljson:encode 1)
#"1"
ok
> (ljson:encode 3.14)
#"3.14"
ok
> (ljson:encode '(a b c 42))
#"[\"a\",\"b\",\"c\",42]"
ok
> (ljson:encode #(a b))
#"{\"a\":\"b\"}"
ok
> (ljson:encode '(#(a b) #(c d)))
#"{\"a\":\"b\",\"c\":\"d\"}"
ok
>
Decode simple JSON:
> (ljson:decode #b("\"a\""))
#"a"
ok
> (ljson:decode "\"a\""))
#"a"
ok
> (ljson:decode #b("[97]"))
"a"
ok
> (ljson:decode #b("1"))
1
ok
> (ljson:decode #b("3.14"))
3.14
ok
> (ljson:decode #b("[\"a\",\"b\",\"c\",42]"))
(#"a" #"b" #"c" 42)
ok
> (ljson:decode "{\"a\": \"b\"}")
#(#"a" #"b")
ok
> (ljson:decode "{\"a\":\"b\",\"c\":\"d\"}")
(#(#"a" #"b") #(#"c" #"d"))
ok
> (ljson:decode
#B(123 34 97 34 58 34 98 34 44 34 99 34 58 34 100 34 125))
(#(#"a" #"b") #(#"c" #"d"))
ok
Decode a JSON data structure (note that, for formatting purposes, the data below has been presented separated with newlines; this won't work in the LFE REPL -- you'll need to put it all on one line):
> (set json-data "{
\"First Name\": \"Jón\",
\"Last Name\": \"Þórson\",
\"Is Alive?\": true,
\"Age\": 25,
\"Height_cm\": 167.6,
\"Address\": {
\"Street Address\": \"í Gongini 5 Postsmoga 108\",
\"City\": \"Tórshavn\",
\"Country\": \"Faroe Islands\",
\"Postal Code\": \"100\"
},
\"Phone Numbers\": [
{
\"Type\": \"home\",
\"Number\": \"20 60 30\"
},
{
\"Type\": \"office\",
\"Number\": \"+298 20 60 20\"
}
],
\"Children\": [],
\"Spouse\": null}")
> (set data (ljson:decode json-data))
(#(#"First Name" #"Jón")
#(#"Last Name" #"Þórson")
#(#"Is Alive?" true)
#(#"Age" 25)
#(#"Height_cm" 167.6)
#(#"Address"
(#(#"Street Address" #"í Gongini 5 Postsmoga 108")
#(#"City" #"Tórshavn")
#(#"Country" #"Faroe Islands")
#(#"Postal Code" #"100")))
#(#"Phone Numbers"
((#(#"Type" #"home") #(#"Number" #"20 60 30"))
(#(#"Type" #"office") #(#"Number" #"+298 20 60 20"))))
#(#"Children" ())
#(#"Spouse" null))
Now let's take it full circle by encoding it again:
> (ljson:prettify (ljson:encode data))
{
"First Name": "Jón",
"Last Name": "Þórson",
"Is Alive?": true,
"Age": 25,
"Height_cm": 167.6,
"Address": {
"Street Address": "í Gongini 5 Postsmoga 108",
"City": "Tórshavn",
"Country": "Faroe Islands",
"Postal Code": "100"
},
"Phone Numbers": [
{
"Type": "home",
"Number": "20 60 30"
},
{
"Type": "office",
"Number": "+298 20 60 20"
}
],
"Children": [],
"Spouse": null
}
ok
Let's do the same, but this time from LFE data:
> (set lfe-data
`(#(#b("First Name") ,(binary ("Jón" utf8)))
#(#b("Last Name") ,(binary ("Þórson" utf8)))
#(#b("Is Alive?") true)
#(#b("Age") 25)
#(#b("Height_cm") 167.6)
#(#b("Address")
#((#(#b("Street Address") ,(binary ("í Gongini 5 Postsmoga 108" utf8)))
#(#b("City") ,(binary ("Tórshavn" utf8)))
#(#b("Country") #b("Faroe Islands"))
#(#b("Postal Code") #b("100")))))
#(#b("Phone Numbers")
(#((#(#b("Type") #b("home")) #(#b("Number") #b("20 60 30"))))
#((#(#b("Type") #b("office")) #(#b("Number") #b("+298 20 60 20"))))))
#(#b("Children") ())
#(#b("Spouse") null)))
(#(#B(...)))
> (ljson:prettify (ljson:encode lfe-data))
{
"First Name": "Jón",
"Last Name": "Þórson",
"Is Alive?": true,
"Age": 25,
"Height_cm": 167.6,
"Address": {
"Street Address": "í Gongini 5 Postsmoga 108",
"City": "Tórshavn",
"Country": "Faroe Islands",
"Postal Code": "100"
},
"Phone Numbers": [
{
"Type": "home",
"Number": "20 60 30"
},
{
"Type": "office",
"Number": "+298 20 60 20"
}
],
"Children": [],
"Spouse": null
}
ok
Extract elements from the original converted data structure as well as our LFE data structure we just entered directly, above:
> (ljson:get data '("First Name"))
#"Jón"
> (ljson:get data '("Address" "City"))
#"Tórshavn"
> (ljson:get data '("Phone Numbers" 1 "Type"))
#"home"
> (ljson:get lfe-data '("First Name") )
#"Jón"
> (ljson:get lfe-data '("Address" "City")data)
#"Tórshavn"
> (ljson:get lfe-data '("Phone Numbers" 1 "Type"))
#"home"
You may also use atom or binary keys:
> (ljson:get lfe-data '(|Phone Numbers| 1 Number))
#"20 60 30"
> (ljson:get lfe-data '(#"Phone Numbers" 1 #"Number"))
#"20 60 30"
Extract elements directly from JSON:
> (ljson:get json-data '("First Name") #(json))
#"\"J\\u00f3n\""
> (ljson:get json-data '("Address" "City") #(json))
#"\"T\\u00f3rshavn\""
> (ljson:get json-data '("Phone Numbers" 1 "Type") #(json))
#"\"home\""
Under the Deck ↟
The Argonauts that are rowing this thing consist of the following:
- mochijson2 - for encoding of nested data
- jsx - for decoding,
prettify
andminify
- dict - (wrapped as
pairs
) for large key/value lists - proplists/lists of tuples - for small key/value lists
License ↟
Apache Version 2 License
Copyright © 2014-2015, Dreki Þórgísl
Copyright © 2015, arpunk
Copyright © 2015-2019, Duncan McGreggor [email protected]