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

Add SCLF-0001: JSON Key Strategies #1301

Merged
merged 1 commit into from
Nov 16, 2017

Conversation

parkera
Copy link
Contributor

@parkera parkera commented Nov 6, 2017

Add a proposal for handling snake and camel case keys in JSONEncoder.

@parkera parkera changed the title Add SCLF-0001 document Add SCLF-0001: JSON Key Strategies Nov 6, 2017
let goodbyeCruelWorld: Int
let key: Int

private enum CodingKeys : CodingKey {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should have a raw type of String, I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup


Converting from camel case to snake case:

1. Splits words at the boundary of lower-case to upper-case
Copy link
Contributor

Choose a reason for hiding this comment

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

If I undestand correctly, this implies imageURL becomes image_URL, right? Or rather image_url I guess.
Can we add examples with acronyms to make it clear?

Copy link
Contributor

@bubski bubski Nov 6, 2017

Choose a reason for hiding this comment

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

ah, right

Lowercases the entire string

so image_url would be the one, yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yah, check out the test cases at swiftlang/swift#12779 for some additional examples.

1. Splits words at the boundary of lower-case to upper-case
2. Inserts `_` between words
3. Lowercases the entire string
4. Preserves starting and ending `_`.
Copy link
Contributor

Choose a reason for hiding this comment

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

How would we approach numbers?
E.g. what would abc123def be resolved to?

Copy link
Contributor Author

@parkera parkera Nov 7, 2017

Choose a reason for hiding this comment

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

abc123def - no uppercase characters to split on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

abc123Def would be abc123_def

@ianpartridge
Copy link
Contributor

@parkera do you want to merge this or leave it as a PR?

@parkera
Copy link
Contributor Author

parkera commented Nov 16, 2017

I think I'll go ahead and merge this now.

@parkera parkera merged commit 12b4635 into swiftlang:master Nov 16, 2017
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

Successfully merging this pull request may close these issues.

3 participants