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

Recommended way to conform JSON to Hashable #1137

Open
BenShutt opened this issue Aug 4, 2023 · 4 comments
Open

Recommended way to conform JSON to Hashable #1137

BenShutt opened this issue Aug 4, 2023 · 4 comments

Comments

@BenShutt
Copy link

BenShutt commented Aug 4, 2023

What did you do?

How would you recommend conforming JSON to Hashable?
My use case looks something like this:

struct MyModel: Equatable, Hashable, Codable {
    var name: String
    var value: Int
    var json: JSON // Not Hashable error
}

What did you expect to happen?

If JSON already conformed to Hashable like it does Codable that would be most desirable.
I could extend JSON to conform to Hashable in my project, but am interested to hear what you recommend.

I understand this is a feature request rather than a bug.

Environment

  • SwiftyJSON: 5.0.1
  • Xcode Version: 14.3.1
  • Swift Version: 5.8.1
  • Swift Package Manager - Swift 5.8.1
@Wejua
Copy link

Wejua commented Apr 7, 2024

YES, This will be a very nice feature that i can use it directly in ForEach Statements in SwiftUI

@wongzigii
Copy link
Member

@BenShutt PR is welcomed.

@BenShutt
Copy link
Author

BenShutt commented Apr 22, 2024

Hey @wongzigii, my workaround was mapping the JSON to its string representation and using the Hashable of that. It didn't feel robust so I was/am curious to hear what you (all) suggest.

@Vyazovoy
Copy link

@BenShutt, your approach is pretty robust as long as you are using sortedKeys writing option for the data generation.

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