lens-aeson-1.0.2/0000755000000000000000000000000013136745314011753 5ustar0000000000000000lens-aeson-1.0.2/.ghci0000644000000000000000000000012513136745314012664 0ustar0000000000000000:set -isrc -idist/build/autogen -optP-include -optPdist/build/autogen/cabal_macros.h lens-aeson-1.0.2/CHANGELOG.markdown0000644000000000000000000000110513136745314015003 0ustar00000000000000001.0.2 ----- * Support `doctest-0.12` 1.0.1 ----- * Revamp `Setup.hs` to use `cabal-doctest`. This makes it build with `Cabal-2.0`, and makes the `doctest`s work with `cabal new-build` and sandboxes. 1.0.0.5 ---- * Fix tests to work against vector-0.11 * Documentation fixes * No functional changes since 1.0.0.4 1.0.0.3 ---- * Move lens upper bound to < 5 like the other packages in the family 1 ---- * Module migrated from lens package to Data.Aeson.Lens 0.1.2 ----- * Added `members` and `values` 0.1.1 ----- * Broadened dependencies 0.1 --- * Repository initialized lens-aeson-1.0.2/AUTHORS.markdown0000644000000000000000000000125213136745314014644 0ustar0000000000000000This project was started by Paul Wilson [@statusfailed](https://github.com/statusfailed). Edward Kmett stole it and decided he was going to polish it up and put it on hackage. In the process he took all sorts of liberties with the structure of the project. If you don't like the result, it is probably his fault. * [Edward Kmett](mailto:ekmett@gmail.com) [@ekmett](https://github.com/ekmett) You can watch contributors carry on the quest for bragging rights in the [contributors graph](https://github.com/analytics/compensated/graphs/contributors). Omission from this list is by no means an attempt to discount your contribution. Thank you for all of your help! -Edward Kmett lens-aeson-1.0.2/README.markdown0000644000000000000000000000115613136745314014457 0ustar0000000000000000lens-aeson ========== [![Hackage](https://img.shields.io/hackage/v/lens-aeson.svg)](https://hackage.haskell.org/package/lens-aeson) [![Build Status](https://secure.travis-ci.org/ekmett/linear.svg)](http://travis-ci.org/lens/lens-aeson) The goal of `lens-aeson` is to provide traversals and prisms for the [Aeson](http://hackage.haskell.org/package/aeson) library's Value type, while obeying the `Traversal`/`Prism` laws. Contact Information ------------------- Contributions and bug reports are welcome! Please feel free to contact me through github or on the #haskell IRC channel on irc.freenode.net. -Edward Kmett lens-aeson-1.0.2/lens-aeson.cabal0000644000000000000000000000400513136745314015002 0ustar0000000000000000name: lens-aeson category: Numeric version: 1.0.2 license: MIT cabal-version: >= 1.8 license-file: LICENSE author: Edward A. Kmett maintainer: Edward A. Kmett stability: provisional homepage: http://github.com/lens/lens-aeson/ bug-reports: http://github.com/lens/lens-aeson/issues copyright: Copyright (C) 2012 Paul Wilson Copyright (C) 2013 Edward A. Kmett build-type: Custom tested-with: GHC == 7.4.2, GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.3, GHC==8.0.2, GHC==8.2.1 synopsis: Law-abiding lenses for aeson description: Law-abiding lenses for aeson extra-source-files: .travis.yml .ghci .gitignore .vim.custom travis/cabal-apt-install travis/config AUTHORS.markdown README.markdown CHANGELOG.markdown Warning.hs source-repository head type: git location: https://github.com/lens/lens-aeson custom-setup setup-depends: base >= 4 && < 5, Cabal, cabal-doctest >= 1 && < 1.1 -- You can disable the doctests test suite with -f-test-doctests flag test-doctests default: True manual: True library build-depends: base >= 4.5 && < 5, lens >= 4.4 && < 5, text >= 0.11.1.10 && < 1.3, vector >= 0.9 && < 0.13, unordered-containers >= 0.2.3 && < 0.3, attoparsec >= 0.10 && < 0.14, bytestring >= 0.9 && < 0.11, aeson >= 0.7.0.5 && < 1.3, scientific >= 0.3.2 && < 0.4 exposed-modules: Data.Aeson.Lens ghc-options: -Wall -fwarn-tabs -O2 hs-source-dirs: src test-suite doctests type: exitcode-stdio-1.0 main-is: doctests.hs ghc-options: -Wall -threaded hs-source-dirs: tests if !flag(test-doctests) buildable: False else build-depends: base, doctest >= 0.11.1 && < 0.13, generic-deriving, lens-aeson, semigroups >= 0.9, simple-reflect >= 0.3.1 lens-aeson-1.0.2/Setup.lhs0000644000000000000000000000124113136745314013561 0ustar0000000000000000\begin{code} {-# LANGUAGE CPP #-} {-# OPTIONS_GHC -Wall #-} module Main (main) where #ifndef MIN_VERSION_cabal_doctest #define MIN_VERSION_cabal_doctest(x,y,z) 0 #endif #if MIN_VERSION_cabal_doctest(1,0,0) import Distribution.Extra.Doctest ( defaultMainWithDoctests ) main :: IO () main = defaultMainWithDoctests "doctests" #else #ifdef MIN_VERSION_Cabal -- If the macro is defined, we have new cabal-install, -- but for some reason we don't have cabal-doctest in package-db -- -- Probably we are running cabal sdist, when otherwise using new-build -- workflow import Warning () #endif import Distribution.Simple main :: IO () main = defaultMain #endif \end{code} lens-aeson-1.0.2/.vim.custom0000644000000000000000000000137713136745314014070 0ustar0000000000000000" Add the following to your .vimrc to automatically load this on startup " if filereadable(".vim.custom") " so .vim.custom " endif function StripTrailingWhitespace() let myline=line(".") let mycolumn = col(".") silent %s/ *$// call cursor(myline, mycolumn) endfunction " enable syntax highlighting syntax on " search for the tags file anywhere between here and / set tags=TAGS;/ " highlight tabs and trailing spaces set listchars=tab:‗‗,trail:‗ set list " f2 runs hasktags map :exec ":!hasktags -x -c --ignore src" " strip trailing whitespace before saving " au BufWritePre *.hs,*.markdown silent! cal StripTrailingWhitespace() " rebuild hasktags after saving au BufWritePost *.hs silent! :exec ":!hasktags -x -c --ignore src" lens-aeson-1.0.2/Warning.hs0000644000000000000000000000040013136745314013706 0ustar0000000000000000module Warning {-# WARNING ["You are configuring this package without cabal-doctest installed.", "The doctests test-suite will not work as a result.", "To fix this, install cabal-doctest before configuring."] #-} () where lens-aeson-1.0.2/.travis.yml0000644000000000000000000000763013136745314014072 0ustar0000000000000000# This file has been generated -- see https://github.com/hvr/multi-ghc-travis language: c sudo: false cache: directories: - $HOME/.cabsnap - $HOME/.cabal/packages before_cache: - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.tar matrix: include: - env: CABALVER=1.24 GHCVER=7.4.2 compiler: ": #GHC 7.4.2" addons: {apt: {packages: [cabal-install-1.24,ghc-7.4.2,hlint], sources: [hvr-ghc]}} - env: CABALVER=1.24 GHCVER=7.6.3 compiler: ": #GHC 7.6.3" addons: {apt: {packages: [cabal-install-1.24,ghc-7.6.3,hlint], sources: [hvr-ghc]}} - env: CABALVER=1.24 GHCVER=7.8.4 compiler: ": #GHC 7.8.4" addons: {apt: {packages: [cabal-install-1.24,ghc-7.8.4,hlint], sources: [hvr-ghc]}} - env: CABALVER=1.24 GHCVER=7.10.3 compiler: ": #GHC 7.10.3" addons: {apt: {packages: [cabal-install-1.24,ghc-7.10.3,hlint], sources: [hvr-ghc]}} - env: CABALVER=1.24 GHCVER=8.0.2 compiler: ": #GHC 8.0.2" addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.2,hlint], sources: [hvr-ghc]}} - env: CABALVER=2.0 GHCVER=8.2.1 compiler: ": #GHC 8.2.1" addons: {apt: {packages: [cabal-install-2.0,ghc-8.2.1,hlint], sources: [hvr-ghc]}} - env: CABALVER=head GHCVER=head compiler: ": #GHC head" addons: {apt: {packages: [cabal-install-head,ghc-head,hlint], sources: [hvr-ghc]}} allow_failures: - env: CABALVER=head GHCVER=head before_install: - unset CC - export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH install: - cabal --version - echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]" - if [ -f $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz ]; then zcat $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz > $HOME/.cabal/packages/hackage.haskell.org/00-index.tar; fi - travis_retry cabal update -v - sed -i 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config - cabal install --only-dependencies --enable-tests --enable-benchmarks --dry -v > installplan.txt - sed -i -e '1,/^Resolving /d' installplan.txt; cat installplan.txt # check whether current requested install-plan matches cached package-db snapshot - if diff -u installplan.txt $HOME/.cabsnap/installplan.txt; then echo "cabal build-cache HIT"; rm -rfv .ghc; cp -a $HOME/.cabsnap/ghc $HOME/.ghc; cp -a $HOME/.cabsnap/lib $HOME/.cabsnap/share $HOME/.cabsnap/bin $HOME/.cabal/; else echo "cabal build-cache MISS"; rm -rf $HOME/.cabsnap; mkdir -p $HOME/.ghc $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin; cabal install -j --only-dependencies --enable-tests --enable-benchmarks; fi # snapshot package-db on cache miss - if [ ! -d $HOME/.cabsnap ]; then echo "snapshotting package-db to build-cache"; mkdir $HOME/.cabsnap; cp -a $HOME/.ghc $HOME/.cabsnap/ghc; cp -a $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin installplan.txt $HOME/.cabsnap/; fi # Here starts the actual work to be performed for the package under test; # any command which exits with a non-zero exit code causes the build to fail. script: - cabal configure -v2 --enable-tests --enable-benchmarks # -v2 provides useful information for debugging - cabal build # this builds all libraries and executables (including tests/benchmarks) - cabal test - cabal sdist # tests that a source-distribution can be generated - hlint src --cpp-define HLINT - export SRC_TGZ=$(cabal info . | awk '{print $2 ".tar.gz";exit}') ; cd dist/; if [ -f "$SRC_TGZ" ]; then cabal install "$SRC_TGZ"; else echo "expected '$SRC_TGZ' not found"; exit 1; fi notifications: irc: channels: - "irc.freenode.org#haskell-lens" skip_join: true template: - "\x0313lens-aeson\x0f/\x0306%{branch}\x0f \x0314%{commit}\x0f %{message} \x0302\x1f%{build_url}\x0f" # EOF lens-aeson-1.0.2/.gitignore0000644000000000000000000000012213136745314013736 0ustar0000000000000000dist dist-newstyle docs wiki TAGS tags wip .DS_Store .*.swp .*.swo *.o *.hi *~ *# lens-aeson-1.0.2/LICENSE0000644000000000000000000000203713136745314012762 0ustar0000000000000000Copyright (C) 2013 Paul Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. lens-aeson-1.0.2/src/0000755000000000000000000000000013136745314012542 5ustar0000000000000000lens-aeson-1.0.2/src/Data/0000755000000000000000000000000013136745314013413 5ustar0000000000000000lens-aeson-1.0.2/src/Data/Aeson/0000755000000000000000000000000013136745314014460 5ustar0000000000000000lens-aeson-1.0.2/src/Data/Aeson/Lens.hs0000644000000000000000000003155113136745314015722 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE Trustworthy #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE DefaultSignatures #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -------------------------------------------------------------------- -- | -- Copyright : (c) Edward Kmett 2013-2014, (c) Paul Wilson 2012 -- License : BSD3 -- Maintainer: Edward Kmett -- Stability : experimental -- Portability: non-portable -- -- This module also exports orphan @'Ixed' 'Value'@ and -- @'Plated' 'Value'@ instances. -------------------------------------------------------------------- module Data.Aeson.Lens ( -- * Numbers AsNumber(..) , _Integral , nonNull -- * Primitive , Primitive(..) , AsPrimitive(..) -- * Objects and Arrays , AsValue(..) , key, members , nth, values -- * Decoding , AsJSON(..) ) where import Control.Applicative import Control.Lens import Data.Aeson import Data.Aeson.Parser (value) import Data.Attoparsec.ByteString.Lazy (maybeResult, parse) import Data.Scientific (Scientific) import qualified Data.Scientific as Scientific import qualified Data.ByteString as Strict import Data.ByteString.Lazy.Char8 as Lazy hiding (putStrLn) import Data.Data import Data.HashMap.Strict (HashMap) import Data.Text as Text import qualified Data.Text.Lazy as LazyText import Data.Text.Lens (packed) import qualified Data.Text.Encoding as StrictText import qualified Data.Text.Lazy.Encoding as LazyText import Data.Vector (Vector) import Prelude hiding (null) -- $setup -- >>> import Data.ByteString.Char8 as Strict.Char8 -- >>> import qualified Data.Vector as Vector -- >>> :set -XOverloadedStrings ------------------------------------------------------------------------------ -- Scientific prisms ------------------------------------------------------------------------------ class AsNumber t where -- | -- >>> "[1, \"x\"]" ^? nth 0 . _Number -- Just 1.0 -- -- >>> "[1, \"x\"]" ^? nth 1 . _Number -- Nothing _Number :: Prism' t Scientific #ifndef HLINT default _Number :: AsPrimitive t => Prism' t Scientific _Number = _Primitive._Number {-# INLINE _Number #-} #endif -- | -- Prism into an 'Double' over a 'Value', 'Primitive' or 'Scientific' -- -- >>> "[10.2]" ^? nth 0 . _Double -- Just 10.2 _Double :: Prism' t Double _Double = _Number.iso Scientific.toRealFloat realToFrac {-# INLINE _Double #-} -- | -- 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 _Integer :: Prism' t Integer _Integer = _Number.iso floor fromIntegral {-# INLINE _Integer #-} instance AsNumber Value where _Number = prism Number $ \v -> case v of Number n -> Right n; _ -> Left v {-# INLINE _Number #-} instance AsNumber Scientific where _Number = id {-# INLINE _Number #-} instance AsNumber Strict.ByteString instance AsNumber Lazy.ByteString instance AsNumber Text instance AsNumber LazyText.Text instance AsNumber String ------------------------------------------------------------------------------ -- Conversion Prisms ------------------------------------------------------------------------------ -- | Access Integer 'Value's as Integrals. -- -- >>> "[10]" ^? nth 0 . _Integral -- Just 10 -- -- >>> "[10.5]" ^? nth 0 . _Integral -- Just 10 _Integral :: (AsNumber t, Integral a) => Prism' t a _Integral = _Number . iso floor fromIntegral {-# INLINE _Integral #-} ------------------------------------------------------------------------------ -- Null values and primitives ------------------------------------------------------------------------------ -- | Primitives of 'Value' data Primitive = StringPrim !Text | NumberPrim !Scientific | BoolPrim !Bool | NullPrim deriving (Eq,Ord,Show,Data,Typeable) instance AsNumber Primitive where _Number = prism NumberPrim $ \v -> case v of NumberPrim s -> Right s; _ -> Left v {-# INLINE _Number #-} class AsNumber t => AsPrimitive t where -- | -- >>> "[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) _Primitive :: Prism' t Primitive #ifndef HLINT default _Primitive :: AsValue t => Prism' t Primitive _Primitive = _Value._Primitive {-# INLINE _Primitive #-} #endif -- | -- >>> "{\"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\"}" _String :: Prism' t Text _String = _Primitive.prism StringPrim (\v -> case v of StringPrim s -> Right s; _ -> Left v) {-# INLINE _String #-} -- | -- >>> "{\"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" _Bool :: Prism' t Bool _Bool = _Primitive.prism BoolPrim (\v -> case v of BoolPrim b -> Right b; _ -> Left v) {-# INLINE _Bool #-} -- | -- >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "b" . _Null -- Just () -- -- >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "a" . _Null -- Nothing -- -- >>> _Null # () :: String -- "null" _Null :: Prism' t () _Null = _Primitive.prism (const NullPrim) (\v -> case v of NullPrim -> Right (); _ -> Left v) {-# INLINE _Null #-} instance AsPrimitive Value where _Primitive = prism fromPrim toPrim where toPrim (String s) = Right $ StringPrim s toPrim (Number n) = Right $ NumberPrim n toPrim (Bool b) = Right $ BoolPrim b toPrim Null = Right NullPrim toPrim v = Left v {-# INLINE toPrim #-} fromPrim (StringPrim s) = String s fromPrim (NumberPrim n) = Number n fromPrim (BoolPrim b) = Bool b fromPrim NullPrim = Null {-# INLINE fromPrim #-} {-# INLINE _Primitive #-} _String = prism String $ \v -> case v of String s -> Right s; _ -> Left v {-# INLINE _String #-} _Bool = prism Bool (\v -> case v of Bool b -> Right b; _ -> Left v) {-# INLINE _Bool #-} _Null = prism (const Null) (\v -> case v of Null -> Right (); _ -> Left v) {-# INLINE _Null #-} instance AsPrimitive Strict.ByteString instance AsPrimitive Lazy.ByteString instance AsPrimitive Text.Text instance AsPrimitive LazyText.Text instance AsPrimitive String instance AsPrimitive Primitive where _Primitive = id {-# INLINE _Primitive #-} -- | 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 nonNull :: Prism' Value Value nonNull = prism id (\v -> if isn't _Null v then Right v else Left v) {-# INLINE nonNull #-} ------------------------------------------------------------------------------ -- Non-primitive traversals ------------------------------------------------------------------------------ class AsPrimitive t => AsValue t where -- | -- >>> preview _Value "[1,2,3]" == Just (Array (Vector.fromList [Number 1.0,Number 2.0,Number 3.0])) -- True _Value :: Prism' t Value -- | -- >>> "{\"a\": {}, \"b\": null}" ^? key "a" . _Object -- Just (fromList []) -- -- >>> "{\"a\": {}, \"b\": null}" ^? key "b" . _Object -- Nothing -- -- >>> _Object._Wrapped # [("key" :: Text, _String # "value")] :: String -- "{\"key\":\"value\"}" _Object :: Prism' t (HashMap Text Value) _Object = _Value.prism Object (\v -> case v of Object o -> Right o; _ -> Left v) {-# INLINE _Object #-} -- | -- >>> preview _Array "[1,2,3]" == Just (Vector.fromList [Number 1.0,Number 2.0,Number 3.0]) -- True _Array :: Prism' t (Vector Value) _Array = _Value.prism Array (\v -> case v of Array a -> Right a; _ -> Left v) {-# INLINE _Array #-} instance AsValue Value where _Value = id {-# INLINE _Value #-} instance AsValue Strict.ByteString where _Value = _JSON {-# INLINE _Value #-} instance AsValue Lazy.ByteString where _Value = _JSON {-# INLINE _Value #-} instance AsValue String where _Value = strictUtf8._JSON {-# INLINE _Value #-} instance AsValue Text where _Value = strictTextUtf8._JSON {-# INLINE _Value #-} instance AsValue LazyText.Text where _Value = lazyTextUtf8._JSON {-# INLINE _Value #-} -- | -- 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 key :: AsValue t => Text -> Traversal' t Value key i = _Object . ix i {-# INLINE key #-} -- | 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}" members :: AsValue t => IndexedTraversal' Text t Value members = _Object . itraversed {-# INLINE members #-} -- | 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]" nth :: AsValue t => Int -> Traversal' t Value nth i = _Array . ix i {-# INLINE nth #-} -- | 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]" values :: AsValue t => IndexedTraversal' Int t Value values = _Array . traversed {-# INLINE values #-} strictUtf8 :: Iso' String Strict.ByteString strictUtf8 = packed . strictTextUtf8 strictTextUtf8 :: Iso' Text.Text Strict.ByteString strictTextUtf8 = iso StrictText.encodeUtf8 StrictText.decodeUtf8 lazyTextUtf8 :: Iso' LazyText.Text Lazy.ByteString lazyTextUtf8 = iso LazyText.encodeUtf8 LazyText.decodeUtf8 class AsJSON t where -- | '_JSON' is a 'Prism' from something containing JSON to something encoded in that structure _JSON :: (FromJSON a, ToJSON a) => Prism' t a instance AsJSON Strict.ByteString where _JSON = lazy._JSON {-# INLINE _JSON #-} instance AsJSON Lazy.ByteString where _JSON = prism' encode decodeValue where decodeValue :: (FromJSON a) => Lazy.ByteString -> Maybe a decodeValue s = maybeResult (parse value s) >>= \x -> case fromJSON x of Success v -> Just v _ -> Nothing {-# INLINE _JSON #-} instance AsJSON String where _JSON = strictUtf8._JSON {-# INLINE _JSON #-} instance AsJSON Text where _JSON = strictTextUtf8._JSON {-# INLINE _JSON #-} instance AsJSON LazyText.Text where _JSON = lazyTextUtf8._JSON {-# INLINE _JSON #-} instance AsJSON Value where _JSON = prism toJSON $ \x -> case fromJSON x of Success y -> Right y; _ -> Left x {-# INLINE _JSON #-} ------------------------------------------------------------------------------ -- Some additional tests for prismhood; see https://github.com/ekmett/lens/issues/439. ------------------------------------------------------------------------------ -- $LazyByteStringTests -- >>> "42" ^? (_JSON :: Prism' Lazy.ByteString Value) -- Just (Number 42.0) -- -- >>> preview (_Integer :: Prism' Lazy.ByteString Integer) "42" -- Just 42 -- -- >>> Lazy.unpack (review (_Integer :: Prism' Lazy.ByteString Integer) 42) -- "42" -- $StrictByteStringTests -- >>> "42" ^? (_JSON :: Prism' Strict.ByteString Value) -- Just (Number 42.0) -- -- >>> preview (_Integer :: Prism' Strict.ByteString Integer) "42" -- Just 42 -- -- >>> Strict.Char8.unpack (review (_Integer :: Prism' Strict.ByteString Integer) 42) -- "42" -- $StringTests -- >>> "42" ^? (_JSON :: Prism' String Value) -- Just (Number 42.0) -- -- >>> preview (_Integer :: Prism' String Integer) "42" -- Just 42 -- -- >>> review (_Integer :: Prism' String Integer) 42 -- "42" ------------------------------------------------------------------------------ -- Orphan instances for lens library interop ------------------------------------------------------------------------------ type instance Index Value = Text type instance IxValue Value = Value instance Ixed Value where ix i f (Object o) = Object <$> ix i f o ix _ _ v = pure v {-# INLINE ix #-} instance Plated Value where plate f (Object o) = Object <$> traverse f o plate f (Array a) = Array <$> traverse f a plate _ xs = pure xs {-# INLINE plate #-} lens-aeson-1.0.2/tests/0000755000000000000000000000000013136745314013115 5ustar0000000000000000lens-aeson-1.0.2/tests/doctests.hs0000644000000000000000000000147213136745314015305 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Main (doctests) -- Copyright : (C) 2012-14 Edward Kmett -- License : BSD-style (see the file LICENSE) -- Maintainer : Edward Kmett -- Stability : provisional -- Portability : portable -- -- This module provides doctests for a project based on the actual versions -- of the packages it was built with. It requires a corresponding Setup.lhs -- to be added to the project ----------------------------------------------------------------------------- module Main where import Build_doctests (flags, pkgs, module_sources) import Data.Foldable (traverse_) import Test.DocTest main :: IO () main = do traverse_ putStrLn args doctest args where args = flags ++ pkgs ++ module_sources lens-aeson-1.0.2/travis/0000755000000000000000000000000013136745314013263 5ustar0000000000000000lens-aeson-1.0.2/travis/config0000644000000000000000000000120613136745314014452 0ustar0000000000000000-- This provides a custom ~/.cabal/config file for use when hackage is down that should work on unix -- -- This is particularly useful for travis-ci to get it to stop complaining -- about a broken build when everything is still correct on our end. -- -- This uses Luite Stegeman's mirror of hackage provided by his 'hdiff' site instead -- -- To enable this, uncomment the before_script in .travis.yml remote-repo: hdiff.luite.com:http://hdiff.luite.com/packages/archive remote-repo-cache: ~/.cabal/packages world-file: ~/.cabal/world build-summary: ~/.cabal/logs/build.log remote-build-reporting: anonymous install-dirs user install-dirs global lens-aeson-1.0.2/travis/cabal-apt-install0000755000000000000000000000127213136745314016503 0ustar0000000000000000#! /bin/bash set -eu APT="sudo apt-get -q -y" CABAL_INSTALL_DEPS="cabal install --only-dependencies --force-reinstall" $APT update $APT install dctrl-tools # Find potential system packages to satisfy cabal dependencies deps() { local M='^\([^ ]\+\)-[0-9.]\+ (.*$' local G=' -o ( -FPackage -X libghc-\L\1\E-dev )' local E="$($CABAL_INSTALL_DEPS "$@" --dry-run -v 2> /dev/null \ | sed -ne "s/$M/$G/p" | sort -u)" grep-aptavail -n -sPackage \( -FNone -X None \) $E | sort -u } $APT install $(deps "$@") libghc-quickcheck2-dev # QuickCheck is special $CABAL_INSTALL_DEPS "$@" # Install the rest via Hackage if ! $APT install hlint ; then $APT install $(deps hlint) cabal install hlint fi