tyro-0.1.1.1: Type derived JSON parsing using Aeson

Copyright(c) Richard Lupton 2017
LicenseBSD-3
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Data.Tyro

Contents

Description

 

Synopsis

Introduction

A small (artificial) example demonstrating how to use the types defined here.

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
import Data.Tyro
import Data.Aeson (decode)
import Data.Text (Text)

json = "{\"key1\":[{\"key2\":41},{\"key2\":42}]}" :: Text

-- Extract [41, 42] inside the Tyro types
parsed = decode json :: Maybe ("key1" |>| List ("key2" |>| Parse Integer))

-- We can dispose of the types using unwrap: 'values' will have the value
-- Just [41, 42]
values :: Maybe [Integer]
values = fmap unwrap parsed

Building types

type Parse a = JSBranch '[] a Source #

Parse a represents trying to parse JSON to an a.

type family (x :: Symbol) |>| (b :: *) :: * Source #

The type operator |>| provides a way of describing how to walk down a JSON tree.

Instances

type x |>| (JSBranch xs a) Source # 
type x |>| (JSBranch xs a) = JSBranch ((:) Symbol x xs) a

type family List (x :: *) :: * Source #

The List type operator constructs a parsing type for parsing a list of JSON objects.

Instances

type List (JSBranch xs a) Source # 
type List (JSBranch xs a) = Parse [JSBranch xs a]

unwrap :: JSBranch xs a -> a Source #

unwrap unwraps a value from it's parsing type.

Internal types

data JSBranch :: [Symbol] -> * -> * Source #

JSBranch is a dependent datatype which represents a walk down a JSON tree. JSBranch ["key1", "key2"] a represents the walk "take the value at key1 and then the value at key2, and (try to) interpret that as an a".

Instances

(FromJSON a, SingI [Symbol] xs) => FromJSON (JSBranch xs a) Source # 
type x |>| (JSBranch xs a) Source # 
type x |>| (JSBranch xs a) = JSBranch ((:) Symbol x xs) a
type List (JSBranch xs a) Source # 
type List (JSBranch xs a) = Parse [JSBranch xs a]