Skip to content

Commit 4257d7e

Browse files
committed
use jsval-json for JSON stuf
1 parent eeeb8c5 commit 4257d7e

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

solga-client-ghcjs/solga-client-ghcjs.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ library
1919
build-depends: base >= 4.8 && < 5,
2020
solga-core,
2121
ghcjs-base,
22+
jsval-json,
2223
dlist
2324
hs-source-dirs: src
2425
default-language: Haskell2010

solga-client-ghcjs/src/Solga/Client/GHCJS.hs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818
{-# LANGUAGE FlexibleContexts #-}
1919
{-# LANGUAGE JavaScriptFFI #-}
2020
{-# LANGUAGE RecordWildCards #-}
21-
module Solga.Client.GHCJS where
22-
{-
21+
module Solga.Client.GHCJS
2322
( Client(..)
2423
, SomeRequestData(..)
2524
, choose
2625
, RawRequest(..)
2726
, ToSegment(..)
2827
, WithData(..)
2928
, GetResponse(..)
29+
, JSONResponse(..)
3030
) where
31-
-}
3231

3332
import Data.Kind
3433
import Data.Proxy
@@ -43,9 +42,8 @@ import Data.Typeable (Typeable)
4342
import qualified Data.DList as DList
4443
import Data.DList (DList)
4544
import Data.String (fromString)
46-
import qualified JavaScript.JSON.Types.Class as Json
47-
import qualified JavaScript.JSON.Types.Internal as Json
48-
import GHCJS.Types (Immutable)
45+
import qualified JavaScript.JSValJSON as Json
46+
import Data.Traversable (for)
4947

5048
import Solga.Core hiding (Header)
5149

@@ -172,16 +170,21 @@ instance (Client next, KnownSymbol method) => Client (Method method next) where
172170

173171
newtype GetResponse resp a b = GetResponse {unGetResponse :: Xhr.Response resp -> a -> IO b}
174172

173+
newtype JSONResponse = JSONResponse {unJSONResponse :: Json.Value}
174+
175+
instance Xhr.ResponseType JSONResponse where
176+
getResponseTypeString _ = "json"
177+
wrapResponseType = JSONResponse
178+
175179
instance (Json.FromJSON a) => Client (JSON a) where
176180
-- note that we do not decode eagerly because it's often the case that the body
177181
-- cannot be decoded since web servers return invalid json on errors
178182
-- (e.g. "Internal server error" on a 500 rather than a json encoded error)
179-
type RequestData (JSON a) = GetResponse (Json.SomeValue Immutable) (Maybe (Either String a))
183+
type RequestData (JSON a) = GetResponse JSONResponse (IO (Maybe (Either String a)))
180184
performRequest _p req (GetResponse f) = do
181185
resp <- Xhr.xhr =<< toXhrRequest req
182-
f resp $ do
183-
data_ <- Xhr.contents resp
184-
return (Json.parseEither Json.parseJSON data_)
186+
f resp $ for (Xhr.contents resp) $ \(JSONResponse data_) ->
187+
Json.runParser Json.parseJSON data_
185188

186189
instance (Client next) => Client (ExtraHeaders next) where
187190
type RequestData (ExtraHeaders next) = RequestData next
@@ -193,8 +196,10 @@ instance (Client next) => Client (NoCache next) where
193196

194197
instance (Client next, Json.ToJSON a) => Client (ReqBodyJSON a next) where
195198
type RequestData (ReqBodyJSON a next) = WithData a (RequestData next)
196-
performRequest _p req (WithData x perf) = performRequest
197-
(Proxy @next) req{reqData = Xhr.StringData (Json.encode (Json.toJSON x))} perf
199+
performRequest _p req (WithData x perf) = do
200+
s <- Json.toJSONString =<< Json.toJSON x
201+
performRequest
202+
(Proxy @next) req{reqData = Xhr.StringData s} perf
198203

199204
instance (Client next) => Client (WithIO next) where
200205
type RequestData (WithIO next) = RequestData next

stack-ghcjs.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ packages:
22
- 'solga-core'
33
- 'solga-client-ghcjs'
44
- location:
5-
git: git@github.com:bitonic/ghcjs-base.git
6-
commit: e36e6be8a99a240c51319abd36827cc00e0c3cf2
5+
git: https://github.com/bitonic/jsval-json.git
6+
commit: 5e24033a30afd832ed064be5c51621cf433baada
77
extra-dep: true
88
allow-newer: true
99
flags: {}

0 commit comments

Comments
 (0)