Nesan Mano
Nesan Mano

Reputation: 2166

Jackson csv writer

I have a function that generates a CSV.

int childCounter =0;
    private void sequentialCSVWriter(SequenceWriter seqW,LinkedHashMap<String,Object> linkedHashMap,boolean child,int childCnt){
    
            Iterator iter = linkedHashMap.values().iterator();
            LinkedHashMap<String,Object> hierarchy = null;
         
           if(linkedHashMap.containsKey("hierarchy")){
                hierarchy = (LinkedHashMap<String,Object>)((List)linkedHashMap.get("hierarchy")).get(0);
                linkedHashMap.remove("hierarchy");
           }
          childCounter +=childCnt;
          for(int i =0 ;i<childCounter;i++){
              System.out.println(childCounter);

                         // the problem is here
                try { seqW.write(",");} catch (IOException e) {throw new UncheckedIOException(e);}
                        System.out.println(hierarchy);
          }
    
           try { seqW.write(linkedHashMap.values());} catch (IOException e) {throw new UncheckedIOException(e);}
            
           if(hierarchy!=null){ 
              sequentialCSVWriter(seqW,hierarchy,true,1);
           }
                
                return;
        }

I keep getting two commas instead of one comma.

"Label","Business Rule Id","Key List","Level"
"20000  -  ROYAL Palace Of King CHarles ","ROLLPO","20000"
",","27367  -  TOTAL BUSINESSES","ROLLP","27367"
"
","20715  -   ENTITY","ROLLPO","20715"
",","20723  -  Britain-CE","ROLLP","20723"
",",",","20769  -  Britain-LE","ROLLP","20769"
",",",",",","20896  -  Britain-LE","ROLLPO","20896"
",",",",",",",","50012  -  Britain-Wales","ROLLPO","50012"

This is how the mapper is configured in the code:

 // Format output as text/csv
                httpResponse.setContentType(Constants.OUTPUT_RESPONSE_CSV + Constants.CONTENT_TYPE_CHARSET_UTF8);
                CsvMapper csvMapper = new CsvMapper();
                DefaultSerializerProvider.Impl sp = new DefaultSerializerProvider.Impl();
                sp.setNullValueSerializer(new CustomNullSerializer());
                csvMapper.setSerializerProvider(sp);
                csvMapper.enable(CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS);
                csvMapper.disable(CsvGenerator.Feature.ALWAYS_QUOTE_EMPTY_STRINGS);
                csvMapper.setDateFormat(dtFormat);

My question is why do I keep seeing two commas in the output.

Upvotes: 0

Views: 99

Answers (0)

Related Questions