Skip to content

Instantly share code, notes, and snippets.

@bubudrc
Forked from vzsg/1_JSONifyTag.swift
Created December 10, 2021 14:49
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 bubudrc/2874dde8823f0a58d4d4d4259a5ec0c3 to your computer and use it in GitHub Desktop.
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)
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"
}
}
}
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)
}
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
}
<!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>
<!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