-
-
Save bubudrc/2874dde8823f0a58d4d4d4259a5ec0c3 to your computer and use it in GitHub Desktop.
Custom Leaf tag for printing anything in the context as JSON (Vapor 4)
This file contains hidden or 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 LeafKit | |
final class JSONifyTag: UnsafeUnescapedLeafTag { | |
func render(_ ctx: LeafContext) throws -> LeafData { | |
guard let param = ctx.parameters.first else { | |
throw "no parameter provided to JSONify" | |
} | |
return LeafData.string(param.jsonString) | |
} | |
} | |
private extension LeafData { | |
var jsonString: String { | |
guard !isNil else { | |
return "null" | |
} | |
switch celf { | |
case .array: | |
let items = array!.map { $0.jsonString } | |
.joined(separator: ", ") | |
return "[\(items)]" | |
case .bool: | |
return bool! ? "true" : "false" | |
case .data: | |
return "\"\(data!.base64EncodedString())\"" | |
case .dictionary: | |
let items = dictionary!.map { (key, value) in "\"\(key)\": \(value.jsonString)" } | |
.joined(separator: ", ") | |
return "{\(items)}" | |
case .double: | |
return String(double!) | |
case .int: | |
return String(int!) | |
case .string: | |
return "\"\(string!)\"" | |
case .void: | |
return "null" | |
} | |
} | |
} |
This file contains hidden or 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 Leaf | |
// ... other imports omitted for brevity | |
public func configure(_ app: Application) throws { | |
// ... other configuration lines omitted for brevity | |
app.views.use(.leaf) | |
app.leaf.tags["jsonify"] = JSONifyTag() | |
// ... other configuration lines omitted for brevity | |
try routes(app) | |
} |
This file contains hidden or 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 Vapor | |
// ... other imports omitted for brevity | |
func routes(_ app: Application) throws { | |
app.get("example") { req async throws -> View in | |
struct ChartData: Encodable { | |
let x: [Double] | |
let labels: [String] | |
let y: [Double] | |
} | |
struct Context: Encodable { | |
let chartData: ChartData | |
} | |
let context = Context( | |
chartData: ChartData( | |
x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], | |
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], | |
y: [10, 3, 2, 6, 3, 7, 4, 2, 1, 5, 1, 5] | |
) | |
) | |
return try await req.view.render("example", context) | |
} | |
// ... other route handlers omitted for brevity | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>JSONify Example</title> | |
</head> | |
<body> | |
<h2>Check the DevTools console :)</h2> | |
<script> | |
const data = #jsonify(chartData); | |
console.log(data); | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>JSONify Example</title> | |
</head> | |
<body> | |
<h2>Check the DevTools console :)</h2> | |
<script> | |
const data = {"labels": [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ], "y": [ 10.0, 3.0, 2.0, 6.0, 3.0, 7.0, 4.0, 2.0, 1.0, 5.0, 1.0, 5.0 ], "x": [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]}; | |
console.log(data); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment