Github user trkurc commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/136#discussion_r46499759
  
    --- Diff: 
nifi-nar-bundles/nifi-avro-bundle/nifi-avro-processors/src/main/java/org/apache/nifi/processors/avro/ConvertAvroToJSON.java
 ---
    @@ -116,51 +124,59 @@ public void onTrigger(ProcessContext context, 
ProcessSession session) throws Pro
             }
     
             final String containerOption = 
context.getProperty(CONTAINER_OPTIONS).getValue();
    +        final boolean useContainer = 
containerOption.equals(CONTAINER_ARRAY);
    +        // Wrap a single record (inclusive of no records) only when a 
container is being used
    +        final boolean wrapSingleRecord = 
context.getProperty(WRAP_SINGLE_RECORD).asBoolean() && useContainer;
     
             try {
                 flowFile = session.write(flowFile, new StreamCallback() {
                     @Override
                     public void process(final InputStream rawIn, final 
OutputStream rawOut) throws IOException {
                         try (final InputStream in = new 
BufferedInputStream(rawIn);
    -
    -                        final OutputStream out = new 
BufferedOutputStream(rawOut);
    -                        final DataFileStream<GenericRecord> reader = new 
DataFileStream<>(in, new GenericDatumReader<GenericRecord>())) {
    +                         final OutputStream out = new 
BufferedOutputStream(rawOut);
    +                         final DataFileStream<GenericRecord> reader = new 
DataFileStream<>(in, new GenericDatumReader<GenericRecord>())) {
     
                             final GenericData genericData = GenericData.get();
     
    -                        if (reader.hasNext() == false ) {
    -                            out.write(EMPTY_JSON_OBJECT);
    -                            return;
    +                        int recordCount = 0;
    +                        GenericRecord currRecord = null;
    +                        if (reader.hasNext()) {
    +                            currRecord = reader.next();
    +                            recordCount++;
                             }
    -                        int recordCount = 1;
    -                        GenericRecord reuse = reader.next();
    -                        // Only open container if more than one record
    -                        if(reader.hasNext() && 
containerOption.equals(CONTAINER_ARRAY)){
    +
    +                        // Open container if desired output is an array 
format and there are are multiple records or
    +                        // if configured to wrap single record
    +                        if (reader.hasNext() && useContainer || 
wrapSingleRecord) {
                                 out.write('[');
                             }
    -                        
out.write(genericData.toString(reuse).getBytes(StandardCharsets.UTF_8));
    +
    +                        // Determine the initial output record, inclusive 
if we should have an empty set of Avro records
    +                        final byte[] outputBytes = (currRecord == null) ? 
EMPTY_JSON_OBJECT : 
genericData.toString(currRecord).getBytes(StandardCharsets.UTF_8);
    +                        out.write(outputBytes);
     
                             while (reader.hasNext()) {
    -                            if (containerOption.equals(CONTAINER_ARRAY)) {
    +                            if (useContainer) {
                                     out.write(',');
                                 } else {
                                     out.write('\n');
                                 }
     
    -                            reuse = reader.next(reuse);
    -                            
out.write(genericData.toString(reuse).getBytes(StandardCharsets.UTF_8));
    +                            currRecord = reader.next(currRecord);
    +                            
out.write(genericData.toString(currRecord).getBytes(StandardCharsets.UTF_8));
                                 recordCount++;
                             }
     
    -                        // Only close container if more than one record
    -                        if (recordCount > 1 && 
containerOption.equals(CONTAINER_ARRAY)) {
    +                        // Close container if desired output is an array 
format and there are multiple records or if
    +                        // configured to wrap a single record
    +                        if (recordCount > 1 && useContainer || 
wrapSingleRecord) {
    --- End diff --
    
    For next time, comment is good, but parens would make the logic more clear 
for the next coder to come along:
    ```
    if ((recordCount > 1 && useContainer) || wrapSingleRecord) {
    ```



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to