feat: add json.swift
This commit is contained in:
21
json.swift
Executable file
21
json.swift
Executable file
@@ -0,0 +1,21 @@
|
||||
#!/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!)
|
||||
Reference in New Issue
Block a user