Skip to content

Commit e852e57

Browse files
closes #132 (#133)
1 parent d4ad013 commit e852e57

14 files changed

+140
-134
lines changed

java/Coded-Json/src/main/java/com/codedjson/CJson.java

+38-32
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,48 @@ public class CJson<T> extends Decode {
5151
/**
5252
* CJson parser using file path.
5353
* @param filePath
54-
* @throws Exception FileNotFoundException
54+
* @throws IllegalJsonType
55+
* @throws AbsolutePathConstraintError
56+
* @throws FileNotFoundException
5557
*/
56-
public CJson(Path filePath) throws FileNotFoundException {
58+
public CJson(Path filePath) throws FileNotFoundException, IllegalJsonType, AbsolutePathConstraintError {
5759
super(filePath.toString(), true);
5860
this.t = null;
5961
this.filePath = filePath.toString();
6062
this.baseFileObj = new File(this.filePath);
63+
contextConverter();
6164
}
6265
/**
6366
* Parser for <code>CJSON</code> content.
6467
* You can directly parse a <code>CJSON</code> string content.<br/>
6568
* <b>Import statements must have paths absolute. Otherwise it throws absolute path constraint error while deserialization</b>
6669
* @param content CJSON/JSON content in string
70+
* @throws IllegalJsonType
71+
* @throws AbsolutePathConstraintError
72+
* @throws FileNotFoundException
6773
*/
68-
public CJson(String content) {
74+
public CJson(String content) throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
6975
super(content);
7076
this.t = null;
7177
this.filePath = null;
7278
this.baseFileObj = null;
79+
contextConverter();
7380
}
7481

7582
/**
7683
* Deserializes CJSON content and returns Java Object equivalent to <code>classType</code>.
7784
* For more cababilitites, refer to <a href="https://subhendushekhar.github.io/cjson/">Official Page</a>
7885
* @param classType Java class object equivalent to target JSON
7986
* @return Java Object equivalent to <code>classType</code>
80-
* @throws IllegalJsonType
81-
* @throws AbsolutePathConstraintError
82-
* @throws FileNotFoundException
8387
*/
84-
public T deserialize(Class<T> classType) throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
85-
this.classType = classType;
86-
87-
if(checks.runtimeKeys(content))
88-
System.out.println("Warning: Runtime variables detected. To inject data, use inject() instead");
89-
90-
decodeKeywords();
88+
public T deserialize(Class<T> classType) throws UndeserializedCJSON, IllegalJsonType {
89+
if(isInjectExist && !isInjectDone)
90+
throw new UndeserializedCJSON("Runtime variables detected. Inject before deserialize.");
9191

92+
content = decodeRelativePathValues(content);
9293
json = parseJson(content);
9394

94-
content = parse().toString();
95+
this.classType = classType;
9596
if(classType.equals(String.class))
9697
t = (T) content;
9798
else
@@ -111,22 +112,22 @@ public T deserialize(Class<T> classType) throws IllegalJsonType, AbsolutePathCon
111112
* This is thrown if import statements contain relative path instead of absolute path
112113
* @throws FileNotFoundException If the imported file is not found in the directory
113114
*/
114-
public T inject(Class<T> classType, HashMap<String, Object> injectingObj) throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
115+
public T inject(Class<T> classType, HashMap<String, Object> injectingObj) throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException, UndeserializedCJSON {
116+
115117
if(injectingObj != null || injectingObj.keySet().size() != 0) {
116118
this.classType = classType;
117119

118-
decodeKeywords();
120+
contextConverter();
119121
content = replaceContent(content, injectingObj);
120122

121123
json = parseJson(content);
122124
content = parse().toString();
123-
}
124125

125-
if(classType.equals(String.class))
126-
t = (T) content;
127-
else
128-
t = gson.fromJson(content, classType);
129-
return t;
126+
}
127+
isInjectDone = true;
128+
if(isRuntimeKeysExist(content))
129+
System.out.println("All runtime values are not injected yet. Deserialization may throw exception");
130+
return deserialize(classType);
130131
}
131132
/**
132133
* Injects single key and value. Uses tag <code>&lt;variable&gt;</code><br/>
@@ -142,19 +143,16 @@ public T inject(Class<T> classType, HashMap<String, Object> injectingObj) throws
142143
* This is thrown if import statements contain relative path instead of absolute path
143144
* @throws FileNotFoundException If the imported file is not found in the directory
144145
*/
145-
public T inject(Class<T> classType, String key, Object value) throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
146+
public T inject(Class<T> classType, String key, Object value) throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException, UndeserializedCJSON {
146147
this.classType = classType;
147148

148-
decodeKeywords();
149+
contextConverter();
149150
content = replaceContent(content, key, value);
150151
json = parseJson(content);
151152
content = parse().toString();
152153

153-
if(classType.equals(String.class))
154-
t = (T) content;
155-
else
156-
t = gson.fromJson(content, classType);
157-
return t;
154+
isInjectDone = true;
155+
return deserialize(classType);
158156
}
159157
/**
160158
* Deserializes <code>CJSON</code> content and returns as string.<br/>
@@ -167,7 +165,7 @@ public T inject(Class<T> classType, String key, Object value) throws IllegalJson
167165
* @throws FileNotFoundException If the imported file is not found in the directory
168166
*/
169167
public String deserializeAsString() throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
170-
decodeKeywords();
168+
contextConverter();
171169
json = parseJson(content);
172170
return content;
173171
}
@@ -182,7 +180,7 @@ public String deserializeAsString() throws IllegalJsonType, AbsolutePathConstrai
182180
*/
183181
public T remove(String key) throws IllegalJsonType, UndeserializedCJSON, InvalidJPathError {
184182
if(!key.startsWith("$.")) throw new InvalidJPathError();
185-
if(json == null) throw new UndeserializedCJSON("Undeserialized CJSON content detected. Use deseralize() before remove()");
183+
if(classType == null) throw new UndeserializedCJSON("Undeserialized CJSON content detected. Use deseralize() before remove()");
186184

187185
removeWithKey(key);
188186

@@ -202,7 +200,7 @@ public T remove(String key) throws IllegalJsonType, UndeserializedCJSON, Invalid
202200
* @throws UndeserializedCJSON Throws if the CJSON/JSON is not deserialized. Call deserialize before remove
203201
*/
204202
public T remove(List<String> keyList) throws IllegalJsonType, UndeserializedCJSON {
205-
if(json == null) throw new UndeserializedCJSON("Undeserialized CJSON content detected. Use deseralize() before remove()");
203+
if(classType == null) throw new UndeserializedCJSON("Undeserialized CJSON content detected. Use deseralize() before remove()");
206204
for(String key: keyList)
207205
removeWithKey(key);
208206

@@ -225,4 +223,12 @@ public static String toString(Object object) throws IllegalAccessException {
225223
return "{}";
226224
return getAsString(object);
227225
}
226+
private void contextConverter() throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
227+
if(checks.runtimeKeys(content))
228+
System.out.println("Warning: Runtime variables detected. To inject data, use inject() instead");
229+
230+
decodeKeywords();
231+
json = parseJson(content);
232+
content = parse().toString();
233+
}
228234
}

java/Coded-Json/src/main/java/com/codedjson/Json.java

+3
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,13 @@ protected Object getValueFromKey(String key) {
146146
if(json == null) throw new NullPointerException("json object is null");
147147

148148
Object value = json;
149+
if(key.startsWith("<") && key.endsWith(">"))
150+
key = key.substring(1, key.length() - 1);
149151
if(key.contains(".")) {
150152
String[] keyList = key.split("\\.");
151153
for(int j = 0; j < keyList.length; j ++) {
152154
if(keyList[j].equals("$")) continue;
155+
153156
if(keyList[j].contains("[") && keyList[j].contains("]")) {
154157
String eachKey = keyList[j].split("\\[")[0];
155158
int index = Integer.parseInt(keyList[j].split("\\[")[1].split("\\]")[0]);

java/Coded-Json/src/main/java/com/codedjson/utils/Base.java

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class Base {
1919
protected Gson gson = new Gson();
2020
protected String[] commaSeparatedLines;
2121
protected boolean isFilePath;
22+
protected boolean isInjectDone;
23+
protected boolean isInjectExist;
2224

2325
protected List<String> commentedLines;
2426
public Base(String filePath, boolean isFilePath) throws FileNotFoundException {

java/Coded-Json/src/main/java/com/codedjson/utils/Decode.java

+35-58
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Decode extends Json {
2222
private static List<String> stringToIgnore = Arrays.asList( "hash", "serialVersionUID", "serialPersistentFields", "CASE_INSENSITIVE_ORDER", "MIN_VALUE", "MAX_VALUE", "TYPE", "digits", "DigitTens", "DigitOnes", "sizeTable", "SIZE", "BYTES", "serialVersionUID",
2323
"POSITIVE_INFINITY", "NEGATIVE_INFINITY", "NaN", "MIN_NORMAL", "MAX_EXPONENT", "MIN_EXPONENT");
2424
protected List<String> runtimeVals = new ArrayList<>();
25+
private List<String> jpaths = new ArrayList<>();
2526
public Decode(String filePath, boolean isFilePath) throws FileNotFoundException {
2627
super(filePath, isFilePath);
2728
}
@@ -68,31 +69,19 @@ private void decodeSingleLineComment() {
6869
for(String eachLine : commentedLines)
6970
content = content.replace(eachLine, "");
7071
}
71-
private String decodeRelativePaths(String content) {
72-
List<String> jpaths = new ArrayList<>();
73-
72+
private String decodeRelativePaths(String content) throws IllegalJsonType {
7473
Matcher matcher = Keywords.relativeJPathRegex.matcher(content);
7574

76-
while(matcher.find()) {
75+
while (matcher.find()) {
7776
String group = matcher.group();
7877

79-
if(!jpaths.contains(group)) {
78+
if (!jpaths.contains(group)) {
8079
jpaths.add(group);
8180
content = content.replaceAll(Pattern.quote(group), "\"<" + group.replace("$", "\\$") + ">\"");
8281
}
8382
}
8483

85-
for(String eachJPath : jpaths) {
86-
ParsedValue value = parseValue(eachJPath);
87-
88-
while (value.value.toString().contains("$."))
89-
value = parseValue(value.value.toString());
90-
91-
if(value.type.equals("String"))
92-
content = content.replaceAll("\"<" + eachJPath.replace("$", "\\$") + ">\"", Matcher.quoteReplacement("\"" + value.value + "\""));
93-
else
94-
content = content.replaceAll("\"<" + eachJPath.replace("$", "\\$") + ">\"", Matcher.quoteReplacement(value.value.toString()));
95-
}
84+
json = parseJson(content);
9685

9786
return content;
9887
}
@@ -101,6 +90,8 @@ private String decodeRuntimeKeys(String content) {
10190
Matcher matcher = Keywords.runtimeVals.matcher(content);
10291

10392
while(matcher.find()) {
93+
isInjectDone = false;
94+
isInjectExist = true;
10495
String group = matcher.group();
10596

10697
if(!runtimeVals.contains(group)) {
@@ -134,9 +125,9 @@ protected void decodeKeywords() throws AbsolutePathConstraintError, FileNotFound
134125
content = decodeRuntimeKeys(content);
135126
if(! isChanged) break;
136127
}
137-
json = parseJson(content);
138-
139128
content = decodeRelativePaths(content);
129+
130+
json = parseJson(content);
140131
}
141132
protected String replaceContent(String content, HashMap<String, Object> injectingObj) {
142133
for (String key : injectingObj.keySet())
@@ -147,6 +138,8 @@ protected String replaceContent(String content, String key, Object value) {
147138
if (content.contains("\"<-" + key + "->\"")) {
148139
if(value == null)
149140
content = content.replaceAll("\"<-" + key + "->\"", "null");
141+
else if(isContentJson(value.toString()))
142+
content = content.replaceAll("\"<-" + key + "->\"", (String) value);
150143
else if (getType(value).equals("string"))
151144
content = content.replaceAll("<-" + key + "->", Matcher.quoteReplacement((String) value));
152145
else
@@ -234,49 +227,33 @@ else if(object.getClass().getName().toLowerCase().contains("hashmap")) {
234227
return values + "}";
235228
}
236229
}
237-
/*protected void findPathToConstruct(String key, Object value) {
238-
String[] keySets = key.split(Matcher.quoteReplacement(Keywords.relativeJPath))[1].split("\\.");
239-
String pathConstruct = "";
240-
Object valObject = null;
230+
protected String decodeRelativePathValues(String content) {
231+
Matcher matcher = Keywords.encodedRelativeJPathRegex.matcher(content);
232+
233+
while (matcher.find()) {
234+
String eachJPath = matcher.group();
235+
236+
ParsedValue value = parseValue(eachJPath);
241237

242-
for(String eachKey : keySets) {
243-
if(pathConstruct.equals(""))
244-
pathConstruct += eachKey;
238+
while (value.value.toString().contains("$."))
239+
value = parseValue(value.value.toString());
240+
241+
if (value.type.equals("String"))
242+
content = content.replaceAll(eachJPath.replace("$", "\\$"), Matcher.quoteReplacement(value.value.toString()));
245243
else
246-
pathConstruct += "." + eachKey;
247-
try {
248-
Object tempVal = getValueFromKey(pathConstruct);
249-
if(tempVal == null) throw new Exception();
250-
else valObject = tempVal;
251-
}
252-
catch (Exception e) {
253-
String updatedValue;// = valObject.toString() + ",";
244+
content = content.replaceAll("\"" + eachJPath.replace("$", "\\$") + "\"", Matcher.quoteReplacement(value.value.toString()));
245+
}
254246

255-
if(valObject.toString().endsWith("}")) {
256-
updatedValue = valObject.toString().split("}")[0] + ",";
257-
if(value.getClass().getName().toLowerCase().contains("string"))
258-
updatedValue += eachKey + ":\"" + value + "\"";
259-
else
260-
updatedValue += eachKey + ":" + value;
261-
updatedValue += "}";
262-
}
263-
else if(valObject.toString().endsWith("]")) {
264-
updatedValue = valObject.toString().split("]")[0] + ",";
265-
if(value.getClass().getName().toLowerCase().contains("string"))
266-
updatedValue += eachKey + ":\"" + value + "\"";
267-
else
268-
updatedValue += eachKey + ":" + value;
269-
updatedValue += "]";
270-
}
271-
else {
247+
return content;
248+
}
249+
protected boolean isRuntimeKeysExist(String content) {
272250

273-
}
274-
*//*if(value.getClass().getName().toLowerCase().contains("string"))
275-
updatedValue += eachKey + ":\"" + value + "\"";
276-
else
277-
updatedValue += eachKey + ":" + value;*//*
278-
content = content.replace(valObject.toString(), updatedValue);
279-
}
251+
Matcher matcher = Keywords.runtimeVals.matcher(content);
252+
253+
while(matcher.find()) {
254+
isInjectDone = false;
255+
return true;
280256
}
281-
}*/
257+
return false;
258+
}
282259
}

java/Coded-Json/src/main/java/com/codedjson/utils/Keywords.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class Keywords {
88
public static String singleLineComment = "//";
99
public static String relativeJPath = "$.";
1010
public static Pattern relativeJPathRegex = Pattern.compile("[$][.][.A-Za-z0-9]*");
11+
public static Pattern encodedRelativeJPathRegex = Pattern.compile("[<][$.][.A-Za-z0-9]*[>]");
1112
public static Pattern runtimeVals = Pattern.compile("[<][A-Za-z0-9]*[>]");
1213
private static Pattern removeWithPreComma(String key, String value, String content) {
1314
Pattern pattern = Pattern.compile(",+\"" + key.split("\\.")[key.split("\\.").length - 1] + "\":\"?" + value + "\"?");

java/Coded-Json/src/test/com/codedjson/Base.java

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public class Base {
1010
protected Path pureJsonfilePath = Paths.get(testDataDir, "pure.json");
1111
protected Path relativeTargetCjson = Paths.get(testDataDir, "targetRelativeCalls.cjson");
1212
protected Path variableInjectionCjson = Paths.get(testDataDir, "VariableInjection.cjson");
13+
protected Path referInjectedVariable = Paths.get(testDataDir, "referInjectedVariable.cjson");
1314
}

java/Coded-Json/src/test/com/codedjson/CJsonToStringTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
import com.codedjson.exceptions.AbsolutePathConstraintError;
44
import com.codedjson.exceptions.IllegalJsonType;
5+
import com.codedjson.exceptions.UndeserializedCJSON;
56
import com.codedjson.templates.*;
67
import org.junit.jupiter.api.Assertions;
78
import org.junit.jupiter.api.Test;
89

910
import java.io.FileNotFoundException;
10-
import java.util.ArrayList;
1111
import java.util.Arrays;
1212
import java.util.HashMap;
1313
import java.util.List;
1414

1515
public class CJsonToStringTests extends Base {
1616
@Test
17-
public void iShouldBeAbleToConvertJavaObjectToString() throws IllegalAccessException, IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
17+
public void iShouldBeAbleToConvertJavaObjectToString() throws IllegalAccessException, IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException, UndeserializedCJSON {
1818
Target target = new Target();
1919
target.source = new Source();
2020
target.source.pure = new Pure();
@@ -44,7 +44,7 @@ public void iShouldBeAbleToConvertJavaObjectToString() throws IllegalAccessExcep
4444
Assertions.assertEquals(deserializedObj.source.pure.quiz.get("sport").get("q1").answer, "Huston Rocket");
4545
}
4646
@Test
47-
public void iShouldBeAbleToConvertJavaObjectWithNullsToString() throws IllegalAccessException, IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
47+
public void iShouldBeAbleToConvertJavaObjectWithNullsToString() throws IllegalAccessException, IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException, UndeserializedCJSON {
4848
Target target = new Target();
4949
target.source = new Source();
5050
target.source.pure = new Pure();
@@ -64,7 +64,7 @@ public void iShouldBeAbleToConvertJavaObjectWithNullsToString() throws IllegalAc
6464
Assertions.assertNull(deserializedObj.source.pure.quiz.get("q1"));
6565
}
6666
@Test
67-
public void iShouldBeAbleToConvertJavaObjectToStringUsingNullArray() throws IllegalAccessException, IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
67+
public void iShouldBeAbleToConvertJavaObjectToStringUsingNullArray() throws IllegalAccessException, IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException, UndeserializedCJSON {
6868
Target target = new Target();
6969
target.source = new Source();
7070
target.source.pure = new Pure();
@@ -91,7 +91,7 @@ public void iShouldBeAbleToConvertJavaObjectToStringUsingNullArray() throws Ille
9191
Assertions.assertEquals(deserializedObj.source.pure.quiz.get("sport").get("q1").answer, "Huston Rocket");
9292
}
9393
@Test
94-
public void iShouldBeAbleToConvertJavaObjectToStringWithObjectArray() throws IllegalAccessException, IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException {
94+
public void iShouldBeAbleToConvertJavaObjectToStringWithObjectArray() throws IllegalAccessException, IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException, UndeserializedCJSON {
9595
List<TargetObj> targetArray;
9696

9797
TargetObj targetObj = new TargetObj();

0 commit comments

Comments
 (0)