22 lines
463 B
Swift
Executable File
22 lines
463 B
Swift
Executable File
#!/usr/bin/env swift
|
|
|
|
// Reference
|
|
// - https://stackoverflow.com/questions/29599005/how-to-serialize-or-convert-swift-objects-to-json
|
|
|
|
import Foundation
|
|
|
|
struct Dog: Codable {
|
|
var name: String
|
|
var owner: String
|
|
}
|
|
|
|
// Encode
|
|
let dog = Dog(name: "Rex", owner: "Etgar")
|
|
|
|
let jsonEncoder = JSONEncoder()
|
|
jsonEncoder.outputFormatting = .prettyPrinted
|
|
let jsonData = try jsonEncoder.encode(dog)
|
|
let json = String(data: jsonData, encoding: .utf8)
|
|
|
|
print(json!)
|