Skip to content

Commit

Permalink
Implementing flag to muteException and return a null object if the JS…
Browse files Browse the repository at this point in the history
…ON is malformated.
  • Loading branch information
Mariana Azevedo committed Apr 12, 2019
1 parent 6019e00 commit 477c8ae
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ You must import .jar into the classpath of your project. If your project is a ma
<dependency>
<groupId>io.github.mariazevedo88</groupId>
<artifactId>json-formatter-validator</artifactId>
<version>1.1.9</version>
<version>2.0.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<connection>scm:git:git://github.com/mariazevedo88/json-formatter-validator.git</connection>
<developerConnection>scm:git:git@github.com:mariazevedo88/json-formatter-validator.git</developerConnection>
<url>https://github.com/mariazevedo88/json-formatter-validator</url>
<tag>json-formatter-validator-1.1.9</tag>
<tag>json-formatter-validator-2.0.0</tag>
</scm>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void main(String[] args) throws IOException{
CustomJSONFormatter formatter = new CustomJSONFormatter();

for(String arg : args) {
json = formatter.checkValidityAndFormatObject(arg, false);
json = formatter.checkValidityAndFormatObject(arg, false, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

import io.github.mariazevedo88.jfv.enumeration.DelimitersEnum;

Expand Down Expand Up @@ -71,13 +72,22 @@ private boolean isValidJson(Object json, boolean muteLog){
* @since 10/02/2019
*
* @param json
* @param muteException
*/
private void parseJSONObject(Object json) {
private void parseJSONObject(Object json, boolean muteException) {

JsonElement res = null;

if(json instanceof String){
res = new JsonParser().parse((String)json);
try {
res = new JsonParser().parse((String)json);
}catch(JsonSyntaxException e) {
if(!muteException) {
throw new JsonSyntaxException("Error: JSON with more invalid characters than commas and quotes on keys and values.");
}else {
this.validJson = null;
}
}
}

if(json instanceof BufferedReader){
Expand Down Expand Up @@ -367,7 +377,7 @@ private static void replaceStringBasedOnAPattern(StringBuilder builderModified,
* @return
* @throws IOException
*/
public JsonObject checkValidityAndFormatObject(Object json, boolean muteLog) throws IOException {
public JsonObject checkValidityAndFormatObject(Object json, boolean muteLog, boolean muteException) throws IOException {

String jsonToTest = null;
BufferedReader reader = null;
Expand All @@ -384,15 +394,18 @@ public JsonObject checkValidityAndFormatObject(Object json, boolean muteLog) thr
if(!isValidJson(json, muteLog)) {
jsonToTest = getInvalidJsonToFormat(json.toString());
if(reader == null){
parseJSONObject(jsonToTest);
parseJSONObject(jsonToTest, muteException);
}else{
parseJSONObject(reader);
parseJSONObject(reader, muteException);
reader.close();
}
}

if(!muteLog) {
if(!muteLog && this.validJson != null) {
logger.info("Valid json: " + this.validJson);
}else {
if(muteException)
logger.warn("JsonParseException: JSON with more invalid characters than commas and quotes on keys and values.");
}

return validJson;
Expand Down

0 comments on commit 477c8ae

Please sign in to comment.