yaml 0.8.4.1 → 0.8.5
raw patch · 4 files changed
+89/−1 lines, 4 filesdep ~aesonnew-component:exe:yaml2json
Dependency ranges changed: aeson
Files
- exe/yaml2json.hs +28/−0
- test/json.yaml +8/−0
- test/main.hs +31/−0
- yaml.cabal +22/−1
+ exe/yaml2json.hs view
@@ -0,0 +1,28 @@+{-# LANGUAGE ScopedTypeVariables #-}++import Data.Yaml (decodeFileEither, decodeEither')+import System.Environment (getArgs)+import System.Exit+import Data.Aeson (encode, Value)+import Prelude hiding (putStr, getContents)+import Data.ByteString.Lazy (putStr)+import Data.ByteString (getContents)++helpMessage :: IO ()+helpMessage = putStrLn "yaml2json FILE\n use - as FILE to indicate stdin" >> exitFailure++showJSON ejson =+ case ejson of+ Left err -> print err >> exitFailure+ Right (res :: Value) -> putStr (encode res) >> exitSuccess++main :: IO ()+main = do+ args <- getArgs+ case args of+ [] -> helpMessage+ (f:a:_) -> helpMessage+ -- strict getContents will read in all of stdin at once+ ("-":[]) -> getContents >>= showJSON . decodeEither'+ (f:[]) -> decodeFileEither f >>= showJSON+
+ test/json.yaml view
@@ -0,0 +1,8 @@+string: str+number: 2+anArray:+ - a+ - b+hash:+ key1: value1+ key2: value2
test/main.hs view
@@ -2,6 +2,7 @@ {-# LANGUAGE PackageImports #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TemplateHaskell #-} import qualified Text.Libyaml as Y import qualified Data.ByteString.Char8 as B8@@ -21,7 +22,22 @@ import Data.Maybe import qualified Data.HashMap.Strict as M import qualified Data.Text as T+import Data.Aeson.TH+import Data.Text (Text)+import Data.Vector (Vector)+import qualified Data.Vector as V+import Data.HashMap.Strict (HashMap)+import qualified Data.HashMap.Strict as HM +data TestJSON = TestJSON+ { string :: Text+ , number :: Int+ , anArray :: Vector Text+ , hash :: HashMap Text Text+ } deriving (Show, Eq)++deriveJSON defaultOptions ''TestJSON+ main :: IO () main = hspec $ do describe "streaming" $ do@@ -70,22 +86,37 @@ checkNull "NULL" checkNull "~" checkNull ""+ describe "pretty output" $ do it "simple nulls" $ D.encode (object ["foo" .= D.Null]) `shouldBe` "foo: null\n" it "simple numbers" $ D.encode (object ["foo" .= (4 :: Int)]) `shouldBe` "foo: 4\n" it "True" $ D.encode (object ["foo" .= True]) `shouldBe` "foo: true\n" it "False" $ D.encode (object ["foo" .= False]) `shouldBe` "foo: false\n" it "simple string" $ D.encode (object ["foo" .= ("bar" :: T.Text)]) `shouldBe` "foo: bar\n"+ describe "special keys" $ do let tester key = it (T.unpack key) $ let value = object [key .= True] in D.decode (D.encode value) `shouldBe` Just value mapM_ tester specialStrings+ describe "special values" $ do let tester value = it (T.unpack value) $ let value' = object ["foo" .= value] in D.decode (D.encode value') `shouldBe` Just value' mapM_ tester specialStrings++ describe "yamlFileJSONparse" $+ it "loads YAML through JSON into Haskell data" $ do+ tj <- either (error . show) id `fmap` D.decodeFileEither "test/json.yaml"+ tj `shouldBe` TestJSON+ { string = "str"+ , number = 2+ , anArray = V.fromList ["a", "b"]+ , hash = HM.fromList [("key1", "value1"), ("key2", "value2")]+ }++ specialStrings :: [T.Text] specialStrings =
yaml.cabal view
@@ -1,5 +1,5 @@ name: yaml-version: 0.8.4.1+version: 0.8.5 license: BSD3 license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov @@ -27,7 +27,12 @@ libyaml/LICENSE test/main.hs test/largest-string.yaml+ test/json.yaml +flag no-exe+ description: don't install the yaml2json executable+ default: False+ flag system-libyaml description: Use the system-wide libyaml instead of the bundled copy default: False@@ -62,6 +67,20 @@ libyaml/writer.c include-dirs: libyaml +executable yaml2json+ if flag(no-exe)+ Buildable: False+ else+ Buildable: True++ hs-source-dirs: exe+ main-is: yaml2json.hs+ build-depends: base >= 4 && < 5+ , yaml+ , bytestring >= 0.9.1.4+ , aeson >= 0.5++ test-suite test type: exitcode-stdio-1.0 hs-source-dirs: test@@ -76,7 +95,9 @@ , conduit , yaml , text+ , aeson , unordered-containers+ , vector ghc-options: -Wall source-repository head