lens-aeson-1.0.0.5: Law-abiding lenses for aeson

Copyright | (c) Edward Kmett 2013-2014, (c) Paul Wilson 2012
License | BSD3
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | experimental
Portability | non-portable
Safe Haskell | Trustworthy
Language | Haskell98

Data.Aeson.Lens

Contents

- Numbers
- Primitive
- Objects and Arrays
- Decoding

Description

This module also exports orphan ``Ixed` `Value`` and ``Plated` `Value`` instances.

Synopsis

- class AsNumber t where
  - _Number :: Prism' t Scientific
  - _Double :: Prism' t Double
  - _Integer :: Prism' t Integer
- _Integral :: (AsNumber t, Integral a) => Prism' t a
- nonNull :: Prism' Value Value
- data Primitive
  - = StringPrim !Text
  - | NumberPrim !Scientific
  - | BoolPrim !Bool
  - | NullPrim
- class AsNumber t => AsPrimitive t where
  - _Primitive :: Prism' t Primitive
  - _String :: Prism' t Text
  - _Bool :: Prism' t Bool
  - _Null :: Prism' t ()
- class AsPrimitive t => AsValue t where
  - _Value :: Prism' t Value
  - _Object :: Prism' t (HashMap Text Value)
  - _Array :: Prism' t (Vector Value)
- key :: AsValue t => Text -> Traversal' t Value
- members :: AsValue t => IndexedTraversal' Text t Value
- nth :: AsValue t => Int -> Traversal' t Value
- values :: AsValue t => IndexedTraversal' Int t Value
- class AsJSON t where
  - _JSON :: (FromJSON a, ToJSON a) => Prism' t a

# Numbers

class AsNumber t where Source

Minimal complete definition

Nothing

Methods

_Number :: Prism' t Scientific Source

    >>> "[1, \"x\"]" ^? nth 0 . _Number
    Just 1.0

    >>> "[1, \"x\"]" ^? nth 1 . _Number
    Nothing

_Double :: Prism' t Double Source

Prism into an `Double` over a `Value`, `Primitive` or `Scientific`

    >>> "[10.2]" ^? nth 0 . _Double
    Just 10.2

_Integer :: Prism' t Integer Source

Prism into an `Integer` over a `Value`, `Primitive` or `Scientific`

    >>> "[10]" ^? nth 0 . _Integer
    Just 10

    >>> "[10.5]" ^? nth 0 . _Integer
    Just 10

    >>> "42" ^? _Integer
    Just 42

Instances

AsNumber String Source |
AsNumber ByteString Source |
AsNumber Scientific Source |
AsNumber ByteString Source |
AsNumber Text Source |
AsNumber Value Source |
AsNumber Text Source |
AsNumber Primitive Source |

_Integral :: (AsNumber t, Integral a) => Prism' t a Source

Access Integer `Value`s as Integrals.

    >>> "[10]" ^? nth 0 . _Integral
    Just 10

    >>> "[10.5]" ^? nth 0 . _Integral
    Just 10

nonNull :: Prism' Value Value Source

Prism into non-`Null` values

    >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "a" . nonNull
    Just (String "xyz")

    >>> "{\"a\": {}, \"b\": null}" ^? key "a" . nonNull
    Just (Object (fromList []))

    >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "b" . nonNull
    Nothing

# Primitive

data Primitive Source

Primitives of `Value`

Constructors

StringPrim !Text |
NumberPrim !Scientific |
BoolPrim !Bool |
NullPrim |

Instances

Eq Primitive Source |
Data Primitive Source |
Ord Primitive Source |
Show Primitive Source |
AsPrimitive Primitive Source |
AsNumber Primitive Source |

class AsNumber t => AsPrimitive t where Source

Minimal complete definition

Nothing

Methods

_Primitive :: Prism' t Primitive Source

    >>> "[1, \"x\", null, true, false]" ^? nth 0 . _Primitive
    Just (NumberPrim 1.0)

    >>> "[1, \"x\", null, true, false]" ^? nth 1 . _Primitive
    Just (StringPrim "x")

    >>> "[1, \"x\", null, true, false]" ^? nth 2 . _Primitive
    Just NullPrim

    >>> "[1, \"x\", null, true, false]" ^? nth 3 . _Primitive
    Just (BoolPrim True)

    >>> "[1, \"x\", null, true, false]" ^? nth 4 . _Primitive
    Just (BoolPrim False)

