cborg-json-0.2.5.0/0000755000000000000000000000000007346545000012110 5ustar0000000000000000cborg-json-0.2.5.0/ChangeLog.md0000644000000000000000000000070207346545000014260 0ustar0000000000000000# Revision history for cborg-json ## 0.2.5.0 -- 2022-09-24 * Support GHC 9.4 * Drop GHC 8.0 and 8.2 support * Support aeson 2.1 ## 0.2.2.0 -- 2019-12-29 * Use `replicateM` to significantly speed up decoding. Now faster than Aeson! * Add support for decoding half-precision and single-precision floating-point numbers. * Version bumps and support for GHC 8.8 ## 0.1.0.0 -- 2017-08-01 * First version. Released on an unsuspecting world. cborg-json-0.2.5.0/LICENSE.txt0000644000000000000000000000276407346545000013744 0ustar0000000000000000Copyright (c) 2017, Duncan Coutts 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 Duncan Coutts 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. cborg-json-0.2.5.0/Setup.hs0000644000000000000000000000005607346545000013545 0ustar0000000000000000import Distribution.Simple main = defaultMain cborg-json-0.2.5.0/bench/0000755000000000000000000000000007346545000013167 5ustar0000000000000000cborg-json-0.2.5.0/bench/Main.hs0000644000000000000000000000434007346545000014410 0ustar0000000000000000module Main (main) where import qualified Data.ByteString.Lazy as LBS import qualified Codec.Compression.GZip as GZip import Control.Monad (unless) import System.Directory (doesFileExist) import System.Process (callProcess) import Codec.CBOR.JSON (encodeValue, decodeValue) import Codec.CBOR.Read (deserialiseFromBytes) import Codec.CBOR.Write (toLazyByteString, toStrictByteString) import Criterion.Main ( defaultMain, Benchmark, bgroup, bench, env, nf, whnf ) import qualified Data.Aeson as Aeson (Value, decode, encode) dataFile :: FilePath dataFile = "bench/data.json.gz" dataFileURL :: FilePath dataFileURL = "https://raw.githubusercontent.com/well-typed/cborg/master/cborg-json/bench/data.json.gz" fetchLargeJSON :: IO () fetchLargeJSON = do exists <- doesFileExist dataFile unless exists $ do putStrLn $ "Fetching bechmark data file from " ++ dataFileURL callProcess "curl" [dataFileURL, "--output", dataFile] loadLargeJSONBytes :: IO LBS.ByteString loadLargeJSONBytes = do fetchLargeJSON fmap GZip.decompress (LBS.readFile dataFile) loadLargeJSON :: IO Aeson.Value loadLargeJSON = do bytes <- loadLargeJSONBytes let Just json = Aeson.decode bytes return json loadLargeCBORBytes :: IO LBS.ByteString loadLargeCBORBytes = fmap (toLazyByteString . encodeValue) loadLargeJSON main :: IO () main = defaultMain benchmarks benchmarks :: [Benchmark] benchmarks = [ bgroup "Decode" [ env loadLargeJSONBytes $ \largeJSONBytes -> bench "JSON" (whnf (Aeson.decode :: LBS.ByteString -> Maybe Aeson.Value) largeJSONBytes) , env loadLargeCBORBytes $ \largeCBORBytes -> bench "CBOR" (whnf (deserialiseFromBytes (decodeValue False)) largeCBORBytes) , env loadLargeCBORBytes $ \largeCBORBytes -> bench "CBOR lenient" (whnf (deserialiseFromBytes (decodeValue True)) largeCBORBytes) ] , env loadLargeJSON $ \largeJSON -> bgroup "Encode" [ bench "JSON" (nf Aeson.encode largeJSON) , bench "CBOR" (nf (toLazyByteString . encodeValue) largeJSON) , bench "CBOR strict" (whnf (toStrictByteString . encodeValue) largeJSON) ] ] cborg-json-0.2.5.0/cborg-json.cabal0000644000000000000000000000365407346545000015147 0ustar0000000000000000name: cborg-json version: 0.2.5.0 synopsis: A library for encoding JSON as CBOR description: This package implements the bijection between JSON and CBOR defined in the CBOR specification, RFC 7049. homepage: https://github.com/well-typed/cborg license: BSD3 license-file: LICENSE.txt author: Duncan Coutts maintainer: ben@smart-cactus.org bug-reports: https://github.com/well-typed/cborg/issues copyright: 2015-2017 Duncan Coutts, 2015-2017 Well-Typed LLP, 2015 IRIS Connect Ltd category: Codec build-type: Simple extra-source-files: ChangeLog.md cabal-version: >=1.10 tested-with: GHC == 8.4.4, GHC == 8.6.5, GHC == 8.8.3, GHC == 8.10.7, GHC == 9.0.1, GHC == 9.2.2, GHC == 9.4.2 library exposed-modules: Codec.CBOR.JSON ghc-options: -Wall build-depends: base >=4.11 && < 4.18, aeson >=0.7 && <2.2, aeson-pretty >=0.8 && <0.9, unordered-containers >=0.2 && <0.3, scientific >=0.3 && <0.4, text >=1.1 && <2.1, vector >=0.10 && <0.13, cborg ==0.2.* hs-source-dirs: src default-language: Haskell2010 if impl(ghc >= 8.0) ghc-options: -Wcompat -Wnoncanonical-monad-instances benchmark bench type: exitcode-stdio-1.0 hs-source-dirs: bench main-is: Main.hs default-language: Haskell2010 ghc-options: -Wall -rtsopts -fno-cse -fno-ignore-asserts -fno-warn-orphans -O2 other-modules: build-depends: base >= 4.11 && < 4.18, bytestring >= 0.10.4 && < 0.12, criterion >= 1.0 && < 1.6, deepseq >= 1.0 && < 1.5, zlib >= 0.5 && < 0.7, directory, process, aeson, cborg, cborg-json cborg-json-0.2.5.0/src/Codec/CBOR/0000755000000000000000000000000007346545000014441 5ustar0000000000000000cborg-json-0.2.5.0/src/Codec/CBOR/JSON.hs0000644000000000000000000001057607346545000015557 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} module Codec.CBOR.JSON ( encodeValue , decodeValue ) where import Data.Monoid import Control.Applicative import Prelude hiding (decodeFloat) import Codec.CBOR.Encoding import Codec.CBOR.Decoding import Data.Aeson ( Value(..) ) import qualified Data.Aeson as Aeson import Data.Scientific as Scientific import qualified Data.Text as T import qualified Data.Vector as V #if MIN_VERSION_aeson(2,0,0) import qualified Data.Aeson.Key as K import qualified Data.Aeson.KeyMap as KM #else import qualified Data.HashMap.Lazy as HM #endif -- | Encode a JSON value into CBOR. encodeValue :: Value -> Encoding encodeValue (Object vs) = encodeObject vs encodeValue (Array vs) = encodeArray vs encodeValue (String s) = encodeString s encodeValue (Number n) = case Scientific.floatingOrInteger n of Left d -> encodeDouble d Right i -> encodeInteger i encodeValue (Bool b) = encodeBool b encodeValue Null = encodeNull encodeObject :: Aeson.Object -> Encoding encodeObject vs = encodeMapLen (fromIntegral size) <> foldrWithKey (\k v r -> encodeString' k <> encodeValue v <> r) mempty vs where #if MIN_VERSION_aeson(2,0,0) size = KM.size vs foldrWithKey = KM.foldrWithKey encodeString' = encodeString . K.toText #else size = HM.size vs foldrWithKey = HM.foldrWithKey encodeString' = encodeString #endif encodeArray :: Aeson.Array -> Encoding encodeArray vs = encodeListLen (fromIntegral (V.length vs)) <> V.foldr (\v r -> encodeValue v <> r) mempty vs -- | Decode an arbitrary CBOR value into JSON. decodeValue :: Bool -> Decoder s Value decodeValue lenient = do tkty <- peekTokenType case tkty of TypeUInt -> decodeNumberIntegral TypeUInt64 -> decodeNumberIntegral TypeNInt -> decodeNumberIntegral TypeNInt64 -> decodeNumberIntegral TypeInteger -> decodeNumberIntegral TypeFloat16 -> decodeNumberFloat16 TypeFloat32 -> decodeNumberFloating TypeFloat64 -> decodeNumberFloating TypeBool -> Bool <$> decodeBool TypeNull -> Null <$ decodeNull TypeString -> String <$> decodeString TypeListLen -> decodeListLen >>= decodeListN lenient TypeListLenIndef -> decodeListLenIndef >> decodeListIndef lenient [] TypeMapLen -> decodeMapLen >>= flip (decodeMapN lenient) mempty _ -> fail $ "unexpected CBOR token type for a JSON value: " ++ show tkty decodeNumberIntegral :: Decoder s Value decodeNumberIntegral = Number . fromInteger <$> decodeInteger decodeNumberFloating :: Decoder s Value decodeNumberFloating = Number . Scientific.fromFloatDigits <$> decodeDouble decodeNumberFloat16 :: Decoder s Value decodeNumberFloat16 = do f <- decodeFloat if isNaN f || isInfinite f then return Null else return $ Number (Scientific.fromFloatDigits f) decodeListN :: Bool -> Int -> Decoder s Value decodeListN !lenient !n = do vec <- V.replicateM n (decodeValue lenient) return $! Array vec decodeListIndef :: Bool -> [Value] -> Decoder s Value decodeListIndef !lenient acc = do stop <- decodeBreakOr if stop then return $! Array (V.fromList (reverse acc)) else do !tm <- decodeValue lenient decodeListIndef lenient (tm : acc) decodeMapN :: Bool -> Int -> Aeson.Object -> Decoder s Value decodeMapN !lenient !n acc = case n of 0 -> return $! Object acc _ -> do !tk <- decodeValue lenient >>= \v -> case v of String s -> return s -- These cases are only allowed when --lenient is passed, -- as printing them as strings may result in key collisions. Number d | lenient -> return $ T.pack (show d) Bool b | lenient -> return $ T.pack (show b) _ -> fail "Could not decode map key type" !tv <- decodeValue lenient decodeMapN lenient (n-1) (insert tk tv acc) where #if MIN_VERSION_aeson(2,0,0) insert k v m = KM.insert (K.fromText k) v m #else insert k v m = HM.insert k v m #endif