Skip to content

Utilities

Jude Payne edited this page Apr 8, 2024 · 9 revisions

Conversion to/ from json

The dictim.json names provides two public functions to-json and from-json for converting dictim to json and back. Under the covers, the Cheshire library is used.

Although dictim is made up of regular nested Clojure data structures and usually conversion to/ from json is simple for Clojure programmers, dictim's syntax has one gotcha; connection-references are in a map and the key is a vector, for example

{[:x "->" :y [0]] {:style.stroke "red"}}

Json does not allow arrays (or maps) as keys, so special processing is necessary in the from-json function.

Please see the Dictim Syntax wiki page for more details on connection references.

Validation

The dictim.validate namespace provides the functions valid? and all-valid? for validating dictim. The first for a single element, the second for a collection of multiple elements.

user> (in-ns 'dictim.validate)
#namespace[dictim.validate]
dictim.validate> (def elem
                   [:mike
                    "Old friends"
                    [:comment "a diagram of friends"]
                    [:t1
                     "T Boggs expanded"
                     [:tris "TAB"]
                     [:mads "Madeline"]
                     [:tris "--" :maddie "wedding bells?"]]])
#'dictim.validate/elem
dictim.validate> (valid? elem)
true

A meaningful error will be thrown if a particular element fails validation.

Flat Dictim

(Also covered on the Dictim Syntax page)

Flat dictim is a secondary format (to dictim itself) where every element is represented as a Clojure map with the same set of keys. Flat dictim is easier to work with than dictim itself due to its flat, homogenous nature. Example use cases are for flattening before storing, or flattening in order to insert or delete nodes.

The dictim.flat namespace provides two public functions for working with flat dictim.

To convert from dictim to flat dictim, use flat

dictim.flat> (in-ns 'dictim.flat)
#namespace[dictim.flat]
dictim.flat> (def ex '({:direction "right"}
                       [:mike "Old friends"
                        [:comment "a diagram of friends"]
                        [:t1 "T Boggs expanded"
                         [:tris "TAB"]
                         [:mads "Madeline"]
                         [:tris "--" :maddie "wedding bells?"]]]))
#'dictim.flat/ex
dictim.flat> (flat ex)
({:type :attrs, :key {:direction "right"}, :meta nil, :parent nil}
 {:type :ctr, :key :mike, :meta {:label "Old friends"}, :parent nil}
 {:type :cmt, :key "a diagram of friends", :meta nil, :parent :mike}
 {:type :ctr,
  :key :t1,
  :meta {:label "T Boggs expanded"},
  :parent :mike}
 {:type :shape, :key :tris, :meta {:label "TAB"}, :parent :t1}
 {:type :shape, :key :mads, :meta {:label "Madeline"}, :parent :t1}
 {:type :conn,
  :key [:tris "--" :maddie],
  :meta {:label "wedding bells?"},
  :parent :t1})

To convert it back again, use build

dictim.flat> (build (flat ex))
({:direction "right"}
 [:mike
  "Old friends"
  [:comment "a diagram of friends"]
  [:t1
   "T Boggs expanded"
   [:tris "TAB"]
   [:mads "Madeline"]
   [:tris "--" :maddie "wedding bells?"]]])
Clone this wiki locally