Skip to content

Add fromJson and toJson #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Added `fromJsonString` and `toJsonString` (#109 by @sigma-andex)

Bugfixes:

Expand Down
3 changes: 2 additions & 1 deletion packages.dhall
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let upstream =
https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall
https://github.com/purescript/package-sets/releases/download/psc-0.15.2-20220612/packages.dhall
sha256:9876aee1362a5dac10061768c68a7ecc4a59ca9267c3760f7d43ea9d3812ec11

in upstream
21 changes: 10 additions & 11 deletions src/Data/Argonaut/Decode.purs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
module Data.Argonaut.Decode
( module Data.Argonaut.Decode.Class
( fromJsonString
, module Data.Argonaut.Decode.Class
, module Data.Argonaut.Decode.Combinators
, module Data.Argonaut.Decode.Error
, module Data.Argonaut.Decode.Parser
) where

import Prelude

import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
import Data.Argonaut.Decode.Combinators
( getField
, getFieldOptional
, getFieldOptional'
, defaultField
, (.:)
, (.:!)
, (.:?)
, (.!=)
)
import Data.Argonaut.Decode.Combinators (getField, getFieldOptional, getFieldOptional', defaultField, (.:), (.:!), (.:?), (.!=))
import Data.Argonaut.Decode.Error (JsonDecodeError(..), printJsonDecodeError)
import Data.Argonaut.Decode.Parser (parseJson)
import Data.Either (Either)

-- | Parse and decode a json in one step.
fromJsonString :: forall json. DecodeJson json => String -> Either JsonDecodeError json
fromJsonString = parseJson >=> decodeJson
19 changes: 9 additions & 10 deletions src/Data/Argonaut/Encode.purs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
module Data.Argonaut.Encode
( module Data.Argonaut.Encode.Class
, module Data.Argonaut.Encode.Combinators
, toJsonString
) where

import Prelude

import Data.Argonaut.Core (stringify)
import Data.Argonaut.Encode.Class (class EncodeJson, encodeJson)
import Data.Argonaut.Encode.Combinators
( assoc
, assocOptional
, extend
, extendOptional
, (:=)
, (:=?)
, (~>)
, (~>?)
)
import Data.Argonaut.Encode.Combinators (assoc, assocOptional, extend, extendOptional, (:=), (:=?), (~>), (~>?))

-- | Encode and stringify a type in one step.
toJsonString :: forall t. EncodeJson t => t -> String
toJsonString = encodeJson >>> stringify