packages feed

rapidhash-aeson (empty) → 0.1.0.0

raw patch · 7 files changed

+251/−0 lines, 7 filesdep +aesondep +basedep +containers

Dependencies added: aeson, base, containers, hedgehog, rapidhash, rapidhash-aeson, tasty, tasty-hedgehog, tasty-hunit

Files

+ CHANGELOG.md view
@@ -0,0 +1,10 @@+# Changelog — rapidhash-aeson++## Unreleased++### 0.1.0.0 - 2026-11-22++Initial release.++- `ToJSON`/`FromJSON` and `ToJSONKey`/`FromJSONKey` instances for `RapidHash`+  and `RapidHashMicro`, moved here from `rapidhash`'s former `aeson` flag.
+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (c) 2026 Jeremy Nuttall++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 Jeremy Nuttall 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+HOLDER 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.
+ README.md view
@@ -0,0 +1,12 @@+# rapidhash-aeson++`ToJSON`/`FromJSON` and `ToJSONKey`/`FromJSONKey` instances for `RapidHash`+and `RapidHashMicro` from [rapidhash](https://hackage.haskell.org/package/rapidhash).+Digests encode as JSON strings in rapidhash's tagged hex form (`rhv3:` or+`rhmv3:` followed by 16 hex digits), and map keys use the same form as values.++The module exports nothing; import it for its instances:++```haskell+import Data.Hash.RapidHash.Aeson ()+```
+ rapidhash-aeson.cabal view
@@ -0,0 +1,81 @@+cabal-version: 2.2++-- This file has been generated from package.yaml by hpack version 0.39.6.+--+-- see: https://github.com/sol/hpack++name:           rapidhash-aeson+version:        0.1.0.0+synopsis:       aeson instances for rapidhash digests+description:    `ToJSON`/`FromJSON` and `ToJSONKey`/`FromJSONKey` instances for rapidhash+                digests, following rapidhash's tagged textual form.+category:       Data+homepage:       https://github.com/jtnuttall/lithon#readme+bug-reports:    https://github.com/jtnuttall/lithon/issues+author:         Jeremy Nuttall+maintainer:     jeremy@jeremy-nuttall.com+copyright:      (c) 2026 Jeremy Nuttall+license:        BSD-3-Clause+license-file:   LICENSE+build-type:     Simple+tested-with:+    GHC == 9.8.4+  , GHC == 9.10.3+  , GHC == 9.12.4+extra-doc-files:+    README.md+    CHANGELOG.md++source-repository head+  type: git+  location: https://github.com/jtnuttall/lithon+  subdir: rapidhash-aeson++library+  exposed-modules:+      Data.Hash.RapidHash.Aeson+  other-modules:+      Paths_rapidhash_aeson+  autogen-modules:+      Paths_rapidhash_aeson+  hs-source-dirs:+      src+  default-extensions:+      BlockArguments+      DerivingStrategies+      LambdaCase+  ghc-options: -Wmissing-deriving-strategies -Wmissing-export-lists -Wredundant-constraints -Wall+  build-depends:+      aeson >=2.2 && <2.4+    , base >=4.19 && <4.22+    , rapidhash ==1.0.*+  default-language: GHC2021++test-suite rapidhash-aeson-test+  type: exitcode-stdio-1.0+  main-is: Driver.hs+  other-modules:+      RapidHashAesonTest+      Paths_rapidhash_aeson+  autogen-modules:+      Paths_rapidhash_aeson+  hs-source-dirs:+      test+  default-extensions:+      BlockArguments+      DerivingStrategies+      LambdaCase+  ghc-options: -Wmissing-deriving-strategies -Wmissing-export-lists -Wredundant-constraints -Wall -Wno-missing-export-lists -threaded -rtsopts -with-rtsopts=-N+  build-tool-depends:+      tasty-discover:tasty-discover ==5.2.*+  build-depends:+      aeson >=2.2 && <2.4+    , base >=4.19 && <4.22+    , containers+    , hedgehog ==1.7.*+    , rapidhash ==1.0.*+    , rapidhash-aeson+    , tasty ==1.5.*+    , tasty-hedgehog ==1.4.*+    , tasty-hunit ==0.10.*+  default-language: GHC2021
+ src/Data/Hash/RapidHash/Aeson.hs view
@@ -0,0 +1,54 @@+{-# OPTIONS_GHC -Wno-orphans #-}++-- | Orphan aeson instances for 'RapidHash' and 'RapidHashMicro', encoding to+-- and from the tagged hex form the rest of rapidhash uses+-- (e.g. @"rhv3:0123456789abcdef"@, @"rhmv3:0123456789abcdef"@).+--+-- The module exports nothing; import it for its instances:+--+-- > import Data.Hash.RapidHash.Aeson ()+module Data.Hash.RapidHash.Aeson () where++import Data.Aeson qualified as A+import Data.Aeson.Types qualified as A+import Data.Hash.RapidHash.Types++instance A.ToJSON RapidHash where+  toJSON = A.String . showRapidHashText+  {-# INLINEABLE toJSON #-}++instance A.FromJSON RapidHash where+  parseJSON = A.withText "rapidhashv3" \t -> case parseRapidHashText t of+    Right h -> pure h+    Left err -> fail err+  {-# INLINEABLE parseJSON #-}++-- | Keys use the same tagged hex form as the value instances, so a map+-- keyed by @RapidHash@ serializes to a JSON object with @rhv3:@-prefixed keys.+instance A.ToJSONKey RapidHash where+  toJSONKey = A.toJSONKeyText showRapidHashText+  {-# INLINEABLE toJSONKey #-}++instance A.FromJSONKey RapidHash where+  fromJSONKey = A.FromJSONKeyTextParser \t -> either fail pure (parseRapidHashText t)+  {-# INLINEABLE fromJSONKey #-}++instance A.ToJSON RapidHashMicro where+  toJSON = A.String . showRapidHashMicroText+  {-# INLINEABLE toJSON #-}++instance A.FromJSON RapidHashMicro where+  parseJSON = A.withText "rapidhashMicrov3" \t -> case parseRapidHashMicroText t of+    Right h -> pure h+    Left err -> fail err+  {-# INLINEABLE parseJSON #-}++-- | Keys use the same tagged hex form as the value instances, so a map+-- keyed by @RapidHashMicro@ serializes to a JSON object with @rhmv3:@-prefixed keys.+instance A.ToJSONKey RapidHashMicro where+  toJSONKey = A.toJSONKeyText showRapidHashMicroText+  {-# INLINEABLE toJSONKey #-}++instance A.FromJSONKey RapidHashMicro where+  fromJSONKey = A.FromJSONKeyTextParser \t -> either fail pure (parseRapidHashMicroText t)+  {-# INLINEABLE fromJSONKey #-}
+ test/Driver.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF tasty-discover #-}
+ test/RapidHashAesonTest.hs view
@@ -0,0 +1,64 @@+{-# LANGUAGE OverloadedStrings #-}++module RapidHashAesonTest where++import Data.Aeson qualified as A+import Data.Aeson.Key qualified as Key+import Data.Hash.RapidHash (RapidHash (RapidHash), RapidHashMicro (RapidHashMicro))+import Data.Map.Strict qualified as Map+import Hedgehog (Property, forAll, property, (===))+import Hedgehog.Gen qualified as Gen+import Hedgehog.Range qualified as Range+import Test.Tasty.HUnit (Assertion, (@?=))++import Data.Hash.RapidHash.Aeson ()++unit_encodesAsTaggedPaddedHexString :: Assertion+unit_encodesAsTaggedPaddedHexString = do+  A.encode (RapidHash 0xabc) @?= "\"rhv3:0000000000000abc\""+  A.encode (RapidHashMicro 0xabc) @?= "\"rhmv3:0000000000000abc\""++unit_rejectsForeignAndMalformedTags :: Assertion+unit_rejectsForeignAndMalformedTags = do+  A.decode @RapidHash "\"rhv4:0000000000000abc\"" @?= Nothing -- future+  A.decode @RapidHash "\"fnv1a64:0000000000000abc\"" @?= Nothing -- other hash+  A.decode @RapidHash "\"rhv3:abc\"" @?= Nothing -- invalid hash length+  A.decode @RapidHash "123" @?= Nothing -- not a string at all+  A.decode @RapidHashMicro "\"rhmv4:0000000000000abc\"" @?= Nothing+  A.decode @RapidHashMicro "\"fnv1a64:0000000000000abc\"" @?= Nothing+  A.decode @RapidHashMicro "\"rhmv3:abc\"" @?= Nothing++-- | The two variants disagree above 80 bytes, so a digest of one must never+-- decode as the other — the distinct tags exist for exactly this.+unit_variantTagsDoNotCrossDecode :: Assertion+unit_variantTagsDoNotCrossDecode = do+  A.decode @RapidHash (A.encode (RapidHashMicro 0xabc)) @?= Nothing+  A.decode @RapidHashMicro (A.encode (RapidHash 0xabc)) @?= Nothing++-- | Keys render through 'Data.Aeson.Types.toJSONKeyText', so a keyed map is a+-- JSON object (not an array of pairs) and its keys carry the same tag as values.+unit_keysAreObjectKeysInTaggedForm :: Assertion+unit_keysAreObjectKeysInTaggedForm = do+  A.toJSON (Map.singleton (RapidHash 0xabc) (1 :: Int))+    @?= A.object [(Key.fromString "rhv3:0000000000000abc", A.Number 1)]+  A.toJSON (Map.singleton (RapidHashMicro 0xabc) (1 :: Int))+    @?= A.object [(Key.fromString "rhmv3:0000000000000abc", A.Number 1)]++hprop_valueRoundtrip :: Property+hprop_valueRoundtrip = property do+  w <- forAll (Gen.word64 Range.linearBounded)+  A.decode (A.encode (RapidHash w)) === Just (RapidHash w)+  A.decode (A.encode (RapidHashMicro w)) === Just (RapidHashMicro w)++hprop_keyRoundtrip :: Property+hprop_keyRoundtrip = property do+  kvs <-+    forAll+      ( Gen.list+          (Range.linear 0 32)+          ((,) <$> Gen.word64 Range.linearBounded <*> Gen.int Range.linearBounded)+      )+  let m = Map.fromList [(RapidHash k, v) | (k, v) <- kvs]+      mm = Map.fromList [(RapidHashMicro k, v) | (k, v) <- kvs]+  A.decode (A.encode m) === Just m+  A.decode (A.encode mm) === Just mm