Crystal 语言以其“像 Ruby 一样开发,像 C 一样快”而著称,但其强类型的特性要求我们在处理 JSON 时必须定义严格的映射映射。本工具能根据您的 JSON 数据,自动生成符合 Crystal 语法的 struct 或 class 定义,并集成内置的 JSON::Serializable 模块,实现零成本的数据反序列化。
.json 配置文件直接映射为 Crystal 对象,利用强类型检查减少运行时错误。本站工具深度适配了 Crystal 的静态类型系统与宏机制:
String, Int32/Int64, Float64, Bool, 以及 Nil。struct。Array(T),确保泛型类型准确无误。include JSON::Serializable 模块。@[JSON::Field(key: "...") ] 标注。String? 的可空声明,提升代码健壮性。User 或 ApiResponse)。.cr 源文件中。原始 JSON:
{
"user_id": 123,
"name": "Crystal Dev",
"is_active": true,
"tags": ["performance", "safety"]
}
生成的 Crystal 代码 (本站处理):
require "json"
struct User
include JSON::Serializable
@[JSON::Field(key: "user_id")]
property user_id : Int32
property name : String
property is_active : Bool
property tags : Array(String)
end
JSON::Serializable 字段标注。