GithubHelp home page GithubHelp logo

Comments (3)

justinwoo avatar justinwoo commented on June 19, 2024

Unit is not meant to have some JSON representation or anything. It seems like what you should be doing is defining a different function to call upstream such that the response bodies aren't automatically parsed.

Regardless what you use, the initial JSON parsing will fail when dealing with the invalid JSON string. ReadForeign really only deals with how to deal with actual Foreign values being decoded, not the actual parsing of the JSON string.

from purescript-simple-json.

justinwoo avatar justinwoo commented on June 19, 2024

You don't have to take my word for it, look at how the readJSON methods work by first calling parseJSON, which is a separate function:

-- | Read a JSON string to a type `a` while returning a `MultipleErrors` if the
-- | parsing failed.
readJSON :: forall a
. ReadForeign a
=> String
-> E a
readJSON = runExcept <<< (readImpl <=< parseJSON)
-- | Read a JSON string to a type `a` using `F a`. Useful with record types.
readJSON' :: forall a
. ReadForeign a
=> String
-> F a
readJSON' = readImpl <=< parseJSON

parseJSON :: String -> F Foreign
parseJSON
= ExceptT
<<< Identity
<<< lmap (pure <<< ForeignError <<< message)
<<< runPure
<<< try
<<< EU.runEffectFn1 _parseJSON
where
-- Nate Faubion: "It uses unsafePerformEffect because that’s the only way to catch exceptions and still use the builtin json decoder"
runPure = unsafePerformEffect

from purescript-simple-json.

cah6 avatar cah6 commented on June 19, 2024

Ack, sorry for the delay here. You're right, of course; when I made this ticket I had forgotten that a completely blank input is indeed invalid JSON.

For anybody else that might stumble on this: what we ended up doing was parameterizing our request method on a "how to handle the response" input (more or less of type String -> E a) and giving it readJSON in the usual case but giving it const (Right unit) in the case where the expected response is unit.

Closing this, thank you!

from purescript-simple-json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.