Skip to content
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

Default empty Arrays #22

Closed
megamaddu opened this issue Oct 23, 2017 · 10 comments
Closed

Default empty Arrays #22

megamaddu opened this issue Oct 23, 2017 · 10 comments

Comments

@megamaddu
Copy link

What do you think about using [] as a "default" array value and allowing the key to be missing when parsing? (or maybe even empty or mempty for any valid types)

Not sure if this has drawbacks I'm not thinking of, but it'd sure be nice to get rid of a bunch of my Maybe (Array a) fields. Nothing and [] are kind of the same thing 🙂

@jacereda
Copy link
Collaborator

Why not simply use Array a and live with the serialised key=[]?

@kritzcreek
Copy link
Collaborator

kritzcreek commented Oct 26, 2017

Nothing and [] are kind of the same thing

😱

nononooooonoo. Especially in decoding you don't want your parser to make these implicit assumptions. One common situation I can think of is an API call querying a collection.

Now (1) null might mean the collection didn't exist, whereas (2) an empty Array means it's empty. That's a very different thing though. 1) means a programmer error, and 2) means the user should populate it with some data.

Don't surprise me with dynamic coercing behaviour. I can always throw in a fromMaybe [] if I feel like it.

@justinwoo
Copy link
Owner

justinwoo commented Oct 26, 2017

I think in this case, having readJSON read from an input and then applying a series of modify functions should work right? Since the key :: Maybe a -> key :: a transformations will provide the types there and your end result being some concrete type of a record should work?

e.g.

import Data.Nullable (toMaybe)

type MyThing =
  { a :: String
  , b :: Array String
  }

json = """
  { "a": "sedf", "b": null }
"""

parse :: String -> E MyThing
parse str = modify (SProxy :: SProxy "b") (fromMaybe [] <<< toMaybe) <$> readJSON str

or something like that?

@justinwoo
Copy link
Owner

Nothing and [] are kind of the same thing

Probably works if you only use NonEmpty Array everywhere, but not entirely sure if that's what you want in your codebase either

@megamaddu
Copy link
Author

@jacereda Sorry, only referring to deserialization.

You're probably right.. unfortunately C# is really loose with nullability so I have a bunch of apis to work with which like to leave off arrays when they're empty, so I'll probably need to write custom ReadForeign implementations for those records.

@justinwoo
Copy link
Owner

Gist example for those reading this thread later: https://gist.github.com/justinwoo/9d0bb67a84c227f327da7171bb7105c2

@megamaddu
Copy link
Author

Is the toMaybe necessary? Or does the Maybe instance distinguish between missing and null?

@justinwoo
Copy link
Owner

the Maybe instance is just a hack and shouldn't be relied on at all. As noted in https://github.com/justinwoo/purescript-simple-json#warning-maybe

@megamaddu
Copy link
Author

megamaddu commented Oct 26, 2017

Seems clear enough.. are you suggesting it shouldn't be used or might be removed?

@justinwoo
Copy link
Owner

Yeah, mostly just shouldn't be used unless you're willing to lock down versions of simple-json (which is the easy part if you use psc-package I guess) and read all the release notes to make sure the behavior wasn't changed again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants