dhall-json-1.0.0: Compile Dhall to JSON or YAML

Safe HaskellNone




This library only exports a single dhallToJSON function for translating a Dhall syntax tree to a JSON syntax tree (i.e. a Value) for the aeson library

NOTE: The yaml library uses the same Value type to represent YAML files, so you can use this to convert Dhall expressions to YAML, too

See the dhall package if you would like to transform Dhall source code into a Dhall syntax tree. Similarly, see the aeson package if you would like to translate a JSON syntax tree into JSON.

This package also provides dhall-to-json and dhall-to-yaml executables which you can use to compile Dhall source code directly to JSON or YAML for your convenience

Not all Dhall expressions can be converted to JSON since JSON is not a programming language. The only things you can convert are:

  • Bools
  • Naturals
  • Integers
  • Doubles
  • Text
  • Lists
  • Optional values
  • records

Dhall Bools translate to JSON bools:

$ dhall-to-json <<< 'True'
$ dhall-to-json <<< 'False'

Dhall numbers translate to JSON numbers:

$ dhall-to-json <<< '+2'
$ dhall-to-json <<< '2'
$ dhall-to-json <<< '2.3'

Dhall Text translates to JSON text:

$ dhall-to-json <<< '"ABC"'

Dhall Lists translate to JSON lists:

$ dhall-to-json <<< '[1, 2, 3] : List Integer'

Dhall Optional values translate to null if absent and the unwrapped value otherwise:

$ dhall-to-json <<< '[] : Optional Integer'
$ dhall-to-json <<< '[1] : Optional Integer'

Dhall records translate to JSON records:

$ dhall-to-json <<< '{ foo = 1, bar = True }'

Note that you cannot convert Dhall unions since JSON does not support sum types. You will need to convert your unions to use the above types within Dhall before translating to JSON

Also, all Dhall expressions are normalized before translation to JSON:

$ dhall-to-json <<< "True == False"


Dhall to JSON

dhallToJSON :: Expr s X -> Either CompileError Value Source #

Convert a Dhall expression to the equivalent Nix expression

>>> :set -XOverloadedStrings
>>> :set -XOverloadedLists
>>> import Dhall.Core
>>> dhallToJSON (RecordLit [("foo", IntegerLit 1), ("bar", TextLit "ABC")])
Right (Object (fromList [("foo",Number 1.0),("bar",String "ABC")]))
>>> fmap Data.Aeson.encode it
Right "{\"foo\":1,\"bar\":\"ABC\"}"


data CompileError Source #

This is the exception type for errors that might arise when translating Dhall to JSON

Because the majority of Dhall language features do not translate to JSON this just returns the expression that failed


Unsupported (Expr X X)