首页 > 解决方案 > 给定方法和类型的 JSON 规范作为输入,如何生成 Ruby 代码?

问题描述

这是类型和方法的定义

精简版

   {
      "name": "Abi",
      "type": "EnumOfTypes",
      "enum_types": [
        {
          "name": "Contract",
          "type": "Struct",
          "struct_fields": [
            {
              "name": "value",
              "type": "Ref",
              "ref_name": "abi.AbiContract",
              "summary": null,
              "description": null
            }
          ],
          "summary": null,
          "description": null
        },
        {
          "name": "Json",
          "type": "Struct",
          "struct_fields": [
            {
              "name": "value",
              "type": "String",
              "summary": null,
              "description": null
            }
          ],
          "summary": null,
          "description": null
        },

粘贴箱:

https://pastebin.com/AinPPgrb

JSON 文档经常更改。目前 Ruby 的类、类型、方法……都是手工编写的,并且也是手工更新的。

我已经尝试了 1-2 个 Ruby 代码生成器,但它们都没有工作。是否有一个体面的 Ruby 代码生成器能够处理这个文档并且生成足够高效和简单的代码?代码应该没有任何额外的依赖或复杂的不必要的类、结构......

或者,简而言之,我如何自己编写一个简单的生成器?困难在于 Ruby 是动态类型语言。

标签: jsonrubyruby-3

解决方案


推荐阅读