diff --git a/.gitignore b/.gitignore index d8c9ae4..303626a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ target/ logs/ .idea/ *.iml +/nbproject/ \ No newline at end of file diff --git a/src/main/java/net/acesinc/data/json/generator/JsonGenerator.java b/src/main/java/net/acesinc/data/json/generator/JsonGenerator.java index d642a4f..4e1717c 100644 --- a/src/main/java/net/acesinc/data/json/generator/JsonGenerator.java +++ b/src/main/java/net/acesinc/data/json/generator/JsonGenerator.java @@ -51,7 +51,8 @@ public List> testListGenerator(String config) throws IOExcep } public static void main(String... args) { - String config = "config6.json"; + String config = "config-array-test.json"; +// String config = "config1.json"; try { ObjectMapper mapper = new ObjectMapper(); JsonGenerator gen = new JsonGenerator(); diff --git a/src/main/java/net/acesinc/data/json/generator/RandomJsonGenerator.java b/src/main/java/net/acesinc/data/json/generator/RandomJsonGenerator.java index 37fc82f..43d7e94 100644 --- a/src/main/java/net/acesinc/data/json/generator/RandomJsonGenerator.java +++ b/src/main/java/net/acesinc/data/json/generator/RandomJsonGenerator.java @@ -71,49 +71,15 @@ public List> generateJsonList() throws IOException { } private javax.json.stream.JsonGenerator processProperties(javax.json.stream.JsonGenerator gen, Map props, String currentContext) { -// Map outputValues = new LinkedHashMap<>(); for (String propName : props.keySet()) { Object value = props.get(propName); if (value == null) { -// outputValues.put(propName, null); generatedValues.put(currentContext + propName, null); addValue(gen, propName, null); } else if (String.class.isAssignableFrom(value.getClass())) { String type = (String) value; - if (type.startsWith("this.") || type.startsWith("cur.")) { - String refPropName = null; - if (type.startsWith("this.")) { - refPropName = type.substring("this.".length(), type.length()); - } else if (type.startsWith("cur.")) { - refPropName = currentContext + type.substring("cur.".length(), type.length()); - } - Object refPropValue = generatedValues.get(refPropName); - if (refPropValue != null) { - addValue(gen, propName, refPropValue); - } else { - log.warn("Sorry, unable to reference property [ " + refPropName + " ]. Maybe it hasn't been generated yet?"); - } - } else { - try { - TypeHandler th = TypeHandlerFactory.getInstance().getTypeHandler(type, generatedValues, currentContext); - - if (th != null) { - Object val = th.getNextRandomValue(); -// outputValues.put(propName, val); - generatedValues.put(currentContext + propName, val); - addValue(gen, propName, val); - } else { -// log.debug("Unknown Type: [ " + type + " ] for prop [ " + propName + " ]. Attempting to echo literal value."); -// outputValues.put(propName, type); - generatedValues.put(currentContext + propName, type); - addValue(gen, propName, type); - } - } catch (IllegalArgumentException iae) { - log.warn("Error creating type [ " + type + " ]. Prop [ " + propName + " ] being ignored in output. Reason: " + iae.getMessage()); - log.debug("Error creating type [ " + type + " ]. Prop [ " + propName + " ] being ignored in output.", iae); - } - } + handleStringGeneration(type, currentContext, gen, propName); } else if (Map.class.isAssignableFrom(value.getClass())) { //nested object Map nestedProps = (Map) value; @@ -138,7 +104,7 @@ private javax.json.stream.JsonGenerator processProperties(javax.json.stream.Json String newContext = ""; if (propName != null) { gen.writeStartArray(propName); - + if (currentContext.isEmpty()) { newContext = propName; } else { @@ -150,6 +116,7 @@ private javax.json.stream.JsonGenerator processProperties(javax.json.stream.Json if (!listOfItems.isEmpty()) { //Check if this is a special function at the start of the array + boolean wasSpecialCase = false; if (String.class.isAssignableFrom(listOfItems.get(0).getClass()) && ((String) listOfItems.get(0)).contains("(")) { //special function in array String name = (String) listOfItems.get(0); @@ -175,16 +142,20 @@ private javax.json.stream.JsonGenerator processProperties(javax.json.stream.Json for (int i = 0; i < timesToRepeat; i++) { processList(subList, gen, newContext); } + wasSpecialCase = true; break; } case "random": { //choose one of the items in the list at random List subList = listOfItems.subList(1, listOfItems.size()); Object item = subList.get(new RandomDataGenerator().nextInt(0, subList.size() - 1)); processItem(item, gen, newContext + "[0]"); + wasSpecialCase = true; break; } } - } else { //it's not a special function, so just add it + } + + if (!wasSpecialCase) { //it's not a special function, so process it like normal processList(listOfItems, gen, newContext); } } @@ -199,6 +170,42 @@ private javax.json.stream.JsonGenerator processProperties(javax.json.stream.Json return gen; } + protected void handleStringGeneration(String type, String currentContext, JsonGenerator gen, String propName) { + if (type.startsWith("this.") || type.startsWith("cur.")) { + String refPropName = null; + if (type.startsWith("this.")) { + refPropName = type.substring("this.".length(), type.length()); + } else if (type.startsWith("cur.")) { + refPropName = currentContext + type.substring("cur.".length(), type.length()); + } + Object refPropValue = generatedValues.get(refPropName); + if (refPropValue != null) { + addValue(gen, propName, refPropValue); + } else { + log.warn("Sorry, unable to reference property [ " + refPropName + " ]. Maybe it hasn't been generated yet?"); + } + } else { + try { + TypeHandler th = TypeHandlerFactory.getInstance().getTypeHandler(type, generatedValues, currentContext); + + if (th != null) { + Object val = th.getNextRandomValue(); +// outputValues.put(propName, val); + generatedValues.put(currentContext + propName, val); + addValue(gen, propName, val); + } else { +// log.debug("Unknown Type: [ " + type + " ] for prop [ " + propName + " ]. Attempting to echo literal value."); +// outputValues.put(propName, type); + generatedValues.put(currentContext + propName, type); + addValue(gen, propName, type); + } + } catch (IllegalArgumentException iae) { + log.warn("Error creating type [ " + type + " ]. Prop [ " + propName + " ] being ignored in output. Reason: " + iae.getMessage()); + log.debug("Error creating type [ " + type + " ]. Prop [ " + propName + " ] being ignored in output.", iae); + } + } + } + protected void processList(List listOfItems, JsonGenerator gen, String currentContext) { for (int i = 0; i < listOfItems.size(); i++) { Object item = listOfItems.get(i); @@ -209,9 +216,8 @@ protected void processList(List listOfItems, JsonGenerator gen, String c protected void processItem(Object item, JsonGenerator gen, String currentContext) { if (String.class.isAssignableFrom(item.getClass())) { - //literal string, just add it - addValue(gen, null, (String) item); - generatedValues.put(currentContext, (String) item); + //process it like normal + handleStringGeneration((String) item, currentContext, gen, null); } else if (Map.class.isAssignableFrom(item.getClass())) { Map nestedProps = (Map) item; gen.writeStartObject(); @@ -231,15 +237,35 @@ private javax.json.stream.JsonGenerator addValue(javax.json.stream.JsonGenerator gen.write((String) val); } } else if (Boolean.class.isAssignableFrom(val.getClass())) { - gen.write(propName, (Boolean) val); + if (propName != null) { + gen.write(propName, (Boolean) val); + } else { + gen.write((Boolean) val); + } } else if (Long.class.isAssignableFrom(val.getClass())) { - gen.write(propName, (Long) val); + if (propName != null) { + gen.write(propName, (Long) val); + } else { + gen.write((Long) val); + } } else if (Integer.class.isAssignableFrom(val.getClass())) { - gen.write(propName, (Integer) val); + if (propName != null) { + gen.write(propName, (Integer) val); + } else { + gen.write((Integer) val); + } } else if (Double.class.isAssignableFrom(val.getClass())) { - gen.write(propName, (Double) val); + if (propName != null) { + gen.write(propName, (Double) val); + } else { + gen.write((Double) val); + } } else if (Date.class.isAssignableFrom(val.getClass())) { - gen.write(propName, iso8601DF.format((Date) val)); + if (propName != null) { + gen.write(propName, iso8601DF.format((Date) val)); + } else { + gen.write(iso8601DF.format((Date) val)); + } } return gen; } diff --git a/src/main/resources/config-array-test.json b/src/main/resources/config-array-test.json new file mode 100644 index 0000000..9664b09 --- /dev/null +++ b/src/main/resources/config-array-test.json @@ -0,0 +1,9 @@ +{ + "timestamp": "nowTimestamp()", + "array-that-worked-with-issue43": [{"one":"integer()"}, {"two":"double(-90.0, 90.0)"}], + "array-that-didnt-work-with-issue43": ["integer()", "double(-90.0, 90.0)", "boolean()","date(\"2015/03/01T00:00:00\")", "long()"], + "location": { + "lat": "double(-90.0, 90.0)", + "lon": "double(-180.0, 180.0)" + } +} \ No newline at end of file diff --git a/src/main/resources/config1.json b/src/main/resources/config1.json index 0a7047c..31c7ea6 100644 --- a/src/main/resources/config1.json +++ b/src/main/resources/config1.json @@ -25,12 +25,12 @@ }, "dates": { "rand-date": "date()", - "min-date": "date(\"2015/03/01\")", - "range-date": "date(\"2015/03/01\",\"2015/03/30\")", + "min-date": "date(\"2015/03/01T00:00:00\")", + "range-date": "date(\"2015/03/01T00:00:00\",\"2015/03/30T00:00:00\")", "now": "now()", "nowTimestamp": "nowTimestamp()", "5days-ago": "now(-5_d)", - "timestamp": "timestamp(\"2015/03/01\",\"2015/03/30\")" + "timestamp": "timestamp(\"2015/03/01T00:00:00\",\"2015/03/30T00:00:00\")" }, "literals": { "literal-bool": true,