1
+ package com .howtodoinjava .core .objectToMap ;
2
+
3
+ import com .fasterxml .jackson .databind .ObjectMapper ;
4
+ import com .fasterxml .jackson .dataformat .javaprop .JavaPropsMapper ;
5
+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
6
+ import com .google .gson .Gson ;
7
+ import com .google .gson .GsonBuilder ;
8
+ import com .google .gson .JsonDeserializationContext ;
9
+ import com .google .gson .JsonDeserializer ;
10
+ import com .google .gson .JsonElement ;
11
+ import com .google .gson .JsonParseException ;
12
+ import com .google .gson .JsonPrimitive ;
13
+ import com .google .gson .JsonSerializationContext ;
14
+ import com .google .gson .JsonSerializer ;
15
+ import com .google .gson .reflect .TypeToken ;
16
+ import java .io .IOException ;
17
+ import java .lang .reflect .Field ;
18
+ import java .lang .reflect .Type ;
19
+ import java .text .SimpleDateFormat ;
20
+ import java .time .LocalDate ;
21
+ import java .time .format .DateTimeFormatter ;
22
+ import java .util .Arrays ;
23
+ import java .util .HashMap ;
24
+ import java .util .List ;
25
+ import java .util .Map ;
26
+ import java .util .Properties ;
27
+ import java .util .stream .Collectors ;
28
+
29
+ public class ConvertObjectToMapExample {
30
+
31
+ public static void main (String [] args ) throws IOException {
32
+
33
+ Employee employee = new Employee (1 , "Alex" ,
34
+ LocalDate .of (1995 , 1 , 2 ),
35
+ List .of ("Delhi" , "Nevada" ),
36
+ List .of (new Role (11 , "Finance" ), new Role (12 , "HR" )));
37
+
38
+ Map <String , String > employeeMap ;
39
+
40
+ employeeMap = convertObjectToMapUsingObjectMapper (employee );
41
+ System .out .println (employeeMap );
42
+
43
+ Properties employeeProperties = convertObjectToMapUsingJavaPropsMapper (employee );
44
+ System .out .println (employeeProperties );
45
+
46
+ employeeMap = convertObjectToMapUsingGson (employee );
47
+ System .out .println (employeeMap );
48
+
49
+ System .out .println (toKeyValuePairs (employee ));
50
+ }
51
+
52
+ public static Map <String , Object > toKeyValuePairs (Object instance ) {
53
+ return Arrays .stream (Employee .class .getDeclaredFields ())
54
+ .collect (Collectors .toMap (
55
+ Field ::getName ,
56
+ field -> {
57
+ try {
58
+ Object result = null ;
59
+ field .setAccessible (true );
60
+ result = field .get (instance );
61
+ return result != null ? result : "" ;
62
+ } catch (Exception e ) {
63
+ return "" ;
64
+ }
65
+ }));
66
+ }
67
+
68
+ static Map <String , String > convertObjectToMapUsingObjectMapper (Employee employee ) {
69
+
70
+ ObjectMapper objectMapper = new ObjectMapper ();
71
+ objectMapper .registerModule (new JavaTimeModule ());
72
+ objectMapper .setDateFormat (new SimpleDateFormat ("yyyy-MM-dd" ));
73
+
74
+ return objectMapper .convertValue (employee , Map .class );
75
+ }
76
+
77
+ static Properties convertObjectToMapUsingJavaPropsMapper (Employee employee )
78
+ throws IOException {
79
+
80
+ JavaPropsMapper mapper = new JavaPropsMapper ();
81
+ mapper .registerModule (new JavaTimeModule ());
82
+ mapper .setDateFormat (new SimpleDateFormat ("yyyy-MM-dd" ));
83
+
84
+ Properties properties = mapper .writeValueAsProperties (employee );
85
+ return properties ;
86
+ }
87
+
88
+ static Map <String , String > convertObjectToMapUsingGson (Employee employee ) {
89
+ Gson gson = new GsonBuilder ()
90
+ .registerTypeAdapter (LocalDate .class , new LocalDateAdapter ())
91
+ .create ();
92
+
93
+ return gson .fromJson (gson .toJson (employee ),
94
+ new TypeToken <HashMap <String , Object >>() {
95
+ }.getType ()
96
+ );
97
+ }
98
+ }
99
+
100
+ class LocalDateAdapter implements JsonSerializer <LocalDate >, JsonDeserializer <LocalDate > {
101
+
102
+ @ Override
103
+ public JsonElement serialize (LocalDate date , Type typeOfSrc , JsonSerializationContext context ) {
104
+ return new JsonPrimitive (date .format (DateTimeFormatter .ISO_LOCAL_DATE )); // "yyyy-mm-dd"
105
+ }
106
+
107
+ @ Override
108
+ public LocalDate deserialize (JsonElement jsonElement , Type type ,
109
+ JsonDeserializationContext jsonDeserializationContext ) throws JsonParseException {
110
+ String ldString = jsonElement .getAsString ();
111
+ return LocalDate .parse (ldString , DateTimeFormatter .ISO_LOCAL_DATE );
112
+ }
113
+ }
0 commit comments