Browse Source

Added json2bencode script

Getty Ritter 8 years ago
parent
commit
98537c517b
5 changed files with 139 additions and 0 deletions
  1. 30 0
      json2bencode/LICENSE
  2. 2 0
      json2bencode/Setup.hs
  3. 27 0
      json2bencode/json2bencode.cabal
  4. 48 0
      json2bencode/src/Main.hs
  5. 32 0
      json2bencode/stack.yaml

+ 30 - 0
json2bencode/LICENSE

@@ -0,0 +1,30 @@
+Copyright (c) 2016, Getty Ritter
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of Getty Ritter nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 2 - 0
json2bencode/Setup.hs

@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain

+ 27 - 0
json2bencode/json2bencode.cabal

@@ -0,0 +1,27 @@
+name:                json2bencode
+version:             0.1.0.0
+synopsis:            A utility for converting JSON to Bencode
+license:             BSD3
+license-file:        LICENSE
+author:              Getty Ritter
+maintainer:          gettyritter@gmail.com
+copyright:           2016
+category:            Data
+build-type:          Simple
+cabal-version:       >=1.10
+
+executable json2bencode
+  main-is:             Main.hs
+  -- other-modules:       
+  -- other-extensions:    
+  build-depends:       base >=4.8 && <4.9,
+                       aeson,
+                       bencode,
+                       bytestring,
+                       containers,
+                       scientific,
+                       text,
+                       unordered-containers,
+                       vector
+  hs-source-dirs:      src
+  default-language:    Haskell2010

+ 48 - 0
json2bencode/src/Main.hs

@@ -0,0 +1,48 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Main where
+
+import           Data.Aeson
+import           Data.BEncode
+import           Data.ByteString.Lazy (ByteString, fromStrict)
+import qualified Data.ByteString.Lazy.Char8 as BS
+import qualified Data.HashMap.Strict as HM
+import qualified Data.Map.Lazy as M
+import           Data.Scientific (isInteger)
+import           Data.Text (Text, unpack)
+import           Data.Text.Encoding (encodeUtf8)
+import qualified Data.Vector as V
+import           System.Environment (getArgs)
+import           System.Exit (die)
+
+byteify :: Text -> ByteString
+byteify = fromStrict . encodeUtf8
+
+convert :: Value -> Either String BEncode
+convert (Object os) =
+  (BDict . M.fromList) `fmap` mapM go (HM.toList os)
+  where go (k, v) = (,) (unpack k) `fmap` convert v
+convert (Array as) =
+  BList `fmap` mapM convert (V.toList as)
+convert (Number n)
+  | isInteger n = return $ BInt (floor n)
+  | otherwise   = Left ("Input contains a non-integer number: " ++ show n)
+convert (String ts) =
+  return $ BString (byteify ts)
+convert (Bool b) = Left ("Input contains a boolean: " ++ show b)
+convert (Null) = return $ BString ""
+
+main :: IO ()
+main = do
+  content <- do
+    args <- getArgs
+    case args of
+      []     -> BS.getContents
+      ["-"]  -> BS.getContents
+      [file] -> BS.readFile file
+      _      -> die "Usage: json2bencode [file]"
+  case decode content of
+    Just val -> case convert val of
+      Right bval -> BS.putStrLn (bPack bval)
+      Left err   -> putStrLn err
+    Nothing -> putStrLn "Unable to parse JSON"

+ 32 - 0
json2bencode/stack.yaml

@@ -0,0 +1,32 @@
+# For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md
+
+# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
+resolver: lts-3.19
+
+# Local packages, usually specified by relative directory name
+packages:
+- '.'
+
+# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
+extra-deps: []
+
+# Override default flag values for local packages and extra-deps
+flags: {}
+
+# Extra package databases containing global packages
+extra-package-dbs: []
+
+# Control whether we use the GHC we find on the path
+# system-ghc: true
+
+# Require a specific version of stack, using version ranges
+# require-stack-version: -any # Default
+# require-stack-version: >= 0.1.4.0
+
+# Override the architecture used by stack, especially useful on Windows
+# arch: i386
+# arch: x86_64
+
+# Extra directories used by stack for building
+# extra-include-dirs: [/path/to/dir]
+# extra-lib-dirs: [/path/to/dir]