Extensible Data Notation (EDN) is a format for serializing data.
EDN is a subset of the syntax used by Clojure. Reading data defined by EDN is safer than that defined by the full Clojure syntax, especially from untrusted sources. EDN is restricted to data, no code. It is similar in intent to JSON. Though it is more commonly used in Clojure, there are implementations of EDN for many other languages.
The main benefit of EDN over JSON and YAML is that it is extensible. We will see how it is extended later on.
1; Comments start with a semicolon.
2; Anything after the semicolon is ignored.
3
4;;;;;;;;;;;;;;;;;;;
5;;; Basic Types ;;;
6;;;;;;;;;;;;;;;;;;;
7
8nil ; also known in other languages as null
9
10; Booleans
11true
12false
13
14; Strings are enclosed in double quotes
15"hungarian breakfast"
16"farmer's cheesy omelette"
17
18; Characters are preceded by backslashes
19\g \r \a \c \e
20
21; Keywords start with a colon. They behave like enums. Kind of
22; like symbols in Ruby.
23:eggs
24:cheese
25:olives
26
27; Symbols are used to represent identifiers.
28; You can namespace symbols by using /. Whatever precedes / is
29; the namespace of the symbol.
30spoon
31kitchen/spoon ; not the same as spoon
32kitchen/fork
33github/fork ; you can't eat with this
34
35; Integers and floats
3642
373.14159
38
39; Lists are sequences of values
40(:bun :beef-patty 9 "yum!")
41
42; Vectors allow random access
43[:gelato 1 2 -2]
44
45; Maps are associative data structures that associate the key with its value
46{:eggs 2
47 :lemon-juice 3.5
48 :butter 1}
49
50; You're not restricted to using keywords as keys
51{[1 2 3 4] "tell the people what she wore",
52 [5 6 7 8] "the more you see the more you hate"}
53
54; You may use commas for readability. They are treated as whitespace.
55
56; Sets are collections that contain unique elements.
57#{:a :b 88 "huat"}
58
59;;;;;;;;;;;;;;;;;;;;;;;
60;;; Tagged Elements ;;;
61;;;;;;;;;;;;;;;;;;;;;;;
62
63; EDN can be extended by tagging elements with # symbols.
64
65#MyYelpClone/MenuItem {:name "eggs-benedict" :rating 10}
66
67; Let me explain this with a Clojure example. Suppose I want to transform that
68; piece of EDN into a MenuItem record.
69
70(defrecord MenuItem [name rating])
71
72; defrecord defined, among other things, map->MenuItem which will take a map
73; of field names (as keywords) to values and generate a user.MenuItem record
74
75; To transform EDN to Clojure values, I will need to use the built-in EDN
76; reader, clojure.edn/read-string
77
78(clojure.edn/read-string "{:eggs 2 :butter 1 :flour 5}")
79; -> {:eggs 2 :butter 1 :flour 5}
80
81; To transform tagged elements, pass to clojure.edn/read-string an option map
82; with a :readers map that maps tag symbols to data-reader functions, like so
83
84(clojure.edn/read-string
85 {:readers {'MyYelpClone/MenuItem map->MenuItem}}
86 "#MyYelpClone/MenuItem {:name \"eggs-benedict\" :rating 10}")
87; -> #user.MenuItem{:name "eggs-benedict", :rating 10}