diff --git a/bower.json b/bower.json index 24c940d..ffc1c9c 100644 --- a/bower.json +++ b/bower.json @@ -12,9 +12,9 @@ "output" ], "dependencies": { - "purescript-prelude": "^3.1.0", - "purescript-foreign-generic": "^5.0.0", - "purescript-typelevel-prelude": "^2.6.0", + "purescript-prelude": "^3.3.0", + "purescript-foreign-generic": "^6.0.0", + "purescript-typelevel-prelude": "^2.7.0", "purescript-record": "^0.2.6", "purescript-nullable": "^3.0.0", "purescript-variant": "^4.1.0" diff --git a/src/Simple/JSON.purs b/src/Simple/JSON.purs index 1baeb06..a2e8a34 100644 --- a/src/Simple/JSON.purs +++ b/src/Simple/JSON.purs @@ -31,8 +31,8 @@ import Data.Foreign (F, Foreign, ForeignError(..), MultipleErrors, fail, readArr import Data.Foreign.Index (readProp) import Data.Foreign.Internal (readStrMap) import Data.Foreign.JSON (parseJSON) -import Data.Foreign.NullOrUndefined (NullOrUndefined(NullOrUndefined), readNullOrUndefined, unNullOrUndefined, undefined) -import Data.Maybe (Maybe(..), maybe) +import Data.Foreign.NullOrUndefined (readNullOrUndefined, undefined) +import Data.Maybe (Maybe(Nothing), maybe) import Data.Nullable (Nullable, toMaybe, toNullable) import Data.Record (get) import Data.Record.Builder (Builder) @@ -110,11 +110,8 @@ instance readBoolean :: ReadForeign Boolean where instance readArray :: ReadForeign a => ReadForeign (Array a) where readImpl = traverse readImpl <=< readArray -instance readNullOrUndefined :: ReadForeign a => ReadForeign (NullOrUndefined a) where - readImpl = readNullOrUndefined readImpl - instance readMaybe :: ReadForeign a => ReadForeign (Maybe a) where - readImpl = map unNullOrUndefined <<< readImpl + readImpl = readNullOrUndefined readImpl instance readNullable :: ReadForeign a => ReadForeign (Nullable a) where readImpl o = withExcept (map reformat) $ @@ -230,15 +227,11 @@ instance writeForeignBoolean :: WriteForeign Boolean where instance writeForeignArray :: WriteForeign a => WriteForeign (Array a) where writeImpl xs = toForeign $ writeImpl <$> xs -instance writeForeignNullOrUndefined :: WriteForeign a => WriteForeign (NullOrUndefined a) where - writeImpl (NullOrUndefined a) = maybe undefined writeImpl a - instance writeForeignMaybe :: WriteForeign a => WriteForeign (Maybe a) where - writeImpl (Just a) = writeImpl a - writeImpl Nothing = toForeign $ toNullable Nothing + writeImpl = maybe undefined writeImpl instance writeForeignNullable :: WriteForeign a => WriteForeign (Nullable a) where - writeImpl = writeImpl <<< toMaybe + writeImpl = maybe (toForeign $ toNullable Nothing) writeImpl <<< toMaybe instance writeForeignStrMap :: WriteForeign a => WriteForeign (StrMap.StrMap a) where writeImpl = toForeign <<< StrMap.mapWithKey (const writeImpl) diff --git a/test/Main.purs b/test/Main.purs index cd05209..8757a51 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -8,7 +8,6 @@ import Data.Argonaut.Core (Json) import Data.Argonaut.Parser (jsonParser) import Data.Either (Either(..), either, fromLeft, isRight) import Data.Foreign (ForeignError(..), MultipleErrors) -import Data.Foreign.NullOrUndefined (NullOrUndefined) import Data.List (List(..)) import Data.List.NonEmpty (NonEmptyList(..)) import Data.Maybe (Maybe) @@ -38,7 +37,7 @@ type MyTestNull = , b :: String , c :: Boolean , d :: Array String - , e :: NullOrUndefined (Array String) + , e :: Maybe (Array String) } type MyTestStrMap = @@ -102,7 +101,7 @@ main = run [consoleReporter] do it "works with missing Maybe fields by setting them to Nothing" do let result = readJSON "{}" - (writeJSON <$> (result :: E MyTestMaybe)) `shouldEqual` (Right """{"a":null}""") + (writeJSON <$> (result :: E MyTestMaybe)) `shouldEqual` (Right """{}""") it "fails with undefined for null with correct error message" do let result = readJSON """ @@ -116,10 +115,10 @@ main = run [consoleReporter] do it "works with proper JSON" $ roundtrips (Proxy :: Proxy MyTest) """ { "a": 1, "b": "asdf", "c": true, "d": ["A", "B"]} """ - it "works with JSON lacking NullOrUndefined field" $ roundtrips (Proxy :: Proxy MyTestNull) """ + it "works with JSON lacking Maybe field" $ roundtrips (Proxy :: Proxy MyTestNull) """ { "a": 1, "b": "asdf", "c": true, "d": ["A", "B"]} """ - it "works with JSON containing NullOrUndefined field" $ roundtrips (Proxy :: Proxy MyTestNull) """ + it "works with JSON containing Maybe field" $ roundtrips (Proxy :: Proxy MyTestNull) """ { "a": 1, "b": "asdf", "c": true, "d": ["A", "B"], "e": ["C", "D"]} """ it "works with JSON containing StrMap field" $ roundtrips (Proxy :: Proxy MyTestStrMap) """ @@ -128,12 +127,6 @@ main = run [consoleReporter] do it "works with Maybe field and existing value" $ roundtrips (Proxy :: Proxy MyTestMaybe) """ { "a": "foo" } """ - it "works with Maybe field and null value" $ roundtrips (Proxy :: Proxy MyTestMaybe) """ - { "a": null } - """ - it "works with many Maybe fields" $ roundtrips (Proxy :: Proxy MyTestManyMaybe) """ - { "a": "str", "aNull": null, "b":1, "bNull": null, "c":true, "cNull":null, "d":1.1, "dNull":null, "e":["str1", "str2", null], "eNull": null } - """ it "works with Nullable" $ roundtrips (Proxy :: Proxy MyTestNullable) """ { "a": null, "b": "a" } """