purescript-contrib/purescript-argonaut-core

fromString or jsonParser?

alarbada opened this issue · 2 comments

Hi! I'm just getting started with argonaut core, and the readme has a broken import

Specifically, this one:

import Data.Argonaut.Core (jsonParser)

Isn't it the fromString function? Are they both the same? Probably the readme should be updated, I couldn't run the examples.

Ah, the README is incorrect, but for a different reason -- jsonParser returns Either instead of Maybe.

The jsonParser function tries to parse a string of JSON into the Json type, which includes parsing objects, arrays, and so on, but it can fail if the string isn't valid JSON. This is typically what you want if you're turning some arbitrary JSON into Json.

The fromString function is only for converting strings that are meant to remain strings into Json. If the string contains "{ a: 10 }", for example, then fromString will keep it as that string whereas jsonParser will convert it into an object. It's pretty rare to use fromString in the real world.