packages feed

miso-aeson (empty) → 0.1.0.0

raw patch · 4 files changed

+154/−0 lines, 4 filesdep +aesondep +basedep +containers

Dependencies added: aeson, base, containers, miso, scientific, unordered-containers, vector

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for miso-aeson++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (c) 2025, dmjio+++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 the copyright holder nor the names of its+      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.
+ miso-aeson.cabal view
@@ -0,0 +1,51 @@+cabal-version:      3.0+name:               miso-aeson+version:            0.1.0.0+synopsis:           JSON conversion library+description:        Convert between Miso and Aeson types+license:            BSD-3-Clause+license-file:       LICENSE+author:             dmjio+maintainer:         code@dmj.io+copyright:          haskell-miso @ 2026+category:           Web+build-type:         Simple+extra-doc-files:    CHANGELOG.md++flag interactive+  default:+    False+  manual:+    True++common warnings+  ghc-options:+    -funbox-strict-fields -ferror-spans -fspecialise-aggressively -Wall++common options+  if arch(wasm32)+    cpp-options:+      -DWASM+    if flag (interactive)+      cpp-options:+        -DINTERACTIVE++  if arch(javascript)+     ld-options:+       -sEXPORTED_RUNTIME_METHODS=HEAP8++source-repository head+   type: git+   location: https://github.com/haskell-miso/miso-aeson.git++library+  import:+    options, warnings+  exposed-modules:+    Miso.Aeson+  build-depends:+    base < 5, containers < 0.9, miso >= 1.9 && < 1.10, aeson < 2.3, unordered-containers < 0.3, vector < 0.14, scientific < 0.4+  hs-source-dirs:+    src+  default-language:+    Haskell2010
+ src/Miso/Aeson.hs view
@@ -0,0 +1,69 @@+-----------------------------------------------------------------------------+{-# LANGUAGE CPP               #-}+{-# LANGUAGE LambdaCase        #-}+{-# LANGUAGE OverloadedStrings #-}+-----------------------------------------------------------------------------+module Miso.Aeson (aesonToJSON, jsonToAeson, MisoAeson(MisoAeson)) where+-----------------------------------------------------------------------------+import           Miso.String (toMisoString, fromMisoString, ms)+import qualified Miso.JSON as JSON+-----------------------------------------------------------------------------+import qualified Data.Aeson as Aeson+import qualified Data.Aeson.Types as Aeson+import qualified Data.Vector as V+import           Data.Scientific+import qualified Data.Aeson.KeyMap as KM+import           Data.Aeson.Key (toText, fromText)+import qualified Data.Map.Strict as M+import           Data.Bifunctor (first)+-----------------------------------------------------------------------------+newtype MisoAeson a = MisoAeson { unMisoAeson :: a }+-----------------------------------------------------------------------------+instance Aeson.ToJSON a => JSON.ToJSON (MisoAeson a) where+    toJSON = aesonToJSON . Aeson.toJSON . unMisoAeson+-----------------------------------------------------------------------------            +instance Aeson.FromJSON a => JSON.FromJSON (MisoAeson a) where+    parseJSON = fmap MisoAeson+            . JSON.Parser+            . first ms+            . flip Aeson.parseEither ()+            . const+            . Aeson.parseJSON+            . jsonToAeson+-----------------------------------------------------------------------------+aesonToJSON :: Aeson.Value -> JSON.Value+aesonToJSON = \case+  Aeson.Null ->+    JSON.Null+  Aeson.Bool b ->+    JSON.Bool b+  Aeson.Number n ->+    JSON.Number (toRealFloat n)+  Aeson.String n ->+    JSON.String (toMisoString n)+  Aeson.Array arr ->+    JSON.Array [ aesonToJSON v | v <- V.toList arr ]+  Aeson.Object o ->+    JSON.Object $+      M.fromList [ (toMisoString (toText k), aesonToJSON v)+                 | (k,v) <- KM.toList o+                 ]+-----------------------------------------------------------------------------+jsonToAeson :: JSON.Value -> Aeson.Value+jsonToAeson = \case+  JSON.Null ->+    Aeson.Null+  JSON.Bool b ->+    Aeson.Bool b+  JSON.Number n ->+    Aeson.Number (fromFloatDigits n)+  JSON.String n ->+    Aeson.String (fromMisoString n)+  JSON.Array arr ->+    Aeson.Array $ V.fromList (jsonToAeson <$> arr)+  JSON.Object o ->+    Aeson.Object $+      KM.fromList [ (fromText (fromMisoString k), jsonToAeson v)+                  | (k,v) <- M.toList o+                  ]+-----------------------------------------------------------------------------