naddym commented on a change in pull request #5083:
URL: https://github.com/apache/nifi/pull/5083#discussion_r635235984



##########
File path: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/FlattenJson.java
##########
@@ -157,25 +191,35 @@ public void onTrigger(final ProcessContext context, final 
ProcessSession session
         final String mode = context.getProperty(FLATTEN_MODE).getValue();
         final FlattenMode flattenMode = getFlattenMode(mode);
 
-        String separator = 
context.getProperty(SEPARATOR).evaluateAttributeExpressions(flowFile).getValue();
-
+        final String separator = 
context.getProperty(SEPARATOR).evaluateAttributeExpressions(flowFile).getValue();
+        final String returnType = context.getProperty(RETURN_TYPE).getValue();
+        final PrintMode printMode = 
context.getProperty(PRETTY_PRINT).asBoolean() ? PrintMode.PRETTY : 
PrintMode.MINIMAL;
 
         try {
-            ByteArrayOutputStream bos = new ByteArrayOutputStream();
-            session.exportTo(flowFile, bos);
-            bos.close();
-
-            String raw = new String(bos.toByteArray());
-            final String flattened = new JsonFlattener(raw)
-                    .withFlattenMode(flattenMode)
-                    .withSeparator(separator.charAt(0))
-                    .withStringEscapePolicy(() -> 
StringEscapeUtils.ESCAPE_JSON)
-                    .flatten();
-
-            flowFile = session.write(flowFile, os -> 
os.write(flattened.getBytes()));
+            final StringBuilder contents = new StringBuilder();
+            session.read(flowFile, in -> contents.append(IOUtils.toString(in, 
Charset.defaultCharset())));
+
+            final String resultedJson;
+            if (returnType.equals(RETURN_TYPE_FLATTEN)) {
+                resultedJson = new JsonFlattener(contents.toString())
+                        .withFlattenMode(flattenMode)
+                        .withSeparator(separator.charAt(0))
+                        .withStringEscapePolicy(() -> 
StringEscapeUtils.ESCAPE_JSON)
+                        .withPrintMode(printMode)
+                        .flatten();
+            } else {
+                resultedJson = new JsonUnflattener(contents.toString())
+                        .withFlattenMode(flattenMode)
+                        .withSeparator(separator.charAt(0))
+                        .withPrintMode(printMode)
+                        .unflatten();
+            }
+
+            flowFile = session.write(flowFile, out -> 
out.write(resultedJson.getBytes()));

Review comment:
       Sure, will do.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to