-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJacksonReader.java
100 lines (71 loc) · 3.19 KB
/
JacksonReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.learning.api.jackson;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonReader {
/*
String json2 ="{ \"userName\" : \"akashtayal999\", \"firstName\" : \"akash\",\"lastName\" : \"tayal\", \"address\": { \"houseNo\": 5,\"streetName\": \"avantika\" } }";
*/
/*
*
Sample Json created for Example
{
"userName": "testuser",
"firstName": "akash",
"lastName": "tayal",
"phoneNo":[ {
"number": 9899456123
}, {
"number": 9899454123
} ],
"emailID": "akashtayal999@gmail.com",
"address": {
"houseNo": 23,
"streetName": "kamla, road",
"city":"kavinagar",
"state": "uttar pradesh",
"pin":201002
}
}
*/
public Object readJsonFromStringJson(String json, Class classVar) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
return objectMapper.readValue(json, classVar);
}
public Object readJsonFromFile(File file, Class classVar) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
return objectMapper.readValue(file, classVar);
}
public Object readJsonFromFilePath(String filePath, Class classVar) throws JsonParseException, JsonMappingException, IOException {
File file = new File(filePath);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
return objectMapper.readValue(file, classVar);
}
//Refer to the file attached AllUserDetails.json
public Object[] readJsonArrayToObjectArray(File file, Class classVar) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
return objectMapper.readValue(file, classVar.arrayType());
}
//Refer to the file attached UserDetails.json
public Map<String,Object> readJsonArrayToMap(File file) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
return objectMapper.readValue(file, new TypeReference<Map<String,Object>>() {});
}
}