Giter VIP home page Giter VIP logo

translator.json's Introduction

translator.json

Parses and translates json to clojure data structures

Usage

Add to deps.edn:

{org.fversnel/translator.json {:git/url "https://github.com/fversnel/translator.json"
                               :sha "..."}}
(require '[clojure.java.io]) ;; optional
(require :reload '[org.fversnel.translator.json :as json])

Given the following json string:

(def json-string
  "{\"string\": \"value\",
    \"number\": 42,
    \"list\": [42, 43],
    \"map\": {\"inner-string\": \"inner-value\",
              \"inner-list\": [56, 67]}}")

(def json-value
  (json/parse 
    {::json/keywordize-keys? true}
    json-string))

translator.json uses a protocol to extend types that can be parsed as json.

(json/parse json-string)
(json/parse (.getBytes json-string))
(json/parse (clojure.java.io/input-stream (.getBytes json-string)))
(json/parse (java.io.StringReader. json-string))

Additionally you can pass a translation directive to the parser that will help translate your json to the desired clojure data structure:

(json/parse
  {::json/translation 
    {"string" ::string
    ;; Just as keys can be translated, values can be as well
    ;; In this case we take the value and increment it
    "number" [::number inc]
    "list" [::list inc]
    ;; to translate the 'outer' key you can optionally specify
    ;; the new name of the key in the map describing the translation
    "map" {::json/translated-key ::map
           "inner-string" [::inner-string (partial str "prefix-")]
           "inner-list" [::inner-list dec]}}}
  json-string)

A merge directive can be given to merge an inner map into the outer map:

(json/parse
  {::json/translation
    {"string" ::string
    ;; Just as keys can be translated, values can be as well
    ;; In this case we take the value and increment it
    "number" [::number inc]
    "list" [::list inc]
    ;; Sometimes json maps are uselessly nested
    ;; With clojure's namespaced keys we can easily merge maps without conflicts
    ;; With the merge-with-parent? directive we will automatically merge the
    ;; inner map into the outer map
    "map" {::json/merge-with-parent? true
            "inner-string" [::inner-string (partial str "prefix-")]
            "inner-list" [::inner-list dec]}}}
  json-string)

translator.json's People

Contributors

fversnel avatar

Watchers

 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.