Is it a good choice to define my own JSON util class like this? package utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gson.Gson;

public class JSONUtils {

    public static final Gson gson = new Gson();

    public static String toJSON(Object o, String... fields) {
        HashMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
        for (String f : fields) {
            try {
                Field field = o.getClass().getField(f);
                map.put(f, field.get(o));
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        return gson.toJson(map);
    }

    public static String toJSONForList(List&lt;?&gt; objects, String... fields) {
        ArrayList&lt;Object&gt; list = new ArrayList&lt;Object&gt;();
        for (Object o : objects) {
            list.add(toJSON(o, fields));
        }
        return gson.toJson(list);
    }
}


Note : I need to convert list to JSON very often.
Are you changing the fields that you expose from your objects depending on certain conditions? 

On the whole it would be best to use reflection only as a last gasp. Gson works fine with collections in general see the documentation over at Google.

If you want to exclude some fields from serialization then you can use the @Expose annotation, as documented here. This requires you to annotate all fields that you want to serialize with the @Expose annotation, and to use a builder to instantiate your Gson Object like so:

Gson gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();


In general try to limit how much you are working with the type Object and reduce as far as possible your use of reflection.