从生成的Java文件JSON [英] Generating JSON from java file

查看:300
本文介绍了从生成的Java文件JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有生成基于它的内容来自未编译Java类'东西'标准或存在方式?所以基本上是这样的:

Is there a standard or existing way to generate 'something' from an uncompiled java class based on its contents? So basically something like this:

@MakeJsonDocumentation
public class ExistingClass{
    private name = "";

    public ExistingClass(String name){
        this.name = name;
    }

    @JsonField
    public String getName(){
        return this.name;
    }

    @JsonField
    public void setName(String name){
        this.name = name;
    }

    @JsonMethod
    public void someMethod(String text){
        System.out.println("someMethod " + text)
    }

    @JsonMethod
    public void otherMethod(){
        System.out.println("otherMethod")
    }
}

弄成这个样子

{
  "ExistingClass": {
    "Fields": {
      "Name": "String"
    },
    "Methods": {
      "someMethod": {
        "Parameters": {
          "Type": "String",
          "Name": "text"
        },
        "Returns": "Nothing"
      },
      "otherMethod": {
        "Parameters": "Nothing",
        "Returns": {
          "Type": "String"
        }
      }
    }
  }
}

如果没有的话,是有可能做到这一点编译时的注解,因为我想生成自动化,而不必写一个解析器和每次我改变一些关于一类把它通过解析器为了得到一个向上最新的数据表。

And if there isn't, is it possible to do this with compile-time annotations because I'd like to automate the generation instead of having to write a parser and everytime I change something about a class throw it through the parser in order to get an up-to-date datasheet.

我在黑暗中是那种在这里,我只知道我想要什么,但不知道如何完成它,所以最起码一些搜索关键字到正确的方向将是非常受欢迎的:P

I'm kind of in the dark here, I only know what I want but no idea how to accomplish it, so at very least some search-keywords into the right direction would be very welcome :p

推荐答案

要实现的Json你贴上面可以是:

To achieve Json you posted above you can by:


  • 使用反射来提取所有的字段名和方法

  • 使用的JSONObject(JSON解析器/建设者)

这是未编译Java类

然而,反思与工程实例(或所有字段/方法必须是静态的)

However, reflection works with instances (or all fields/methods must be static)

这篇关于从生成的Java文件JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