Skip to content

Instantly share code, notes, and snippets.

@systemed
Created March 5, 2022 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save systemed/922d7cc7e3bbdc85e4bae466863dc27f to your computer and use it in GitHub Desktop.
Save systemed/922d7cc7e3bbdc85e4bae466863dc27f to your computer and use it in GitHub Desktop.
Serialise [String:Any?] to JSON in Swift
// This can be used to serialise the results of a Eureka form to JSON
// requires AnyCodable from https://github.com/Flight-School/AnyCodable
let results:[String:AnyCodable] = form.values().mapValues { v in
return AnyCodable(v)
}
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let json = try! encoder.encode(results)
print(String(data: json, encoding: .utf8)!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment