Added json2bencode script
Getty Ritter
8 years ago
1 | Copyright (c) 2016, Getty Ritter | |
2 | ||
3 | All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are met: | |
7 | ||
8 | * Redistributions of source code must retain the above copyright | |
9 | notice, this list of conditions and the following disclaimer. | |
10 | ||
11 | * Redistributions in binary form must reproduce the above | |
12 | copyright notice, this list of conditions and the following | |
13 | disclaimer in the documentation and/or other materials provided | |
14 | with the distribution. | |
15 | ||
16 | * Neither the name of Getty Ritter nor the names of other | |
17 | contributors may be used to endorse or promote products derived | |
18 | from this software without specific prior written permission. | |
19 | ||
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
1 | name: json2bencode | |
2 | version: 0.1.0.0 | |
3 | synopsis: A utility for converting JSON to Bencode | |
4 | license: BSD3 | |
5 | license-file: LICENSE | |
6 | author: Getty Ritter | |
7 | maintainer: gettyritter@gmail.com | |
8 | copyright: 2016 | |
9 | category: Data | |
10 | build-type: Simple | |
11 | cabal-version: >=1.10 | |
12 | ||
13 | executable json2bencode | |
14 | main-is: Main.hs | |
15 | -- other-modules: | |
16 | -- other-extensions: | |
17 | build-depends: base >=4.8 && <4.9, | |
18 | aeson, | |
19 | bencode, | |
20 | bytestring, | |
21 | containers, | |
22 | scientific, | |
23 | text, | |
24 | unordered-containers, | |
25 | vector | |
26 | hs-source-dirs: src | |
27 | default-language: Haskell2010⏎ |
1 | {-# LANGUAGE OverloadedStrings #-} | |
2 | ||
3 | module Main where | |
4 | ||
5 | import Data.Aeson | |
6 | import Data.BEncode | |
7 | import Data.ByteString.Lazy (ByteString, fromStrict) | |
8 | import qualified Data.ByteString.Lazy.Char8 as BS | |
9 | import qualified Data.HashMap.Strict as HM | |
10 | import qualified Data.Map.Lazy as M | |
11 | import Data.Scientific (isInteger) | |
12 | import Data.Text (Text, unpack) | |
13 | import Data.Text.Encoding (encodeUtf8) | |
14 | import qualified Data.Vector as V | |
15 | import System.Environment (getArgs) | |
16 | import System.Exit (die) | |
17 | ||
18 | byteify :: Text -> ByteString | |
19 | byteify = fromStrict . encodeUtf8 | |
20 | ||
21 | convert :: Value -> Either String BEncode | |
22 | convert (Object os) = | |
23 | (BDict . M.fromList) `fmap` mapM go (HM.toList os) | |
24 | where go (k, v) = (,) (unpack k) `fmap` convert v | |
25 | convert (Array as) = | |
26 | BList `fmap` mapM convert (V.toList as) | |
27 | convert (Number n) | |
28 | | isInteger n = return $ BInt (floor n) | |
29 | | otherwise = Left ("Input contains a non-integer number: " ++ show n) | |
30 | convert (String ts) = | |
31 | return $ BString (byteify ts) | |
32 | convert (Bool b) = Left ("Input contains a boolean: " ++ show b) | |
33 | convert (Null) = return $ BString "" | |
34 | ||
35 | main :: IO () | |
36 | main = do | |
37 | content <- do | |
38 | args <- getArgs | |
39 | case args of | |
40 | [] -> BS.getContents | |
41 | ["-"] -> BS.getContents | |
42 | [file] -> BS.readFile file | |
43 | _ -> die "Usage: json2bencode [file]" | |
44 | case decode content of | |
45 | Just val -> case convert val of | |
46 | Right bval -> BS.putStrLn (bPack bval) | |
47 | Left err -> putStrLn err | |
48 | Nothing -> putStrLn "Unable to parse JSON" |
1 | # For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md | |
2 | ||
3 | # Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2) | |
4 | resolver: lts-3.19 | |
5 | ||
6 | # Local packages, usually specified by relative directory name | |
7 | packages: | |
8 | - '.' | |
9 | ||
10 | # Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3) | |
11 | extra-deps: [] | |
12 | ||
13 | # Override default flag values for local packages and extra-deps | |
14 | flags: {} | |
15 | ||
16 | # Extra package databases containing global packages | |
17 | extra-package-dbs: [] | |
18 | ||
19 | # Control whether we use the GHC we find on the path | |
20 | # system-ghc: true | |
21 | ||
22 | # Require a specific version of stack, using version ranges | |
23 | # require-stack-version: -any # Default | |
24 | # require-stack-version: >= 0.1.4.0 | |
25 | ||
26 | # Override the architecture used by stack, especially useful on Windows | |
27 | # arch: i386 | |
28 | # arch: x86_64 | |
29 | ||
30 | # Extra directories used by stack for building | |
31 | # extra-include-dirs: [/path/to/dir] | |
32 | # extra-lib-dirs: [/path/to/dir] |