Giter Club home page Giter Club logo

into-edn's Introduction

Available via clojars,
Current stable version: [into-edn "1.0.2"] Build Status

into-edn

into-edn is a Clojure library to simplify the conversion of data from one format, into Clojure EDN. It does not handle the reading or parsing of data, but instead provides a standard method for defining the EDN format as well as how to generate it.

Why write this library?
Far too often when dealing with XML, Java objects, etc; I end up converting them to a Clojure map. This was written to simplify and standardize that process across libraries.

Usage

Handling a Java object

(require '[into-edn :refer [into-edn]])

(def string-spec
  {:bytes  #(.getBytes %)
   :length #(.length %)
   :hash   #(.hashCode %)
   :empty? #(.isEmpty %)})

(into-edn string-spec "Hello World")
;; -->
{:bytes [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100],
 :length 11,
 :hash -862545276,
 :empty? false}

Handling XML

(require '[into-edn :refer [into-edn]]
         '[clojure.zip :as zip]
         '[clojure.data.xml :as xml]
         '[clojure.data.zip.xml :as zf])

(def xml-string "
<responses>
  <response status=\"okay\" code=\"200\">
    <ident>1234</ident>
    <title lang=\"English\">Creativity fails me</title>
    <children>
      <child><name>Humphrey</name><age>3</age></child>
      <child><name>Laura</name><age>5</age></child>
      <child><name>Mikey</name><age>7</age></child>
    </children>
  </response>
</responses>")

(def xml-spec
  [#(zf/xml-> % :response)
   {:into-edn/into #(some-> % (zf/xml1-> zip/node) :attrs)
    :ident  #(some-> %
                     (zf/xml1-> :ident zf/text)
                     (Long/parseLong))
    :title #(zf/xml1-> % :title zf/text)
    :title-lang #(zf/xml1-> % :title (zf/attr :lang))
    :children [#(zf/xml-> % :children :child)
               {:name #(zf/xml1-> % :name zf/text)
                :age  #(some-> %
                               (zf/xml1-> :age zf/text)
                               (Long/parseLong))}]}])


(into-edn xml-spec (some-> xml-string
                           xml/parse-str
                           zip/xml-zip))
;; -->
[{:status "okay",
  :code "200",
  :ident 1234,
  :title "Creativity fails me",
  :title-lang "English",
  :children
  [{:name "Humphrey", :age 3}
   {:name "Laura", :age 5}
   {:name "Mikey", :age 7}]}]

Artifacts

into-edn artifacts are released to Clojars.

If you are using Maven, add the following repository definition to your pom.xml:

<repository>
  <id>clojars</id>
  <url>http://clojars.org/repo</url>
</repository>

The Most Recent Release

With Leiningen:

    [into-edn "1.0.2"]

With Maven:

    <dependency>
      <groupId>into-edn</groupId>
      <artifactId>into-edn</artifactId>
      <version>1.0.2</version>
    </dependency>

License

MIT http://opensource.org/licenses/MIT

Copyright © 2013 Alan Busby, 2018 Francois Rey

into-edn's People

Contributors

thebusby 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.