_String :: Prism' t Text Source

    >>> "{\"a\": \"xyz\", \"b\": true}" ^? key "a" . _String
    Just "xyz"

    >>> "{\"a\": \"xyz\", \"b\": true}" ^? key "b" . _String
    Nothing

    >>> _Object._Wrapped # [("key" :: Text, _String # "value")] :: String
    "{\"key\":\"value\"}"

_Bool :: Prism' t Bool Source

    >>> "{\"a\": \"xyz\", \"b\": true}" ^? key "b" . _Bool
    Just True

    >>> "{\"a\": \"xyz\", \"b\": true}" ^? key "a" . _Bool
    Nothing

    >>> _Bool # True :: String
    "true"

    >>> _Bool # False :: String
    "false"

_Null :: Prism' t () Source

    >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "b" . _Null
    Just ()

    >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "a" . _Null
    Nothing

    >>> _Null # () :: String
    "null"

Instances

AsPrimitive String Source |
AsPrimitive ByteString Source |
AsPrimitive ByteString Source |
AsPrimitive Text Source |
AsPrimitive Value Source |
AsPrimitive Text Source |
AsPrimitive Primitive Source |

# Objects and Arrays

class AsPrimitive t => AsValue t where Source

Minimal complete definition

Nothing

Methods

_Value :: Prism' t Value Source

    >>> preview _Value "[1,2,3]" == Just (Array (Vector.fromList [Number 1.0,Number 2.0,Number 3.0]))
    True

_Object :: Prism' t (HashMap Text Value) Source

    >>> "{\"a\": {}, \"b\": null}" ^? key "a" . _Object
    Just (fromList [])

    >>> "{\"a\": {}, \"b\": null}" ^? key "b" . _Object
    Nothing

    >>> _Object._Wrapped # [("key" :: Text, _String # "value")] :: String
    "{\"key\":\"value\"}"

_Array :: Prism' t (Vector Value) Source

    >>> preview _Array "[1,2,3]" == Just (Vector.fromList [Number 1.0,Number 2.0,Number 3.0])
    True

Instances

AsValue String Source |
AsValue ByteString Source |
AsValue ByteString Source |
AsValue Text Source |
AsValue Value Source |
AsValue Text Source |

key :: AsValue t => Text -> Traversal' t Value Source

Like `ix`, but for `Object` with Text indices. This often has better inference than `ix` when used with OverloadedStrings.

    >>> "{\"a\": 100, \"b\": 200}" ^? key "a"
    Just (Number 100.0)

    >>> "[1,2,3]" ^? key "a"
    Nothing

members :: AsValue t => IndexedTraversal' Text t Value Source

An indexed Traversal into Object properties

    >>> "{\"a\": 4, \"b\": 7}" ^@.. members
    [("a",Number 4.0),("b",Number 7.0)]

    >>> "{\"a\": 4, \"b\": 7}" & members . _Number *~ 10
    "{\"a\":40,\"b\":70}"

nth :: AsValue t => Int -> Traversal' t Value Source

Like `ix`, but for Arrays with Int indexes

    >>> "[1,2,3]" ^? nth 1
    Just (Number 2.0)

    >>> "\"a\": 100, \"b\": 200}" ^? nth 1
    Nothing

    >>> "[1,2,3]" & nth 1 .~ Number 20
    "[1,20,3]"

values :: AsValue t => IndexedTraversal' Int t Value Source

An indexed Traversal into Array elements

    >>> "[1,2,3]" ^.. values
    [Number 1.0,Number 2.0,Number 3.0]

    >>> "[1,2,3]" & values . _Number *~ 10
    "[10,20,30]"

# Decoding

class AsJSON t where Source

Minimal complete definition

Nothing

Methods

_JSON :: (FromJSON a, ToJSON a) => Prism' t a Source

`_JSON` is a `Prism` from something containing JSON to something encoded in that structure

Instances

AsJSON String Source |
AsJSON ByteString Source |
AsJSON ByteString Source |
AsJSON Text Source |
AsJSON Value Source |
AsJSON Text Source |