vendredi 27 juillet 2018

Java - Get all local field values of a object as map

I want to retrieve a map of field-values present in a java object. I tried com.google.Gson and org.apache.commons.lang3.builder.ToStringBuilder to retrieve all the details regarding the given object. But following problems occured :

  1. com.google.Gson:

    Gson gson = new GsonBuilder.create();
    String str = gson.reflectionToString(myObject);
    
    

    StackOverFlowError occurs for some objects, even a map having JSONObject fails miserably.

  2. org.apache.commons.lang3.builder.ToStringBuilder:

    String str = ToStringBuilder.reflectionToString(myObject);
    
    

    While using the above API for HashMap, variables inside the HashMap are displayed but contents of the HashMap are not visible.

    Output :

    samplePackage.CustomHashMap@5fec459b[accessOrder=false,threshold=12,loadFactor=0.75]

Help me to find the perfect solution !





Aucun commentaire:

Enregistrer un commentaire