1

I'm looking for a tool or a way to analyze standard JavaBeans source code (with getters and setters) and generate some sort of json descriptors..
maybe with grunt or ant or whatever.
example:

FilterBean.java:

package com.abc.beans;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


public class FilterBean implements Serializable {
    private static final long serialVersionUID = 7490361447912259765L;

    private Map<String, List<LabelValueBean>> filterMapList;
    private String name;

    public Map<String, List<LabelValueBean>> getFilterMapList() {
        return this.filterMapList;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

LabelValueBean.java:

package com.abc.beans;

import java.io.Serializable;    
import java.util.List;

public class LabelValueBean implements Serializable {
    private static final long serialVersionUID = 1237198378921379812L;

    private String label;
    private Integer id;
    private List<String> values;

    public String getLabel() {
        return this.label;
    }
    public void setLabel(String label) {
        this.label = label;
    }
    public Integer getId() {
        return this.idlabel;
    }
    public List<String> getValues() {
        return this.values;
    }
    public void setValues(List<String> values) {
        this.values = values;
    }
}

outputs something like:

com.abc.beans.FilterBean.json:

{
  "name" : {
    "type" : "String",
    "setter" : true
  },
  "filterMapList" : {
    "type" : "Map",
    "innerType" : "com.abc.beans.LabelValueBean",
    "setter" : false
  }
}

com.abc.beans.LabelValueBean.json:

{
  "label" : {
    "type" : "String",
    "setter" : true
  },
  "values" : {
    "type" : "Array",
    "innerType" : "String",
    "setter" : true
  },
  "id" : {
    "type" : "Integer",
    "setter" : false
  }
}

any idea ?

aleclofabbro
  • 1,635
  • 1
  • 18
  • 35

1 Answers1

3

There's plenty of tools that will take an object-graph and convert them to JSON. Jackson and GSON are two that are commonly used.

But these things only represent data, and may not given you the full picture about the structure. If you want to communicate the structure outside of a Java environment what you may want to do is generate a "JSON Schema". There's another question which discusses how you might do that with a Jackson module.

Community
  • 1
  • 1
brindy
  • 4,585
  • 2
  • 24
  • 27
  • i'm giving a look to Jackson, it seems to be a good lib.. unfortunately i can't find a fast & simple solution with it.. i'd need a fast build tool. – aleclofabbro Jun 11 '14 at 16:40
  • What you could do is take the contents of the main method from the answer I linked to and then run it using Groovy via Ant. http://groovy.codehaus.org/The+groovy+Ant+Task – brindy Jun 11 '14 at 21:35
  • there's also a little pkg built on top of Jackson [JJSChema](https://github.com/reinert/JJSchema), that did the job, i'll try that Groovy-Ant too.. you pointed me to the right direction anyway, Thank you! – aleclofabbro Jun 12 '14 at 11:09