Created
June 11, 2021 12:13
-
-
Save chriseidhof/7b821f72320ef34bc653fb612305c5de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Yams | |
extension String { | |
var yamlToJSON: String { | |
do { | |
guard let parsed = try Yams.load(yaml: self) else { return "" } | |
let data = try JSONSerialization.data(withJSONObject: parsed, options: [.sortedKeys, .prettyPrinted]) | |
return String(decoding: data, as: UTF8.self) | |
} catch { | |
return "\(error)" | |
} | |
} | |
} | |
@main | |
struct YamlToJSONApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
} | |
} | |
struct ContentView: View { | |
@State var input = "" | |
var body: some View { | |
HSplitView { | |
TextEditor(text: $input) | |
TextEditor(text: .constant(input.yamlToJSON)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a swift-sh script: