(Since I use Gson quite liberally, I am sharing a Gson based approach)
Gson gson = new Gson();
Map<Object,Object> attributes = gson.fromJson(gson.toJson(value),Map.class);
What it does is:
gson.toJson(value)
will serialize your object into its equivalent Json representation. gson.fromJson
will convert the Json string to specified object. (in this example - Map
)There are 2 advantages with this approach:
toJson
method.