diff --git a/pom.xml b/pom.xml index 00d3cab..b00d454 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ com.americanexpress.unify.jdocs unify-jdocs - 1.7.0 + 1.7.1 jar unify-jdocs @@ -108,6 +108,12 @@ ${jfiglet.version} + + io.vavr + vavr + 0.10.4 + + org.junit.jupiter junit-jupiter-engine diff --git a/readme.md b/readme.md index a80dbe8..97faf36 100644 --- a/readme.md +++ b/readme.md @@ -15,7 +15,7 @@ JDocs is available as a jar file in Maven central with the following latest Mave ````pom com.americanexpress.unify.jdocs unify-jdocs -1.7.0 +1.7.1 ```` --- diff --git a/src/main/java/com/americanexpress/unify/jdocs/JDocument.java b/src/main/java/com/americanexpress/unify/jdocs/JDocument.java index c40a22a..77e7b7e 100644 --- a/src/main/java/com/americanexpress/unify/jdocs/JDocument.java +++ b/src/main/java/com/americanexpress/unify/jdocs/JDocument.java @@ -26,6 +26,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.node.*; +import io.vavr.Tuple2; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -1020,15 +1021,17 @@ private void validatePath1(String path, CONSTS_JDOCS.API api, List tokenL } } - protected Object getValue(String path, Class clazz, List tokenList) { + protected Tuple2 getValue(String path, Class clazz, List tokenList) { JsonNode node = null; Object value = null; + Boolean isPathPresent = true; while (true) { node = traverse(rootNode, tokenList, false); if (node == null) { value = null; + isPathPresent = false; break; } @@ -1112,7 +1115,7 @@ protected Object getValue(String path, Class clazz, List tokenList) { break; } - return value; + return new Tuple2(value, isPathPresent); } private void setLeafNode(ObjectNode node, String field, Object value) { @@ -1442,8 +1445,12 @@ public Object getValue(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - Object value = getValue(path, null, tokenList); - checkFieldValue(path, modelPath, value, false); + Tuple2 tuple2 = getValue(path, null, tokenList); + Object value = tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, false); + } return value; } @@ -1452,8 +1459,12 @@ public String getString(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - String value = (String)getValue(path, String.class, tokenList); - checkFieldValue(path, modelPath, value, false); + Tuple2 tuple2 = getValue(path, String.class, tokenList); + String value = (String)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, false); + } return value; } @@ -1479,8 +1490,12 @@ public Integer getInteger(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - Integer value = (Integer)getValue(path, Integer.class, tokenList); - checkFieldValue(path, modelPath, value, false); + Tuple2 tuple2 = getValue(path, Integer.class, tokenList); + Integer value = (Integer)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, false); + } return value; } @@ -1489,8 +1504,12 @@ public Boolean getBoolean(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - Boolean value = (Boolean)getValue(path, Boolean.class, tokenList); - checkFieldValue(path, modelPath, value, false); + Tuple2 tuple2 = getValue(path, Boolean.class, tokenList); + Boolean value = (Boolean)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, false); + } return value; } @@ -1499,8 +1518,12 @@ public Long getLong(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - Long value = (Long)getValue(path, Long.class, tokenList); - checkFieldValue(path, modelPath, value, false); + Tuple2 tuple2 = getValue(path, Long.class, tokenList); + Long value = (Long)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, false); + } return value; } @@ -1509,8 +1532,12 @@ public BigDecimal getBigDecimal(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - BigDecimal value = (BigDecimal)getValue(path, BigDecimal.class, tokenList); - checkFieldValue(path, modelPath, value, false); + Tuple2 tuple2 = getValue(path, BigDecimal.class, tokenList); + BigDecimal value = (BigDecimal)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, false); + } return value; } @@ -1519,8 +1546,12 @@ public Object getArrayValue(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - Object value = getValue(path, null, tokenList); - checkFieldValue(path, modelPath, value, true); + Tuple2 tuple2 = getValue(path, null, tokenList); + Object value = tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, true); + } return value; } @@ -1529,8 +1560,12 @@ public String getArrayValueString(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - String value = (String)getValue(path, String.class, tokenList); - checkFieldValue(path, modelPath, value, true); + Tuple2 tuple2 = getValue(path, String.class, tokenList); + String value = (String)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, true); + } return value; } @@ -1539,8 +1574,12 @@ public Integer getArrayValueInteger(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - Integer value = (Integer)getValue(path, Integer.class, tokenList); - checkFieldValue(path, modelPath, value, true); + Tuple2 tuple2 = getValue(path, Integer.class, tokenList); + Integer value = (Integer)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, true); + } return value; } @@ -1549,8 +1588,12 @@ public Boolean getArrayValueBoolean(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - Boolean value = (Boolean)getValue(path, Boolean.class, tokenList); - checkFieldValue(path, modelPath, value, true); + Tuple2 tuple2 = getValue(path, Boolean.class, tokenList); + Boolean value = (Boolean)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, true); + } return value; } @@ -1559,8 +1602,12 @@ public Long getArrayValueLong(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - Long value = (Long)getValue(path, Long.class, tokenList); - checkFieldValue(path, modelPath, value, true); + Tuple2 tuple2 = getValue(path, Long.class, tokenList); + Long value = (Long)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, true); + } return value; } @@ -1569,8 +1616,12 @@ public BigDecimal getArrayValueBigDecimal(String path, String... vargs) { path = getStaticPath(path, vargs); List tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE); String modelPath = checkPathInModel(path, tokenList); - BigDecimal value = (BigDecimal)getValue(path, BigDecimal.class, tokenList); - checkFieldValue(path, modelPath, value, true); + Tuple2 tuple2 = getValue(path, BigDecimal.class, tokenList); + BigDecimal value = (BigDecimal)tuple2._1; + boolean isPathPresent = tuple2._2; + if (isPathPresent == true) { + checkFieldValue(path, modelPath, value, true); + } return value; } @@ -2304,7 +2355,7 @@ private void throwExceptionOrSetErrorList(String errorCode, String path, List tokenList) { case BOOLEAN: if (BaseUtils.compareWithMany(fieldValue, "true", "false") == true) { - value = new Boolean(fieldValue); + value = Boolean.valueOf(fieldValue); } else { throw new UnifyException("jdoc_err_53", fieldName, path); @@ -2528,11 +2579,11 @@ private void validateFilterNames(String path, List tokenList) { break; case INTEGER: - value = new Integer(fieldValue); + value = Integer.valueOf(fieldValue); break; case LONG: - value = new Long(fieldValue); + value = Long.valueOf(fieldValue); break; case DECIMAL: @@ -2725,7 +2776,7 @@ private void setFilterFieldNode(ObjectNode filterNode, String filterField, Strin case BOOLEAN: if (BaseUtils.compareWithMany(filterValue, "true", "false") == true) { - filterNode.put(filterField, new Boolean(filterValue)); + filterNode.put(filterField, Boolean.valueOf(filterValue)); } else { throw new UnifyException("jdoc_err_53", filterField, path); @@ -2737,11 +2788,11 @@ private void setFilterFieldNode(ObjectNode filterNode, String filterField, Strin break; case INTEGER: - filterNode.put(filterField, new Integer(filterValue)); + filterNode.put(filterField, Integer.valueOf(filterValue)); break; case LONG: - filterNode.put(filterField, new Long(filterValue)); + filterNode.put(filterField, Long.valueOf(filterValue)); break; case DECIMAL: diff --git a/src/test/java/com/americanexpress/unify/jdocs/DocumentTest.java b/src/test/java/com/americanexpress/unify/jdocs/DocumentTest.java index f7f0986..8df244b 100644 --- a/src/test/java/com/americanexpress/unify/jdocs/DocumentTest.java +++ b/src/test/java/com/americanexpress/unify/jdocs/DocumentTest.java @@ -135,6 +135,15 @@ void testMisc() { d.setString("$.[1].name", "Nitika"); Document d1 = new JDocument(); d1.setContent(d, "$.[]", "$.application[]"); + + { + // this will run ok + d = new JDocument(); + d.setString("$.pod_info.fname", "Deepak"); + d.setString("$.pod_info.lname", "Arora"); + Document rd = new JDocument("[]"); + rd.setContent(d, "$.pod_info", "$.[0]"); + } } @Test @@ -150,12 +159,12 @@ void testRead() { assertEquals("9999999999", d.getString("$.members[sex=male].phones[type=mobile].number")); // check int and long reads - assertEquals(new Integer(0), d.getInteger("$.members[0].index")); - assertEquals(new Long(0), d.getLong("$.members[0].index")); + assertEquals(Integer.valueOf(0), d.getInteger("$.members[0].index")); + assertEquals(Long.valueOf(0), d.getLong("$.members[0].index")); // check boolean reads - assertEquals(new Boolean(true), d.getBoolean("$.members[0].is_married")); - assertEquals(new Boolean(false), d.getBoolean("$.members[1].is_married")); + assertEquals(Boolean.valueOf(true), d.getBoolean("$.members[0].is_married")); + assertEquals(Boolean.valueOf(false), d.getBoolean("$.members[1].is_married")); // check null read assertEquals(null, d.getString("$.info.iid")); @@ -411,7 +420,7 @@ void testNativeArrayValue() { assertEquals(s, "AZ"); Integer code = (Integer)d.getArrayValue("$.valid_states[0].codes[0]"); - assertEquals(code, new Integer(1)); + assertEquals(code, Integer.valueOf(1)); } @Test @@ -1443,4 +1452,21 @@ void testMissingParams() { } + @Test + void testNumber() { + try { + // all primitive types at root are valid JSONs + Document d = new JDocument("14"); + // use below to read the value of such a JSON document + // String s = d.getJson(); + // long value = Long.valueOf(s); + d = new JDocument("14.1"); + d = new JDocument("true"); + d = new JDocument("\"hello\""); + } + catch (Exception e) { + assert(false); + } + } + }