Skip to content

Commit b84ba5c

Browse files
author
Stephane Piejdou
committed
refactoring: one class per file
1 parent 2dc5a3a commit b84ba5c

File tree

10 files changed

+212
-174
lines changed

10 files changed

+212
-174
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target/
2+
.idea/
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.p2sdev.jsonToOrmMapper.convert;
2+
3+
import com.p2sdev.jsonToOrmMapper.enums.JSONTypes;
4+
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
8+
import java.util.*;
9+
import java.util.stream.Collectors;
10+
11+
/**
12+
*
13+
* @author p2sdev.com
14+
* This class build the @Table representing each @JSONObject of
15+
* the source JSON data. The keys of the @JSONObject will be the attributes
16+
* of the @Table.
17+
*
18+
*/
19+
class BuildTables {
20+
21+
private List<Table> tables;
22+
23+
public BuildTables(Map<String, JSONTypes> keysToJSONTypes, JSONObject baseJsonObj) {
24+
convertToTable(null, keysToJSONTypes, baseJsonObj);
25+
buildTables(baseJsonObj, keysToJSONTypes);
26+
}
27+
28+
public List<Table> getTables(){
29+
return tables;
30+
}
31+
32+
private List<Table> buildTables(JSONObject json, Map<String, JSONTypes> jsonAttributes) {
33+
for(String key : jsonAttributes.keySet()) {
34+
if(jsonAttributes.get(key) == JSONTypes.JSONOBJECT) {
35+
JSONObject jsonSub = json.getJSONObject(key);
36+
buildTable(jsonSub, key);
37+
}else if(jsonAttributes.get(key) == JSONTypes.JSONARRAY) {
38+
// get the first element and ignore the rest
39+
// Only process if array element is JSONObject
40+
try {
41+
JSONObject jsonSub = json.getJSONArray(key).getJSONObject(0);
42+
buildTable(jsonSub, key);
43+
}catch(JSONException e) {
44+
// JSONArray doesn't hold JSONObjects
45+
System.out.println("Error: JSONArray doesn't hold JSONObjects");
46+
}
47+
48+
}
49+
}
50+
return tables;
51+
}
52+
53+
private void buildTable(JSONObject json, String key) {
54+
Map<String, JSONTypes> attributes = new HashMap<>();
55+
56+
Iterator<String> keys = json.keys();
57+
58+
while(keys.hasNext()) {
59+
String aKey = keys.next();
60+
Object value = json.get(aKey);
61+
attributes.put(aKey, JsonTypes.getType(value));
62+
}
63+
convertToTable(key, attributes, json);
64+
buildTables(json, attributes);
65+
}
66+
67+
/**
68+
* Check if the data object can be converted into a table and convert if convertible
69+
* To be converted into a table the data Object should have one or more @DataTypes
70+
* @param tableName : Used as table name.
71+
* @param attributes : a mapping of JSON keys to @JSONTypes
72+
* @param json : a json Object from the source data that will be converted into a @Table
73+
*/
74+
private void convertToTable(String tableName, Map<String, JSONTypes> attributes, JSONObject json) {
75+
76+
Collection<JSONTypes> currentDataTypes = attributes.values();
77+
78+
List<JSONTypes> dataTypes = Arrays.asList(JSONTypes.BOOLEAN,
79+
JSONTypes.CHARACTER, JSONTypes.INTEGER, JSONTypes.DATE, JSONTypes.STRING);
80+
81+
82+
List<JSONTypes> intersection = currentDataTypes.stream()
83+
.distinct()
84+
.filter(dataTypes::contains)
85+
.collect(Collectors.toList());
86+
87+
if((tableName == null || tableName.isEmpty()) && intersection.size()==0 ) {
88+
// the jsonObject cannot be converted into a table
89+
return;
90+
}
91+
92+
Table table;
93+
94+
if((tableName == null || tableName.isEmpty()) && intersection.size() != 0) {
95+
table = new Table.TableBuilder()
96+
.setAttributes(attributes)
97+
.setRawJson(json)
98+
.build();
99+
}
100+
101+
else {
102+
table = new Table.TableBuilder()
103+
.setAttributes(attributes)
104+
.setTableName(tableName)
105+
.setRawJson(json)
106+
.build();
107+
}
108+
109+
if(table != null) {
110+
if(tables == null) {
111+
tables = new ArrayList<>();
112+
}
113+
tables.add(table);
114+
}
115+
}
116+
117+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.p2sdev.jsonToOrmMapper.convert;
2+
3+
import com.p2sdev.jsonToOrmMapper.enums.JSONTypes;
4+
5+
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
9+
import java.time.LocalDate;
10+
import java.time.format.DateTimeFormatter;
11+
12+
public class JsonTypes {
13+
14+
/**
15+
* Given the value of a json key identify the matching @JSONTypes
16+
* @param value : the value of a particular json key
17+
* @return @JSONTypes
18+
*/
19+
public static JSONTypes getType(Object value) {
20+
String data = value.toString();
21+
if(data == null) {
22+
return JSONTypes.STRING;
23+
}
24+
25+
// Check if it is a JSONObject
26+
try {
27+
new JSONObject(data);
28+
return JSONTypes.JSONOBJECT;
29+
}catch(JSONException e) {}
30+
31+
// Then check if it is JSONArray
32+
try {
33+
new JSONArray(data);
34+
return JSONTypes.JSONARRAY;
35+
}catch(JSONException e) {}
36+
37+
// Then check if it is a Boolean
38+
try {
39+
if(data.equals("true") || data.equals("false"))
40+
return JSONTypes.BOOLEAN;
41+
}catch(Exception e) {}
42+
43+
// Then check if it is a Character
44+
try {
45+
if(data.length() == 1) {
46+
try {
47+
Integer.parseInt(data);
48+
return JSONTypes.INTEGER;
49+
}catch(Exception e) {}
50+
51+
data.charAt(0);
52+
return JSONTypes.STRING;
53+
}
54+
}catch(Exception e) {}
55+
56+
// Then check if it is an Integer/Number
57+
try {
58+
Integer.parseInt(data);
59+
return JSONTypes.INTEGER;
60+
}catch(Exception e) {}
61+
62+
// Then check if it is a Date
63+
try {
64+
// make date to accept date and date/time format
65+
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("MM-dd-yyyy");
66+
// DateTimeFormatter f2 = DateTimeFormatter.ofPattern("MM/dd/yyyy");
67+
LocalDate.parse(data, f1);
68+
return JSONTypes.DATE;
69+
}catch(Exception e) {}
70+
71+
// Lastly if none of the above return String type
72+
return JSONTypes.STRING;
73+
}
74+
}

0 commit comments

Comments
 (0)