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

Extend 'Set' to conform to protocol 'JSONConvertible' #242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Sources/PerfectLib/JSONConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ extension Array: JSONConvertible {
}
}

extension Set: JSONConvertible {
/// Convert a Set into JSON text.
public func jsonEncodedString() throws -> String {
var s = "["
var first = true
for v in self {
if !first {
s.append(",")

Choose a reason for hiding this comment

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

Is there a reason you're using this for loop instead of joined(separator:)? https://developer.apple.com/documentation/swift/array/1690077-joined

Copy link
Author

Choose a reason for hiding this comment

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

Hi Alex, I went with the same (existing) approach used in Array's extension. My pull request was intended to let others know that Set is not conforming to protocol JSONConvertible, as stated in the JIRA issue I created (ISS-556). Feel free to implement it in any other (better) way. Thanks for looking into it!

Copy link
Contributor

Choose a reason for hiding this comment

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

please adding a test script as well to prove this work. Thank you!

} else {
first = false
}
s.append(try jsonEncodedStringWorkAround(v))
}
s.append("]")
return s
}
}

extension Dictionary: JSONConvertible {
/// Convert a Dictionary into JSON text.
public func jsonEncodedString() throws -> String {
Expand Down