git.iko.soy/rowtype-yoga/purescript-yoga-json
on main help sign in
Add instances for Set
commitdf6f94a6cdcf7132846ab7c18eced216252db771 authorMason Mackaman <masondeanm@aol.com> dateSun 26 Feb 2023, 22:46 (4 years ago) parent6b0bc40
2 files changed+11
src/Yoga/JSON.purs+9 test/BasicsSpec.purs+2
src/Yoga/JSON.purs+9
@@ -55,6 +55,8 @@ import Data.Maybe (Maybe(..), fromMaybe, fromMaybe', maybe)
5555import Data.Newtype (class Newtype, unwrap, wrap)
5656import Data.Nullable (Nullable, toMaybe, toNullable)
5757import Data.Number as Number
58+import Data.Set (Set)
59+import Data.Set as Set
5860import Data.String.NonEmpty.Internal (NonEmptyString)
5961import Data.String.NonEmpty.Internal as NonEmptyString
6062import Data.Symbol (class IsSymbol, reflectSymbol)
@@ -575,6 +577,13 @@ else instance (ReadForeign a) ⇒ ReadForeign (Map BigInt a) where
575577else instance (Newtype nt key, ReadForeign (Map key value)) ⇒ ReadForeign (Map nt value) where
576578 readImpl = (readImpl ∷ (_ → _ (Map key value))) >>> map (unsafeCoerce ∷ (Map key value → Map nt value))
577579
580+-- Set instances
581+instance (WriteForeign a) ⇒ WriteForeign (Set a) where
582+ writeImpl set = writeImpl (Set.toUnfoldable set :: Array a)
583+
584+instance (Ord a, ReadForeign a) ⇒ ReadForeign (Set a) where
585+ readImpl f = Set.fromFoldable <$> (readImpl f :: F (Array a))
586+
578587-- Date instances
579588instance WriteForeign JSDate where
580589 writeImpl = JSDate.toISOString >>> unsafePerformEffect >>> writeImpl
test/BasicsSpec.purs+2
@@ -12,6 +12,7 @@ import Data.Map as Map
1212import Data.Maybe (Maybe(..), fromJust, fromMaybe')
1313import Data.Newtype (class Newtype, un)
1414import Data.Nullable as Nullable
15+import Data.Set as Set
1516import Data.String.NonEmpty (NonEmptyString, nes)
1617import Data.Time.Duration (Days(..), Hours(..), Milliseconds(..), Minutes(..), Seconds(..))
1718import Data.Tuple (Tuple(..))
@@ -68,6 +69,7 @@ spec = describe "En- and decoding" $ do
6869 $ roundtrips (Map.fromFoldable [ (Inty 4 /\ "B"), (Inty 8 /\ "D") ])
6970 it "roundtrips Map with BigInt newtype keys"
7071 $ roundtrips (Map.fromFoldable [ (BigInty (BigInt.fromInt 5) /\ "B"), (BigInty big /\ "D") ])
72+ it "roundtrips Set String" $ roundtrips (Set.fromFoldable [ "A", "B", "C" ])
7173 it "roundtrips BigInt" do
7274 let
7375 inputStr = """{ "number":1, "big": 18014398509481982, "mediumBig": 1652955871799, "smallBig": 10 }"""