Skip to content

Add handling of duplicate keys #714

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 3 commits into from
Jun 23, 2019
Merged

Add handling of duplicate keys #714

merged 3 commits into from
Jun 23, 2019

Conversation

Lysxia
Copy link
Collaborator

@Lysxia Lysxia commented Jun 22, 2019

Fixes #531.

This adds a parameterized parser jsonWith to let users decide what to do with objects with duplicate keys. A few common variants are also included.

Benchmarks to guard against regression (using the aeson-parse benchmark) (no difference on GHC 8.6, 8.4, there seems to be a 2% slowdown on 8.2.2) https://gist.github.com/Lysxia/beb1aa201c79f576b94c509af7f0abfa

Lysxia added 2 commits June 22, 2019 00:49
- This allows explicit handling of duplicate keys.
- Also remove an outdated comment about the difference between the
  parsers value and json (they have been the same for a while).
@Lysxia
Copy link
Collaborator Author

Lysxia commented Jun 22, 2019

Woohoo, a compiler bug on 7.6.3 - 7.10.3 https://travis-ci.org/bos/aeson/jobs/549001593

@bergmark
Copy link
Collaborator

Cool, thanks!

@bergmark bergmark added this to the 1.4.4.0 milestone Jun 23, 2019
@bergmark bergmark merged commit 8df0981 into haskell:master Jun 23, 2019
objectValues str val = do
-- The object parser 'objectValues' is parameterized by the object constructor.
-- See also 'jsonWith'.
type HFromList = [(Text, Value)] -> Parser Object
Copy link
Collaborator

@phadej phadej Jun 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have Either String Object as a return type. Parser is "too big".

And actually wouldn't use a type synonym, hFromList is confusing naming, mkObject is what it is

EDIT: I see HFromList is because of HashMap.fromList, too far. mkObject is a domain specific, thus better.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I forgot we're in attoparsec's Parser, not aeson's Parser (which is closer to Either). That's a fair point about the synonym, I'll remove it. Thanks for the review!

fmap (Array . Vector.fromList . ($ [])) . H.fromListWith (.) . (fmap . fmap) (:)

-- | @'fromListNoDup' kvs@ fails if @kvs@ contains duplicate keys.
parseListNoDup :: [(Text, Value)] -> Parser Object
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

point in case: here the HFromList is not used, so connecting the dots is difficult.

I'd remove the type alias.

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

Successfully merging this pull request may close these issues.

Duplicate keys
3 participants