diff --git a/pom.xml b/pom.xml
index 906322d..e2fbb92 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
4.0.0
io.github.mariazevedo88
json-formatter-validator
- 1.1.10-SNAPSHOT
+ 2.0.0-SNAPSHOT
json-formatter-validator
Java tool for json validation via string or file and correction of invalid json-like string
@@ -51,7 +51,7 @@
scm:git:git://github.com/mariazevedo88/json-formatter-validator.git
scm:git:git@github.com:mariazevedo88/json-formatter-validator.git
https://github.com/mariazevedo88/json-formatter-validator
- json-formatter-validator-1.1.8
+ json-formatter-validator-1.1.9
diff --git a/src/main/java/io/github/mariazevedo88/jfv/JsonFormatterValidatorApplication.java b/src/main/java/io/github/mariazevedo88/jfv/JsonFormatterValidatorApplication.java
index 66fae8c..bb997e0 100644
--- a/src/main/java/io/github/mariazevedo88/jfv/JsonFormatterValidatorApplication.java
+++ b/src/main/java/io/github/mariazevedo88/jfv/JsonFormatterValidatorApplication.java
@@ -27,7 +27,7 @@ public static void main(String[] args) throws IOException{
CustomJSONFormatter formatter = new CustomJSONFormatter();
for(String arg : args) {
- json = formatter.checkValidityAndFormatObject(arg);
+ json = formatter.checkValidityAndFormatObject(arg, false);
}
}
diff --git a/src/main/java/io/github/mariazevedo88/jfv/enumeration/DelimitersEnum.java b/src/main/java/io/github/mariazevedo88/jfv/enumeration/DelimitersEnum.java
index 9fe779d..df9a812 100644
--- a/src/main/java/io/github/mariazevedo88/jfv/enumeration/DelimitersEnum.java
+++ b/src/main/java/io/github/mariazevedo88/jfv/enumeration/DelimitersEnum.java
@@ -11,7 +11,10 @@ public enum DelimitersEnum {
RIGHT_KEY_WITH_ESCAPE("\"}"),
SEMICOLON(";"),
DOUBLE_SEMICOLON(";;"),
- QUOTES("''");
+ QUOTES("''"),
+ LEFT_KEY("{"),
+ LEFT_BRACKETS("["),
+ RIGHT_BRACKETS("]");
private String value;
diff --git a/src/main/java/io/github/mariazevedo88/jfv/formatter/CustomJSONFormatter.java b/src/main/java/io/github/mariazevedo88/jfv/formatter/CustomJSONFormatter.java
index 94b6cfb..e1a87b4 100644
--- a/src/main/java/io/github/mariazevedo88/jfv/formatter/CustomJSONFormatter.java
+++ b/src/main/java/io/github/mariazevedo88/jfv/formatter/CustomJSONFormatter.java
@@ -17,8 +17,6 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
-import com.google.gson.JsonSyntaxException;
-import com.google.gson.stream.MalformedJsonException;
import io.github.mariazevedo88.jfv.enumeration.DelimitersEnum;
@@ -35,14 +33,16 @@ public class CustomJSONFormatter {
private JsonObject validJson;
/**
- * Method that verify in a object is a valid or invalid JSON.
+ * Method that verify in a object is a valid or invalid JSON
*
* @author Mariana Azevedo
* @since 10/02/2019
+ *
* @param json
+ * @param muteLog
* @return
*/
- private boolean isValidJson(Object json){
+ private boolean isValidJson(Object json, boolean muteLog){
if(json instanceof BufferedReader){
JsonElement res = new JsonParser().parse((BufferedReader)json);
@@ -57,15 +57,19 @@ private boolean isValidJson(Object json){
return true;
}
- logger.info("Invalid json: " + json.toString());
+ if(!muteLog) {
+ logger.info("Invalid json: " + json.toString());
+ }
+
return false;
}
/**
- * Method that parses a JSON object.
+ * Method that parses a JSON object
*
* @author Mariana Azevedo
* @since 10/02/2019
+ *
* @param json
*/
private void parseJSONObject(Object json) {
@@ -97,9 +101,9 @@ private void parseJSONObject(Object json) {
*
* @author Mariana Azevedo
* @since 10/02/2019
+ *
* @param invalidJson
* @return
- * @throws MalformedJsonException
*/
private static String getInvalidJsonToFormat(String invalidJson) {
@@ -122,6 +126,7 @@ private static String getInvalidJsonToFormat(String invalidJson) {
*
* @author Mariana Azevedo
* @since 28/02/2019
+ *
* @param invalidJson
* @return
*/
@@ -134,16 +139,19 @@ private static String fixFieldsWithSimpleQuotes(String invalidJson) {
*
* @author Mariana Azevedo
* @since 02/04/2019
+ *
* @param invalidJson
* @return
*/
private static String fixMalformatedFields(String invalidJson) {
+
invalidJson = invalidJson.replaceAll("\\s+,,", ","); //correcting double commas with space
invalidJson = invalidJson.replaceAll("(\\d+)\\,(\\d+)", "$1.$2"); //correcting decimal numbers with comma
invalidJson = invalidJson.replaceAll("(\\d+)\\:(\\d+)", "$1;;$2"); //correcting hours in the HH:mm format
invalidJson = invalidJson.replaceAll("(\\()", ";"); //correcting left parentheses wrongly placed
invalidJson = invalidJson.replaceAll("(\\))", ";"); //correcting right parentheses wrongly placed
invalidJson = invalidJson.replaceAll("[A-Z]+:", ""); //removing colon wrongly placed
+
return invalidJson;
}
@@ -152,25 +160,31 @@ private static String fixMalformatedFields(String invalidJson) {
*
* @author Mariana Azevedo
* @since 28/02/2019
+ *
* @param invalidJson
* @return
*/
private static String fixEmptyFields(String invalidJson) {
+
invalidJson = invalidJson.replaceAll("(:,)", ":\'\',");
invalidJson = invalidJson.replaceAll("(:})", ": \'\'}");
invalidJson = invalidJson.replaceAll("(,,)", "\'\',");
+ invalidJson = invalidJson.replaceAll("(,})", "}");
+
return invalidJson;
}
/**
- * Method that replaces some control delimiters in the fix routine on fields with wrong commas.
+ * Method that replaces some control delimiters in the fix routine on fields with wrong commas
*
* @author Mariana Azevedo
* @since 17/02/2019
+ *
* @param builderModified
* @return
*/
private static String replaceControlDelimiters(StringBuilder builderModified) {
+
String finalString = builderModified.toString().replaceAll(DelimitersEnum.DOUBLE_SEMICOLON.getValue(), DelimitersEnum.COLON.getValue());
finalString = finalString.replaceAll(DelimitersEnum.SEMICOLON.getValue(), DelimitersEnum.COMMA.getValue());
@@ -178,13 +192,13 @@ private static String replaceControlDelimiters(StringBuilder builderModified) {
}
/**
- * Method that fix invalid fields wrongly converted by the regex of getInvalidJsonToFormat() method.
+ * Method that fix invalid fields wrongly converted by the regex of getInvalidJsonToFormat() method
*
* @author Mariana Azevedo
* @since 17/02/2019
+ *
* @param builderModified
* @return
- * @throws MalformedJsonException
*/
private static StringBuilder fixFieldsWithCommasWronglyModified(StringBuilder builderModified){
@@ -205,10 +219,6 @@ private static StringBuilder fixFieldsWithCommasWronglyModified(StringBuilder bu
}
}
- if(!builderModified.toString().contains(DelimitersEnum.COMMA.getValue())){
- throw new JsonSyntaxException("Error: JSON doesn't have fields separated by commas.");
- }
-
return builderModified;
}
@@ -217,6 +227,7 @@ private static StringBuilder fixFieldsWithCommasWronglyModified(StringBuilder bu
*
* @author Mariana Azevedo
* @since 17/02/2019
+ *
* @param invalidJsonValues
* @return
*/
@@ -230,6 +241,7 @@ private static boolean isStringHasInvalidJsonValues(String [] invalidJsonValues)
*
* @author Mariana Azevedo
* @since 17/02/2019
+ *
* @param invalidJsonValues
* @param builder
* @return
@@ -261,6 +273,7 @@ private static StringBuilder cleanInvalidJsonValues(String[] invalidJsonValues,
*
* @author Mariana Azevedo
* @since 17/02/2019
+ *
* @param builderModified
* @param previousField
* @param str
@@ -329,6 +342,7 @@ private static void splitPatternToNearowTheSearch(StringBuilder builderModified,
*
* @author Mariana Azevedo
* @since 17/02/2019
+ *
* @param builderModified
* @param pattern
* @param replacement
@@ -347,11 +361,13 @@ private static void replaceStringBasedOnAPattern(StringBuilder builderModified,
*
* @author Mariana Azevedo
* @since 17/02/2019
+ *
* @param json
+ * @param muteLog
* @return
* @throws IOException
*/
- public JsonObject checkValidityAndFormatObject(Object json) throws IOException {
+ public JsonObject checkValidityAndFormatObject(Object json, boolean muteLog) throws IOException {
String jsonToTest = null;
BufferedReader reader = null;
@@ -365,7 +381,7 @@ public JsonObject checkValidityAndFormatObject(Object json) throws IOException {
throw new NullPointerException("Object to validated is null.");
}
- if(!isValidJson(json)) {
+ if(!isValidJson(json, muteLog)) {
jsonToTest = getInvalidJsonToFormat(json.toString());
if(reader == null){
parseJSONObject(jsonToTest);
@@ -375,16 +391,182 @@ public JsonObject checkValidityAndFormatObject(Object json) throws IOException {
}
}
- logger.info("Valid json: " + this.validJson);
+ if(!muteLog) {
+ logger.info("Valid json: " + this.validJson);
+ }
return validJson;
}
+ /**
+ * Method that remove a json object/json array pattern from the string
+ *
+ * @author Mariana Azevedo
+ * @since 12/04/2019
+ *
+ * @param invalidJson
+ * @param jsonObjectPattern
+ * @return
+ */
+ private String removeJSONObjectFromString(String invalidJson, String jsonObjectPattern) {
+
+ StringBuilder builderModified = new StringBuilder(invalidJson);
+ int lastIndexOf = builderModified.indexOf(jsonObjectPattern) + jsonObjectPattern.length();
+
+ int numberLeftKeys = 0;
+ int numberRightKeys = 0;
+ int numberLeftBrackets = 0;
+ int numberRightBrackets = 0;
+
+ for (int i = lastIndexOf; i < builderModified.length(); i++) {
+ String next = builderModified.substring(i,i+1);
+ if(next.equals(DelimitersEnum.LEFT_KEY.getValue())) numberLeftKeys++;
+
+ if(next.equals(DelimitersEnum.RIGHT_KEY.getValue())) numberRightKeys++;
+
+ if(next.equals(DelimitersEnum.LEFT_BRACKETS.getValue())) numberLeftBrackets++;
+
+ if(next.equals(DelimitersEnum.RIGHT_BRACKETS.getValue())) numberRightBrackets++;
+
+ if((next.equals(DelimitersEnum.COMMA.getValue()) && hasEqualNumberOfKeysOrBrackets(numberLeftKeys, numberRightKeys) && hasEqualNumberOfKeysOrBrackets(numberLeftBrackets, numberRightBrackets))
+ || (next.equals(DelimitersEnum.RIGHT_KEY.getValue()) && hasMoreRightKeys(numberLeftKeys, numberRightKeys))) {
+ if (next.equals(DelimitersEnum.COMMA.getValue())) jsonObjectPattern = jsonObjectPattern.concat(next);
+ break;
+ }
+
+ jsonObjectPattern = jsonObjectPattern.concat(next);
+
+ }
+
+ return builderModified.toString().replace(jsonObjectPattern, "");
+ }
+
+ /**
+ * Method that filter a json object/json array pattern from the string
+ *
+ * @author Mariana Azevedo
+ * @since 12/04/2019
+ *
+ * @param invalidJson
+ * @param jsonObjectPattern
+ * @return
+ */
+ private String filterJSONObjectFromString(String invalidJson, String jsonObjectPattern) {
+
+ StringBuilder builderModified = new StringBuilder(invalidJson);
+ int lastIndexOf = builderModified.indexOf(jsonObjectPattern) + jsonObjectPattern.length();
+
+ int numberLeftKeys = 0;
+ int numberRightKeys = 0;
+ int numberLeftBrackets = 0;
+ int numberRightBrackets = 0;
+
+ for (int i = lastIndexOf; i < builderModified.length(); i++) {
+
+ String next = builderModified.substring(i,i+1);
+
+ if(next.equals(DelimitersEnum.LEFT_KEY.getValue())) numberLeftKeys++;
+
+ if(next.equals(DelimitersEnum.RIGHT_KEY.getValue())) numberRightKeys++;
+
+ if(next.equals(DelimitersEnum.LEFT_BRACKETS.getValue())) numberLeftBrackets++;
+
+ if(next.equals(DelimitersEnum.RIGHT_BRACKETS.getValue())) numberRightBrackets++;
+
+ if((next.equals(DelimitersEnum.COMMA.getValue()) && hasEqualNumberOfKeysOrBrackets(numberLeftKeys, numberRightKeys) && hasEqualNumberOfKeysOrBrackets(numberLeftBrackets, numberRightBrackets))
+ || (next.equals(DelimitersEnum.RIGHT_KEY.getValue()) && hasMoreRightKeys(numberLeftKeys, numberRightKeys))) {
+ if (next.equals(DelimitersEnum.COMMA.getValue())) jsonObjectPattern = jsonObjectPattern.concat(next);
+ break;
+ }
+
+ jsonObjectPattern = jsonObjectPattern.concat(next);
+
+ }
+
+ return new StringBuilder(jsonObjectPattern).toString();
+ }
+
+ /**
+ * Method that remove a list of json object/json array patterns from the string
+ *
+ * @author Mariana Azevedo
+ * @since 12/04/2019
+ *
+ * @param invalidJson
+ * @param jsonObjectPattern
+ * @return
+ */
+ public String removeJSONObjectsFromString(String invalidJson, String[] jsonObjectPattern) {
+
+ String jsonModified = invalidJson;
+
+ for(String attribute : jsonObjectPattern) {
+ jsonModified = removeJSONObjectFromString(jsonModified, attribute);
+ }
+
+ return jsonModified;
+ }
+
+ /**
+ * Method that filter a list of json object/json array pattern from the string
+ *
+ * @author Mariana Azevedo
+ * @since 12/04/2019
+ *
+ * @param invalidJson
+ * @param jsonObjectPattern
+ * @return
+ */
+ public String filterJSONObjectsFromString(String invalidJson, String[] jsonObjectPattern) {
+
+ String jsonModified = "";
+
+ for(String attribute : jsonObjectPattern) {
+ String filterResult = filterJSONObjectFromString(invalidJson, attribute);
+ jsonModified = jsonModified.concat(filterResult).concat(DelimitersEnum.COMMA.getValue());
+ }
+
+ if(!jsonModified.startsWith(DelimitersEnum.LEFT_KEY.getValue())) jsonModified = DelimitersEnum.LEFT_KEY.getValue().concat(jsonModified);
+ if(!jsonModified.endsWith(DelimitersEnum.RIGHT_KEY.getValue())) jsonModified = jsonModified.concat(DelimitersEnum.RIGHT_KEY.getValue());
+
+ return jsonModified;
+ }
+
+ /**
+ * Method that verifies with string has more right keys
+ *
+ * @author Mariana Azevedo
+ * @since 12/04/2019
+ *
+ * @param numberLeftKeys
+ * @param numberRightKeys
+ * @return
+ */
+ private boolean hasMoreRightKeys(int numberLeftKeys, int numberRightKeys) {
+ return numberLeftKeys < numberRightKeys;
+ }
+
+ /**
+ * Method that verifies with string has an equal number of left and right keys
+ * or left and right brackets
+ *
+ * @author Mariana Azevedo
+ * @since 12/04/2019
+ *
+ * @param numberLeft
+ * @param numberRight
+ * @return
+ */
+ private boolean hasEqualNumberOfKeysOrBrackets(int numberLeft, int numberRight) {
+ return numberLeft == numberRight;
+ }
+
/**
* Method that return a valid JSON
*
* @author Mariana Azevedo
* @since 10/02/2019
+ *
* @return
*/
public JsonObject getValidJson() {
diff --git a/src/test/java/io/github/mariazevedo88/jfv/test/CustomJSONFormatterTest.java b/src/test/java/io/github/mariazevedo88/jfv/test/CustomJSONFormatterTest.java
index bced121..604c4fc 100644
--- a/src/test/java/io/github/mariazevedo88/jfv/test/CustomJSONFormatterTest.java
+++ b/src/test/java/io/github/mariazevedo88/jfv/test/CustomJSONFormatterTest.java
@@ -10,6 +10,7 @@
import java.io.FileReader;
import java.io.IOException;
+import org.apache.log4j.Logger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
@@ -23,7 +24,6 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
-import com.google.gson.JsonSyntaxException;
import io.github.mariazevedo88.jfv.JsonFormatterValidatorApplication;
import io.github.mariazevedo88.jfv.formatter.CustomJSONFormatter;
@@ -40,6 +40,7 @@
@TestMethodOrder(OrderAnnotation.class)
public class CustomJSONFormatterTest{
+ private static final Logger logger = Logger.getLogger(CustomJSONFormatterTest.class.getName());
private CustomJSONFormatter formatter;
@BeforeAll
@@ -61,7 +62,7 @@ public void verifyNullParams() throws IOException {
@Order(2)
public void verifyANullObject() throws IOException {
assertThrows(NullPointerException.class,()->{
- formatter.checkValidityAndFormatObject(null);
+ formatter.checkValidityAndFormatObject(null, false);
});
}
@@ -81,7 +82,7 @@ public void getJSONFromStringArgsExecution() throws IOException {
@Order(4)
public void getJSONFromString() throws IOException {
String jsonFromString = "{id:267107086801,productCode:02-671070868,lastUpdate:2018-07-15,payment:[{sequential:1,id:CREDIT_CARD,value:188,installments:9}]}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonFromString);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonFromString, false);
assertTrue(json.isJsonObject());
}
@@ -98,7 +99,7 @@ public void getValidJson() throws IOException {
@Order(6)
public void getJSONWithEmptyFields() throws IOException {
String jsonWithEmptyFields = "{id:267111784501,productCode:02-671117845,purchaseDate:2018-07-15,status:APPROVED,estimatedDeliveryDate:2018-09-26,deliveryAddress:{street:Rua Wanderlin Vieira,number:216,reference:,neighborhood:Cachoeira,city:Conselheiro Lafaiete,state:MG,zipcode:36408106,additionalInfo:},paymentMethods:[{sequential:1,id:CREDIT_CARD,value:1216.03,installments:10}]}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithEmptyFields);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithEmptyFields, false);
assertTrue(json.isJsonObject());
}
@@ -107,7 +108,7 @@ public void getJSONWithEmptyFields() throws IOException {
@Order(7)
public void getJSONWithCommasOnFieldsWithEqualValues() throws IOException {
String jsonWithCommasOnAddressField = "{id:267590641902,productCode:02-675906419,purchaseDate:2018-09-17,status:NEW,estimatedDeliveryDate:2018-12-03,deliveryAddress:{street:Rua Baru00e3o do Flamengo,number:35,additionalInfo:311,reference:Entregar na entrada de serviu00e7o, na parte de tru00e1s do pru00e9dio, na rua Senador Vergueiro, num 5.,neighborhood:Flamengo,city:RIO DE JANEIRO,state:RJ,zipcode:22220080},telephones:{main:{ddd:21,number:00026310},secondary:{ddd:21,number:00015462},business:{ddd:21,number:632154789}},billingAddress:{street:Rua Baru00e3o do Flamengo,number:500,additionalInfo:311,reference:Entregar na entrada de serviu00e7o, na parte de tru00e1s do pru00e9dio, na rua Senador Vergueiro, num 5.,neighborhood:Flamengo,city:RIO DE JANEIRO,state:RJ,zipcode:22220080},telephones:{main:{ddd:21,number:00026310},secondary:{ddd:21,number:981405949},business:{ddd:21,number:981405949}},paymentMethods:[{sequential:1,id:VOUCHER,value:70.53,installments:1,idAutorization:null,cardIssuer:null},{sequential:2,id:VOUCHER,value:40.62,installments:1,idAutorization:null,cardIssuer:null}]}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithCommasOnAddressField);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithCommasOnAddressField, false);
assertTrue(json.isJsonObject());
}
@@ -116,7 +117,7 @@ public void getJSONWithCommasOnFieldsWithEqualValues() throws IOException {
@Order(8)
public void getJSONWithCommasOnFieldsWithDifferentValues() throws IOException {
String jsonWithCommasOnFieldName = "{id:267034342303,productCode:02-670343423,purchaseDate:2018-07-02,customer:{name:Juliano, Thais Ou Lourdes,deliveryAddress:{street:Rua Landel de Moura,number:1212,additionalInfo:CASA,reference:PRu00d3XIMO DA AV. WENCESLAU ESCOBAR.,neighborhood:Tristeza,city:Porto Alegre,state:RS,zipcode:91920150}},billingAddress:{street:Avenida Alberto Bins,number:9687,additionalInfo:conj. 23651,reference:em frente ao sesc,neighborhood:Centro Histu00f3rico,city:Porto Alegre,state:RS,zipcode:90030140},telephones:{main:{ddd:51,number:00032146},secondary:{ddd:51,number:025412333},business:{ddd:51,number:003214541}},totalAmount:578.79,totalFreight:58.99,totalDiscount:0,totalInterest:0,quantity:2,price:259.9,freight:58.99,discount:0,paymentMethods:[{sequential:1,id:CREDIT_CARD,value:578.79,installments:10}]}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithCommasOnFieldName);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithCommasOnFieldName, false);
assertTrue(json.isJsonObject());
}
@@ -125,7 +126,7 @@ public void getJSONWithCommasOnFieldsWithDifferentValues() throws IOException {
@Order(9)
public void getJSONWithDoubleComma() throws IOException {
String jsonWithDoubleComma = "{id:267133121501,productCode:02-671331215,purchaseDate:2018-07-18,estimatedDeliveryDate:2018-09-17,deliveryAddress:{street:Rua Au00e7au00ed,,number:451,additionalInfo:Frente u00e0 Av. Sucupira,,reference:Garagem pequena,,neighborhood:Morada do Sol,city:Presidente Figueiredo,state:AM,zipcode:69735000},totalAmount:169.88,totalFreight:14.99,totalDiscount:0,totalInterest:0,paymentMethods:[{sequential:1,id:CREDIT_CARD,value:169.88,installments:5}]}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDoubleComma);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDoubleComma, false);
assertTrue(json.isJsonObject());
}
@@ -134,7 +135,7 @@ public void getJSONWithDoubleComma() throws IOException {
@Order(10)
public void getJSONWithNumbersAfterComma() throws IOException {
String jsonWithNumbersAfterComma = "{id:267180636401,productCode:02-671806364,purchaseDate:2018-07-26,lastUpdate:2018-07-26,purchaseTimestamp:2018-07-26 18:00:31,lastUpdateTimestamp:2018-07-26 18:09:22,status:NEW,estimatedDeliveryDate:2018-10-31,deliveryAddress:{street:Av. Eugu00eanio Krause, 3034/02,number:3034,reference:zazzazaaa,neighborhood:Armau00e7u00e3o,city:Penha,state:SC,zipcode:88385000},telephones:{main:{ddd:47,number:02020312},secondary:{ddd:47,number:085246321},business:{ddd:47,number:065234187}},billingAddress:{street:Av. Eugu00eanio Krause, 3034/02,number:3034,reference:zazadazaa,neighborhood:Armau00e7u00e3o,city:Penha,state:SC,zipcode:88385000},telephones:{main:{ddd:47,number:001321456},secondary:{ddd:47,number:465413100},business:{ddd:47,number:789798745}},totalAmount:463.89,totalFreight:63.99,totalDiscount:0,totalInterest:0,quantity:1,price:399.9,freight:63.99,discount:0,warehouse:98,paymentMethods:[{sequential:1,id:CREDIT_CARD,value:463.89,installments:10}]}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithNumbersAfterComma);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithNumbersAfterComma, false);
assertTrue(json.isJsonObject());
}
@@ -143,7 +144,7 @@ public void getJSONWithNumbersAfterComma() throws IOException {
@Order(11)
public void getJSONWithDotBeforeComma() throws IOException {
String jsonWithDotBeforeComma = "{id:106946382801,productCode:01-69463828,purchaseDate:2018-07-22,lastUpdate:2018-07-22,deliveryAddress:{street:Rua Pastor Blablabla,number:666,additionalInfo:Apto 14, bloco B, pru00e9dio vermelho,reference:Pru00f3ximo ao shopping aricanduva,neighborhood:Jardim Imperador (Zona Leste),city:Sao Paulo,state:SP,zipcode:00000000},billingAddress:{street:Rua Pastor Blablabla,number:666,additionalInfo:Bloco B, pru00e9dio vermelho.,reference:Pru00f3ximo ao shopping aricanduva,neighborhood:Jardim Imperador (Zona Leste),city:Sao Paulo,state:SP,zipcode:00000000},totalAmount:169.89,totalFreight:0,totalDiscount:0,totalInterest:0,paymentMethods:[{sequential:1,id:CREDIT_CARD,value:169.89,installments:2}]}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDotBeforeComma);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDotBeforeComma, false);
assertTrue(json.isJsonObject());
}
@@ -152,7 +153,7 @@ public void getJSONWithDotBeforeComma() throws IOException {
@Order(12)
public void getJSONFromEmptyString() throws IOException {
assertThrows(JsonParseException.class,()->{
- formatter.checkValidityAndFormatObject("");
+ formatter.checkValidityAndFormatObject("", false);
});
}
@@ -161,7 +162,7 @@ public void getJSONFromEmptyString() throws IOException {
@Order(13)
public void getJSONFromEmptyObjectAsString() throws IOException {
assertThrows(StringIndexOutOfBoundsException.class,()->{
- formatter.checkValidityAndFormatObject("{}");
+ formatter.checkValidityAndFormatObject("{}", false);
});
}
@@ -170,7 +171,7 @@ public void getJSONFromEmptyObjectAsString() throws IOException {
@Order(14)
public void getJSONFromJsonObjectWithoutValue() throws IOException {
assertThrows(StringIndexOutOfBoundsException.class,()->{
- formatter.checkValidityAndFormatObject("{blablablabla}");
+ formatter.checkValidityAndFormatObject("{blablablabla}", false);
});
}
@@ -178,9 +179,8 @@ public void getJSONFromJsonObjectWithoutValue() throws IOException {
@DisplayName("Get a JSON From Json Object with a comma as value")
@Order(15)
public void getJSONFromJsonObjectWithCommaAsValue() throws IOException {
- assertThrows(JsonSyntaxException.class,()->{
- formatter.checkValidityAndFormatObject("{id: ,}");
- });
+ JsonObject json = formatter.checkValidityAndFormatObject("{id: ,}", false);
+ assertTrue(json.isJsonObject());
}
@Test
@@ -189,7 +189,7 @@ public void getJSONFromJsonObjectWithCommaAsValue() throws IOException {
public void getJSONFromJsonFile() throws IOException {
File file = new File("src/test/resources/mock.json");
BufferedReader reader = new BufferedReader(new FileReader(file));
- JsonObject json = formatter.checkValidityAndFormatObject(reader);
+ JsonObject json = formatter.checkValidityAndFormatObject(reader, false);
assertTrue(json.isJsonObject());
}
@@ -200,7 +200,7 @@ public void getInvalidJSONFromJsonFile() throws IOException {
assertThrows(FileNotFoundException.class,()->{
File file = new File("");
BufferedReader reader = new BufferedReader(new FileReader(file));
- formatter.checkValidityAndFormatObject(reader);
+ formatter.checkValidityAndFormatObject(reader, false);
});
}
@@ -209,7 +209,7 @@ public void getInvalidJSONFromJsonFile() throws IOException {
@Order(18)
public void getJSONArrayFromEmptyObjectAsString() throws IOException {
assertThrows(StringIndexOutOfBoundsException.class,()->{
- formatter.checkValidityAndFormatObject("[]");
+ formatter.checkValidityAndFormatObject("[]", false);
});
}
@@ -218,7 +218,7 @@ public void getJSONArrayFromEmptyObjectAsString() throws IOException {
@Order(19)
public void verifyIfJSONArrayIsJSONObject() throws IOException {
String jsonWithDotBeforeComma = "{paymentMethods:[{sequential:1,id:CREDIT_CARD,value:169.89,installments:2}]}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDotBeforeComma);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDotBeforeComma, false);
JsonElement jsonElement = json.get("paymentMethods");
assertFalse(jsonElement.isJsonObject());
@@ -229,7 +229,7 @@ public void verifyIfJSONArrayIsJSONObject() throws IOException {
@Order(20)
public void verifyIfJSONObjectHasValidValue() throws IOException {
String jsonWithDotBeforeComma = "{id:1234567890, productCode:02-671806364}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDotBeforeComma);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDotBeforeComma, false);
JsonElement jsonElement = json.get("id");
assertTrue(jsonElement.isJsonPrimitive()); //JsonPrimitive = primitive types and Java types
@@ -240,7 +240,7 @@ public void verifyIfJSONObjectHasValidValue() throws IOException {
@Order(21)
public void getJSONObjectWithDoubleCommaAndSpaceValue() throws IOException {
String jsonWithDoubleCommaAndSpace = "{id:268852005101,productCode:02-688520051,address:{street:Rua B,number:666,additionalInfo:Apto 666 , Bloco 1 ,,reference:Organizacoes Tabajara,neighborhood:Tabajara,city:São Paulo,state:SP,zipcode:12345678}}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDoubleCommaAndSpace);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithDoubleCommaAndSpace, false);
assertTrue(json.isJsonObject());
}
@@ -249,7 +249,7 @@ public void getJSONObjectWithDoubleCommaAndSpaceValue() throws IOException {
@Order(22)
public void getJSONObjectWithOnlyHoursAsString() throws IOException {
String jsonWithOnlyHoursAsString = "{id:268862679704,productCode:02-688626797,purchaseDate:2019-02-03,address:{street:Rua Cinco,number:240,additionalInfo:Teste,reference:Ao lado lotus, Recebimento 7:15 as 17:00,neighborhood:Centro,city:Lavras,state:MG,zipcode:00000000}}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithOnlyHoursAsString);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithOnlyHoursAsString, false);
assertTrue(json.isJsonObject());
}
@@ -258,7 +258,7 @@ public void getJSONObjectWithOnlyHoursAsString() throws IOException {
@Order(23)
public void getJSONObjectWithParenthesesWronglyPlaced() throws IOException {
String jsonWithParenthesesWronglyPlaced = "{id:266861122901,productCode:02-668611229,address:{street:Rua Teste,number:22,additionalInfo:Apto 01,reference:Em frente a padaria ( a casa nao tem porteiro, ou campainha) ligar avisando que chegou,neighborhood:Teste,city:Lavras,state:MG,zipcode:37200000}}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithParenthesesWronglyPlaced);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithParenthesesWronglyPlaced, false);
assertTrue(json.isJsonObject());
}
@@ -267,7 +267,79 @@ public void getJSONObjectWithParenthesesWronglyPlaced() throws IOException {
@Order(24)
public void getJSONObjectWithColonWronglyPlaced() throws IOException {
String jsonWithColonWronglyPlaced = "{id:268856993701,productCode:02-688569937,purchaseDate:2019-02-02,address:{street:Rua Pachecao,number:2019,additionalInfo:casa FRENTE. 21965307587,reference:depois do ponto de onibus 666, no seguno numero. Procurar fulano TELE: 35 981149567 .,neighborhood:Jardim Floresta,city:Lavras,state:MG,zipcode:37200000}}";
- JsonObject json = formatter.checkValidityAndFormatObject(jsonWithColonWronglyPlaced);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonWithColonWronglyPlaced, false);
+ assertTrue(json.isJsonObject());
+ }
+
+ @Test
+ @DisplayName("Remove JSON Attribute from String")
+ @Order(25)
+ public void removeJSONAttributeFromString() throws IOException {
+ String jsonToRemove = "{id:268852005101,productCode:02-688520051,purchaseDate:2019-02-01,lastUpdate:2019-02-01}";
+ logger.info("Invalid json with string to remove: " + jsonToRemove);
+ String[] removeAll = {"purchaseDate"};
+ String jsonFormatted = formatter.removeJSONObjectsFromString(jsonToRemove, removeAll);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonFormatted, false);
+ assertTrue(json.isJsonObject());
+ }
+
+ @Test
+ @DisplayName("Remove JSON Object from String")
+ @Order(26)
+ public void removeJSONObjectFromString() throws IOException {
+ String jsonToRemove = "{pf:{cpf:11122233385,name:MARIANA DE AZEVEDO SANTOS}, localDate:2019-02-01}";
+ logger.info("Invalid json with string to remove: " + jsonToRemove);
+ String[] removeAll = {"pf"};
+ String jsonFormatted = formatter.removeJSONObjectsFromString(jsonToRemove, removeAll);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonFormatted, false);
+ assertTrue(json.isJsonObject());
+ }
+
+ @Test
+ @DisplayName("Remove JSON Array from String")
+ @Order(27)
+ public void removeJSONArrayFromString() throws IOException {
+ String jsonToRemove = "{totalAmount:326.98,totalFreight:79.99,totalDiscount:0,products:[{link:{id:BLABLABLA-1,rel:sku},quantity:1,price:246.99,freight:79.99,discount:0}, {link:{id:BLABLABLA-2,rel:sku},quantity:1,price:246.99,freight:79.99,discount:0}]}";
+ logger.info("Invalid json with string to remove: " + jsonToRemove);
+ String[] removeAll = {"products"};
+ String jsonFormatted = formatter.removeJSONObjectsFromString(jsonToRemove, removeAll);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonFormatted, false);
+ assertTrue(json.isJsonObject());
+ }
+
+ @Test
+ @DisplayName("Remove more than one attribute from String")
+ @Order(28)
+ public void removeMoreThanOneAttributeFromString() throws IOException {
+ String jsonToRemove = "{totalAmount:326.98,totalFreight:79.99,totalDiscount:0,products:[{link:{id:BLABLABLA-1,rel:sku},quantity:1,price:246.99,freight:79.99,discount:0}, {link:{id:BLABLABLA-2,rel:sku},quantity:1,price:246.99,freight:79.99,discount:0}]}";
+ logger.info("Invalid json with string to remove: " + jsonToRemove);
+ String[] removeAll = {"products", "totalAmount"};
+ String jsonFormatted = formatter.removeJSONObjectsFromString(jsonToRemove, removeAll);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonFormatted, false);
+ assertTrue(json.isJsonObject());
+ }
+
+ @Test
+ @DisplayName("Filter JSON Object from String")
+ @Order(29)
+ public void filterJSONObjectFromString() throws IOException {
+ String jsonToRemove = "{totalAmount:326.98,totalFreight:79.99,totalDiscount:0,products:[{link:{id:BLABLABLA-1,rel:sku},quantity:1,price:246.99,freight:79.99,discount:0}, {link:{id:BLABLABLA-2,rel:sku},quantity:1,price:246.99,freight:79.99,discount:0}]}";
+ logger.info("Invalid json with string to filter: " + jsonToRemove);
+ String[] filter = {"totalDiscount"};
+ String jsonFormatted = formatter.filterJSONObjectsFromString(jsonToRemove, filter);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonFormatted, false);
+ assertTrue(json.isJsonObject());
+ }
+
+ @Test
+ @DisplayName("Filter more than one attribute from String")
+ @Order(30)
+ public void filterMoreThanOneAttributeFromString() throws IOException {
+ String jsonToRemove = "{totalAmount:326.98,totalFreight:79.99,totalDiscount:0,products:[{link:{id:BLABLABLA-1,rel:sku},quantity:1,price:246.99,freight:79.99,discount:0}, {link:{id:BLABLABLA-2,rel:sku},quantity:1,price:246.99,freight:79.99,discount:0}]}";
+ logger.info("Invalid json with string to filter: " + jsonToRemove);
+ String[] filter = {"products", "totalAmount"};
+ String jsonFormatted = formatter.filterJSONObjectsFromString(jsonToRemove, filter);
+ JsonObject json = formatter.checkValidityAndFormatObject(jsonFormatted, false);
assertTrue(json.isJsonObject());
}