@@ -51,47 +51,48 @@ public class CJson<T> extends Decode {
51
51
/**
52
52
* CJson parser using file path.
53
53
* @param filePath
54
- * @throws Exception FileNotFoundException
54
+ * @throws IllegalJsonType
55
+ * @throws AbsolutePathConstraintError
56
+ * @throws FileNotFoundException
55
57
*/
56
- public CJson (Path filePath ) throws FileNotFoundException {
58
+ public CJson (Path filePath ) throws FileNotFoundException , IllegalJsonType , AbsolutePathConstraintError {
57
59
super (filePath .toString (), true );
58
60
this .t = null ;
59
61
this .filePath = filePath .toString ();
60
62
this .baseFileObj = new File (this .filePath );
63
+ contextConverter ();
61
64
}
62
65
/**
63
66
* Parser for <code>CJSON</code> content.
64
67
* You can directly parse a <code>CJSON</code> string content.<br/>
65
68
* <b>Import statements must have paths absolute. Otherwise it throws absolute path constraint error while deserialization</b>
66
69
* @param content CJSON/JSON content in string
70
+ * @throws IllegalJsonType
71
+ * @throws AbsolutePathConstraintError
72
+ * @throws FileNotFoundException
67
73
*/
68
- public CJson (String content ) {
74
+ public CJson (String content ) throws IllegalJsonType , AbsolutePathConstraintError , FileNotFoundException {
69
75
super (content );
70
76
this .t = null ;
71
77
this .filePath = null ;
72
78
this .baseFileObj = null ;
79
+ contextConverter ();
73
80
}
74
81
75
82
/**
76
83
* Deserializes CJSON content and returns Java Object equivalent to <code>classType</code>.
77
84
* For more cababilitites, refer to <a href="https://subhendushekhar.github.io/cjson/">Official Page</a>
78
85
* @param classType Java class object equivalent to target JSON
79
86
* @return Java Object equivalent to <code>classType</code>
80
- * @throws IllegalJsonType
81
- * @throws AbsolutePathConstraintError
82
- * @throws FileNotFoundException
83
87
*/
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." );
91
91
92
+ content = decodeRelativePathValues (content );
92
93
json = parseJson (content );
93
94
94
- content = parse (). toString () ;
95
+ this . classType = classType ;
95
96
if (classType .equals (String .class ))
96
97
t = (T ) content ;
97
98
else
@@ -111,22 +112,22 @@ public T deserialize(Class<T> classType) throws IllegalJsonType, AbsolutePathCon
111
112
* This is thrown if import statements contain relative path instead of absolute path
112
113
* @throws FileNotFoundException If the imported file is not found in the directory
113
114
*/
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
+
115
117
if (injectingObj != null || injectingObj .keySet ().size () != 0 ) {
116
118
this .classType = classType ;
117
119
118
- decodeKeywords ();
120
+ contextConverter ();
119
121
content = replaceContent (content , injectingObj );
120
122
121
123
json = parseJson (content );
122
124
content = parse ().toString ();
123
- }
124
125
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 ) ;
130
131
}
131
132
/**
132
133
* Injects single key and value. Uses tag <code><variable></code><br/>
@@ -142,19 +143,16 @@ public T inject(Class<T> classType, HashMap<String, Object> injectingObj) throws
142
143
* This is thrown if import statements contain relative path instead of absolute path
143
144
* @throws FileNotFoundException If the imported file is not found in the directory
144
145
*/
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 {
146
147
this .classType = classType ;
147
148
148
- decodeKeywords ();
149
+ contextConverter ();
149
150
content = replaceContent (content , key , value );
150
151
json = parseJson (content );
151
152
content = parse ().toString ();
152
153
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 );
158
156
}
159
157
/**
160
158
* 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
167
165
* @throws FileNotFoundException If the imported file is not found in the directory
168
166
*/
169
167
public String deserializeAsString () throws IllegalJsonType , AbsolutePathConstraintError , FileNotFoundException {
170
- decodeKeywords ();
168
+ contextConverter ();
171
169
json = parseJson (content );
172
170
return content ;
173
171
}
@@ -182,7 +180,7 @@ public String deserializeAsString() throws IllegalJsonType, AbsolutePathConstrai
182
180
*/
183
181
public T remove (String key ) throws IllegalJsonType , UndeserializedCJSON , InvalidJPathError {
184
182
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()" );
186
184
187
185
removeWithKey (key );
188
186
@@ -202,7 +200,7 @@ public T remove(String key) throws IllegalJsonType, UndeserializedCJSON, Invalid
202
200
* @throws UndeserializedCJSON Throws if the CJSON/JSON is not deserialized. Call deserialize before remove
203
201
*/
204
202
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()" );
206
204
for (String key : keyList )
207
205
removeWithKey (key );
208
206
@@ -225,4 +223,12 @@ public static String toString(Object object) throws IllegalAccessException {
225
223
return "{}" ;
226
224
return getAsString (object );
227
225
}
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
+ }
228
234
}
0 commit comments