Package com.codedjson
Class CJson<T>
- java.lang.Object
-
- com.codedjson.utils.Base
-
- com.codedjson.utils.Is
-
- com.codedjson.Json
-
- com.codedjson.utils.Decode
-
- com.codedjson.CJson<T>
-
public class CJson<T> extends Decode
Coded JSON is an extended format of JSON formatted data storage, which gives you more previledge to organize data into more structured format. Here is an example for `CJSON` format:{ "source": $import "./source.json", "target": { "fruit": "Apple", "size": "Large", "color": "Red", "secColor": $.target.color, "colorList": [ $.target.color, $.target.secColor ], // You can add comments like this "digitCheck": 1.5, "digitImport": $.target.digitCheck, "digitArrayImport": [ $.target.digitCheck, $.target.digitImport ] } }
The aboveCJSON
snipped will be deserialized in JSON format and can be used as same as other JSON files. For other details, please refer to Official Page
-
-
Field Summary
-
Fields inherited from class com.codedjson.utils.Decode
runtimeVals
-
Fields inherited from class com.codedjson.Json
json, jsonKeys, jsonValues
-
Fields inherited from class com.codedjson.utils.Base
baseFileObj, commaSeparatedLines, commentedLines, content, filePath, gson, isContextConverted, isFilePath, isInjectDone, isInjectExist
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T
deserialize(Class<T> classType)
Deserializes CJSON content and returns Java Object equivalent toclassType
.String
deserializeAsString()
DeserializesCJSON
content and returns as string.
No need to explicitly deserialize CJSON contexts and call this method to get the deserializedString
.T
inject(Class<T> classType, String key, Object value)
Injects single key and value.T
inject(Class<T> classType, HashMap<String,Object> injectingObj)
Inject a hashmap to a json object.T
remove(String key)
Removes a key value set from deserialized JSON.
Takes json path as input and returns updated Serialized class.T
remove(List<String> keyList)
Removes a key value set from deserialized JSON.
Takes list of json paths as input and returns updated Serialized class.static String
toString(Object object)
Converts JAVA object to JSON string.-
Methods inherited from class com.codedjson.utils.Decode
decodeKeywords, decodeRelativePathValues, getAsString, isRuntimeKeysExist, removeWithKey, replaceContent, replaceContent
-
Methods inherited from class com.codedjson.Json
getAllKeys, getAllValues, getValueFromKey, isContentJson, isContentJson, isContentJson, parse, parse, parseValue
-
Methods inherited from class com.codedjson.utils.Is
isImport, isRelativeJPath, isSingleLineComent
-
Methods inherited from class com.codedjson.utils.Base
appendPath, getDirectory, getFullPath, getType, isAbsolutePath, parseJson, read
-
-
-
-
Constructor Detail
-
CJson
public CJson(Path filePath) throws FileNotFoundException, IllegalJsonType, AbsolutePathConstraintError
CJson parser using file path.- Parameters:
filePath
-- Throws:
IllegalJsonType
AbsolutePathConstraintError
FileNotFoundException
-
CJson
public CJson(String content) throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException
Parser forCJSON
content. You can directly parse aCJSON
string content.
Import statements must have paths absolute. Otherwise it throws absolute path constraint error while deserialization- Parameters:
content
- CJSON/JSON content in string- Throws:
IllegalJsonType
AbsolutePathConstraintError
FileNotFoundException
-
-
Method Detail
-
deserialize
public T deserialize(Class<T> classType) throws IllegalJsonType, VariableInjectionException
Deserializes CJSON content and returns Java Object equivalent toclassType
. For more capabilities, refer to Official Page- Parameters:
classType
- Java class object equivalent to target JSON- Returns:
- Java Object equivalent to
classType
- Throws:
IllegalJsonType
VariableInjectionException
-
inject
public T inject(Class<T> classType, HashMap<String,Object> injectingObj) throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException, VariableInjectionException
Inject a hashmap to a json object. Uses tag<variable>
JSONArrays and JSONObjects cannot be injected
For more details on usage, click here- Parameters:
classType
- Java class object equivalent to target JSONinjectingObj
-- Returns:
- Throws:
IllegalJsonType
- If syntax is not correctAbsolutePathConstraintError
- This is throws if CJson object is created with CJSON string. This is thrown if import statements contain relative path instead of absolute pathFileNotFoundException
- If the imported file is not found in the directoryVariableInjectionException
-
inject
public T inject(Class<T> classType, String key, Object value) throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException, VariableInjectionException
Injects single key and value. Uses tag<variable>
JSONArrays and JSONObjects cannot be injected
For more details on usage, click here- Parameters:
classType
- generic class typekey
- Key to be replaced in string format. Please note, this is not JPath. For more details, view this pagevalue
- Value to be replaced- Returns:
- Throws:
IllegalJsonType
- If syntax is not correctAbsolutePathConstraintError
- This is throws if CJson object is created with CJSON string. This is thrown if import statements contain relative path instead of absolute pathFileNotFoundException
- If the imported file is not found in the directoryVariableInjectionException
-
deserializeAsString
public String deserializeAsString() throws IllegalJsonType, AbsolutePathConstraintError, FileNotFoundException
DeserializesCJSON
content and returns as string.
No need to explicitly deserialize CJSON contexts and call this method to get the deserializedString
. This method wraps all the steps.- Returns:
- String value of parsed
cjson
- Throws:
IllegalJsonType
- If syntax is not correctAbsolutePathConstraintError
- This is throws if CJson object is created with CJSON string. This is thrown if import statements contain relative path instead of absolute pathFileNotFoundException
- If the imported file is not found in the directory
-
remove
public T remove(String key) throws IllegalJsonType, UndeserializedCJSON, InvalidJPathError
Removes a key value set from deserialized JSON.
Takes json path as input and returns updated Serialized class.- Parameters:
key
- JPath starting with "$."- Returns:
- Updated Class<T>
- Throws:
IllegalJsonType
- Throws if any of the path is incorrectUndeserializedCJSON
- Throws if the CJSON/JSON is not deserialized. Call deserialize before removeInvalidJPathError
- If the JPath format is invalid
-
remove
public T remove(List<String> keyList) throws IllegalJsonType, UndeserializedCJSON
Removes a key value set from deserialized JSON.
Takes list of json paths as input and returns updated Serialized class.- Parameters:
keyList
- JPath list starting with "$."- Returns:
- Updated Class<T>
- Throws:
IllegalJsonType
- Throws if any of the path is incorrectUndeserializedCJSON
- Throws if the CJSON/JSON is not deserialized. Call deserialize before remove
-
toString
public static String toString(Object object) throws IllegalAccessException
Converts JAVA object to JSON string.
If null value is passed, it returns empty JSON - {}- Parameters:
object
- Any JAVA object- Returns:
- Throws:
IllegalAccessException
-
-