Code Monkey home page Code Monkey logo

edn.el's Introduction

Edn.el

Edn.el is an emacs lisp library for reading and writing the data format edn.

Installation

I highly recommend installing edn.el through elpa.

It's available on melpa:

M-x package-install edn

Examples

;;; Reading
(edn-read "[(:foo bar :bar 12 ) \"foo\"]")
;; => [(:foo bar :bar 12 ) "foo"]

;;; Writing
(edn-print-string [(:foo bar :bar 12 ) \"foo\"])
;; => "[(:foo bar :bar 12 ) \"foo\"]"

;;; Using the internal representations
(edn-set-p (edn-list-to-set '(1 2 3 3)))
;; => t
(edn-set-to-list (edn-list-to-set '(1 2 3 3)))
;; => (1 2 3)

(defvar time (edn-inst-to-time (edn-read "#inst \"1985-04-12T23:20:50.52Z\"")))
;; => (7357 47698)
(edn-inst-p (edn-time-to-inst time))
;; => t

(defvar uuid (edn-read "#uuid  \"f81d4fae-7dec-11d0-a765-00a0c91e6bf6\""))
(edn-uuid-p uuid)
;; => t
(edn-uuid-to-string uuid)
;; => "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"

;;; Custom readers
(edn-read "#my/handler value")
;; => error unknown handler
(edn-add-reader :my/reader (lambda (val) :val))
(edn-read "#my/handler value")
;; => :val

;;; Custom writers
(cl-defstruct my-type (val))
(edn-print-string (make-my-type :val))
;; => "[cl-struct-my-type \"val\"]"
(edn-add-writer #'my-type-p (lambda (v) (concat "#my/value " (my-type-val v))))
(edn-print-string (make-my-type :val))
;; => :val

Known limitations

Set representation

Emacs lisp doesn't have have a data structure dedicated to sets. In emacs lisp, and in other lisps like common lisp, sets are just lists without duplicate elements. This means that when outputting edn and we encounter a list without duplicates we can't know if we should write a set or not. There are three solutions to this problem:

  1. Let the user tag the lists that should be turned into sets prior to serialization.
  2. Always create lists, unless the user passes an option to eagerly create sets.
  3. Use our own datastructure to represent sets.

I've chosen to take the third approach.

Time instances and UUIDs

The problem of representation also arises with regard to instants in time and UUIDs. I've opted to create an internal representation here as well.

Bignums

Elisp doesn't have support for bignums without pulling in calc or an external lib. For now I've opted to just throw an error when edn containing bignums is put on the input stream.

Is it any good?

Yes!

Contribute

Please do! There is a suite of tests that I'd like you to add to whenever a bug is fixed or a new feature is added. If you don't do this I'm likely to break your code when I stumble around the codebase.

To fetch the test dependencies, install cask if you haven't already, then:

$ cd /path/to/edn.el
$ cask

Run the tests with:

$ make test

License

Copyright (c) 2015, Lars Andersen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

edn.el's People

Contributors

expez avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.