learnxinyminutes-docs/edn.html.markdown

115 lines
3.2 KiB
Markdown
Raw Normal View History

2015-10-20 14:26:37 +00:00
---
2024-10-20 21:46:35 +00:00
language: EDN
2015-10-20 14:26:37 +00:00
filename: learnedn.edn
contributors:
- ["Jason Yeo", "https://github.com/jsyeo"]
- ["Jonathan D Johnston", "https://github.com/jdjohnston"]
2015-10-20 14:26:37 +00:00
---
2015-10-31 19:27:44 +00:00
Extensible Data Notation (EDN) is a format for serializing data.
2015-10-20 14:26:37 +00:00
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.
2015-10-20 14:26:37 +00:00
2015-10-31 19:27:44 +00:00
The main benefit of EDN over JSON and YAML is that it is extensible. We
will see how it is extended later on.
2015-10-20 14:26:37 +00:00
2016-02-13 07:04:31 +00:00
```clojure
2015-10-20 14:26:37 +00:00
; Comments start with a semicolon.
2015-10-31 00:33:13 +00:00
; Anything after the semicolon is ignored.
2015-10-20 14:26:37 +00:00
;;;;;;;;;;;;;;;;;;;
;;; Basic Types ;;;
;;;;;;;;;;;;;;;;;;;
2015-10-28 09:46:57 +00:00
nil ; also known in other languages as null
2015-10-20 14:26:37 +00:00
; Booleans
true
false
; Strings are enclosed in double quotes
"hungarian breakfast"
"farmer's cheesy omelette"
2017-08-23 08:14:39 +00:00
; Characters are preceded by backslashes
2015-10-20 14:26:37 +00:00
\g \r \a \c \e
2015-10-28 09:51:18 +00:00
; Keywords start with a colon. They behave like enums. Kind of
2015-10-28 09:46:57 +00:00
; like symbols in Ruby.
2015-10-20 14:26:37 +00:00
:eggs
:cheese
:olives
; Symbols are used to represent identifiers.
2017-08-23 08:14:39 +00:00
; You can namespace symbols by using /. Whatever precedes / is
; the namespace of the symbol.
spoon
kitchen/spoon ; not the same as spoon
kitchen/fork
github/fork ; you can't eat with this
2015-10-20 14:26:37 +00:00
; Integers and floats
42
3.14159
2015-10-28 09:46:57 +00:00
; Lists are sequences of values
2015-10-20 14:26:37 +00:00
(:bun :beef-patty 9 "yum!")
; Vectors allow random access
[:gelato 1 2 -2]
2015-10-31 19:27:44 +00:00
; Maps are associative data structures that associate the key with its value
2015-10-20 14:26:37 +00:00
{:eggs 2
:lemon-juice 3.5
:butter 1}
; You're not restricted to using keywords as keys
{[1 2 3 4] "tell the people what she wore",
[5 6 7 8] "the more you see the more you hate"}
2015-10-31 19:27:44 +00:00
; You may use commas for readability. They are treated as whitespace.
2015-10-20 14:26:37 +00:00
; Sets are collections that contain unique elements.
#{:a :b 88 "huat"}
;;;;;;;;;;;;;;;;;;;;;;;
;;; Tagged Elements ;;;
;;;;;;;;;;;;;;;;;;;;;;;
2015-10-20 14:26:37 +00:00
; EDN can be extended by tagging elements with # symbols.
#MyYelpClone/MenuItem {:name "eggs-benedict" :rating 10}
; Let me explain this with a Clojure example. Suppose I want to transform that
2015-10-31 19:27:44 +00:00
; piece of EDN into a MenuItem record.
2015-10-20 14:26:37 +00:00
(defrecord MenuItem [name rating])
; defrecord defined, among other things, map->MenuItem which will take a map
; of field names (as keywords) to values and generate a user.MenuItem record
2015-10-20 14:26:37 +00:00
; To transform EDN to Clojure values, I will need to use the built-in EDN
; reader, clojure.edn/read-string
(clojure.edn/read-string "{:eggs 2 :butter 1 :flour 5}")
2015-10-20 14:26:37 +00:00
; -> {:eggs 2 :butter 1 :flour 5}
; To transform tagged elements, pass to clojure.edn/read-string an option map
; with a :readers map that maps tag symbols to data-reader functions, like so
2015-10-20 14:26:37 +00:00
(clojure.edn/read-string
{:readers {'MyYelpClone/MenuItem map->MenuItem}}
"#MyYelpClone/MenuItem {:name \"eggs-benedict\" :rating 10}")
2015-10-20 14:26:37 +00:00
; -> #user.MenuItem{:name "eggs-benedict", :rating 10}
```
# References
- [EDN spec](https://github.com/edn-format/edn)
- [Implementations](https://github.com/edn-format/edn/wiki/Implementations)
- [Tagged Elements](http://www.compoundtheory.com/clojure-edn-walkthrough/)