0

Is there some way to parse .java files into JSON format?

Suppose that we have a class that contains different fucntions such as:

public static int getMax(int[] inputArray){ 
    int maxValue = inputArray[0]; 
    for(int i=1;i < inputArray.length;i++){ 
      if(inputArray[i] > maxValue){ 
         maxValue = inputArray[i]; 
      } 
    } 
    return maxValue; 
  }

I would like to parse it so I can store in the JSON file information about this fucntion, such as its name, type, inputs etc.

How is this possible to do? Is there any program or framework that perhaps can do this?

For example, this is what I would like my JSON to contain:

{
   "function": [

      {
         "name":"getMax",
         "language": "Java",
         "type": "public static int",
         "other_info": "whatever"
      },

      {
         "name":"getMin",
         "language": "Java",
         "type": "public static int",
         "other_info": "whatever"
      }

   ]
} 

Any info much appreciated!

M. Prk
  • 11
  • 2
  • Yes it is possible. AFAIK nobody has done it. Is it a good idea? No, IMO. You are better off using an ordinary Java parser, building an in-memory parse tree and using it directly or populating a database from selected info. – Stephen C May 09 '17 at 10:52
  • @StephenC sounds alright to me, is there some good java parser I could use? – M. Prk May 09 '17 at 10:57
  • take a look at http://stackoverflow.com/questions/31073624/how-to-convert-json-objects-to-pojo-from-gson – Saurabh Jhunjhunwala May 09 '17 at 10:58
  • I'm sure there is. However, asking for recommendations is off-topic. Even in comments. – Stephen C May 09 '17 at 11:59
  • @SaurabhJhunjhunwala - That is solving a completely different problem. – Stephen C May 09 '17 at 12:00

1 Answers1

0

You could use Gson (Json <=> Java API by Google), to convert classes to to Json:

String json =gson.toJson(classToJson);
classname jsonToClass = gson.fromJson(jsonString, classname.class);

But why would you want to do it with methods ? I'm curious.

Youri
  • 442
  • 5
  • 15
  • 1
    This example code is converting *instances* of classes to and from JSON. The question is about converting Java *source code* to JSON. (Or, more precisely, parsing and extracting details from Java source code, and emitting them as JSON.) – Stephen C May 09 '17 at 12:02