Skip to content

Recommended client side library broken? #87

@flip111

Description

@flip111

Given this purescript code

module Foo where

import Data.Generic.Rep (class Generic)
import Data.Argonaut.Encode.Class (class EncodeJson, encodeJson)
import Data.Argonaut.Aeson.Encode.Generic (genericEncodeAeson)
import Data.Argonaut.Aeson.Options as Argonaut
import Data.Argonaut.Core (stringify)

data Bla = Bla
  { a :: Boolean
  , b :: Boolean
  }
  | Foo
instance encodeJsonBla :: EncodeJson Bla where
  encodeJson = genericEncodeAeson Argonaut.defaultOptions
derive instance genericBla :: Generic Bla _

bla :: Bla
bla = Bla {a: true, b: false}

json :: String
json = stringify (encodeJson bla)

The code uses this purescript library as per recommendation of this repositories readme https://pursuit.purescript.org/packages/purescript-argonaut-aeson-generic/0.4.1

Output of the code is

{"tag":"Bla","contents":{"b":false,"a":true}}

Then with this haskell program

{-# LANGUAGE OverloadedStrings #-}

import Data.Aeson (FromJSON, eitherDecode)
import GHC.Generics (Generic)

data Bla = Bla
  { a :: Bool
  , b :: Bool
  }
  | Foo
  deriving (Show, Generic)
instance FromJSON Bla

main :: IO ()
main = do
  putStrLn $ show (eitherDecode "{\"tag\":\"Bla\",\"contents\":{\"a\":true,\"b\":false}}" :: Either String Bla)
  putStrLn $ show (eitherDecode "{\"tag\":\"Bla\",\"a\":true,\"b\":false}" :: Either String Bla)

https://play.haskell.org/saved/iSQjGLvY

Output is

Left "Error in $: parsing Main.Bla(Bla) failed, key \"a\" not found"
Right (Bla {a = True, b = False})

This old library had an option which i think get rid of the contents field for Aeson https://github.com/eskimor/purescript-argonaut-generic-codecs/blob/master/src/Data/Argonaut/Generic/Aeson.purs#L37 But i didn't test this.

When i did a search for client side libraries i found these:

Does someone have a recommendation of how to get client side encoding compatible with purescript-bridge?

Examples with
purescript version 0.15.10
GHC version 9.4.6 (stackage LTS 21.9)
github: eskimor/purescript-bridge 5ee8778
package set https://github.com/purescript/package-sets/releases/download/psc-0.15.10-20230828/packages.dhall

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions