Hacker News new | ask | show | jobs
by leiferik 1354 days ago
Some advantages of EDN:

* Can represent sets `#{1 2 3 4 "foo" "bar" true false}` (JSON only supports arrays)

* Maps/sets can contain arbitrary EDN as keys/values. `{[1 2] "foo", 5 :a, "5" :b, true -1, false 5}` or `#{[1 2] #{3 4} (5 6)}` (JSON only supports string keys on "objects")

* Supports clojure types like keywords (`:foo`, `:bar/baz`) and symbols (`foo`, `bar/baz`), and can be extended to support other values as well

3 comments

EDN also contains support for date and UUID literals, the former of which is a huge pain when using JSON and needing to communicate which standard you're using and remember to encode/decode on each side of the wire.
Plus you can even write your own reader literals to enhance your data format. Aero does this well/horribly depending on your taste but it's a cool mechanism for declarative formats.
I could probably look it up myself, but does EDN support comments?
Yep, two types in fact, line comments with ; and a discard sequence - you can tag code that is read (so it must be correct), but then discarded. We have edn files that are well documented with comments.

https://github.com/edn-format/edn#comments

I thought so — then it is strictly better than JSON.
1. Objects are sets of keys

2. JS has Map/Set which allow other composite types as keys and can be converted to objects for JSON serialization then deserialize back to Map/Set.

Yeah but still you have to admit, there's quite a lot more friction and ambiguity in that approach. Do I represent a map as

    [
      ["key", "value"],
      ["otherKey", "otherValue"]
    ]  
  
or

    [
      {"key": "key", "value": "value"},
      {"key": "otherKey", "value":"otherValue"}
    ]  
or something else? And how can I distinguish between values which just happen to look like maps but are actually not maps? And what if the service I'm talking to does it differently? What if I'm comparing or sorting two JSON values, don't I have to modify the equality/hashcode/ordering logic now to interpret

    [
      ["key", "value"],
      ["otherKey", "otherValue"]
    ] 
and

    [
      ["otherKey", "otherValue"],
      ["key", "value"]
    ] 
as being equal and implement a new hashcode/ordering which treats them as equal?

Quite a lot easier when this is just native to the format.

JS Map/Set use referential equality, which is pretty worthless if you aren't dealing with primitives.