/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.hortonworks.streamline.streams.runtime.storm.spout;


import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.storm.hdfs.spout.ParseException;
import org.apache.storm.hdfs.spout.TextFileReader;
import com.hortonworks.streamline.streams.common.StreamlineEventImpl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  Converts each JSON line in the Text file into a SreamlineEvent based tuple
 */

public class JsonFileReader extends TextFileReader {
    public JsonFileReader(FileSystem fs, Path file, Map conf) throws IOException {
        super(fs, file, conf);
    }

    public JsonFileReader(FileSystem fs, Path file, Map conf, String startOffset) throws IOException {
        super(fs, file, conf, startOffset);
    }

    public List<Object> next() throws IOException, ParseException {
        List<Object> lineTuple = super.next();
        String jsonLine = (String)lineTuple.get(0);
        if(jsonLine==null)
            return null;
        //1- convert Json to Map<>
        HashMap<String,Object> jsonMap = new ObjectMapper().readValue(jsonLine, HashMap.class);

        //2- make StreamlineEvent from map
        StreamlineEventImpl slEvent = new StreamlineEventImpl(jsonMap, "HdfsSpout");

        //3- create tuple from StreamlineEvent
        ArrayList<Object> tuple = new ArrayList<>();
        tuple.add(slEvent);
        return tuple;
    }
}

@@ -865,16 +865,75 @@
 pout;%0A%0A%0A
+import com.fasterxml.jackson.core.JsonProcessingException;%0A
 import c
@@ -1276,24 +1276,54 @@
 .ArrayList;%0A
+import java.util.Collections;%0A
 import java.
@@ -1947,16 +1947,17 @@
 (String)
+ 
 lineTupl
@@ -1976,17 +1976,19 @@
       if
-(
+ ( 
 jsonLine
@@ -1993,16 +1993,17 @@
 ne==null
+ 
 )%0A      
@@ -2021,16 +2021,103 @@
 n null;%0A
+        if ( jsonLine.trim().isEmpty() )%0A            return next();%0A%0A        try %7B%0A    
         
@@ -2151,16 +2151,20 @@
         
+    
 HashMap%3C
@@ -2170,16 +2170,17 @@
 %3CString,
+ 
 Object%3E 
@@ -2253,16 +2253,20 @@
         
+  
+  
 //2- mak
@@ -2292,16 +2292,20 @@
 rom map%0A
+    
         
@@ -2378,24 +2378,28 @@
 fsSpout%22);%0A%0A
+    
         //3-
@@ -2445,102 +2445,218 @@
     
-ArrayList%3CObject%3E tuple = new ArrayList%3C%3E();%0A        tuple.add(slEvent);%0A        return tuple;
+    return Collections.singletonList(slEvent);%0A        %7D catch (JsonProcessingException e) %7B%0A            throw new ParseException(%22Json parsing error at location : %22 + getFileOffset().toString(), e);%0A        %7D%0A
 %0A   

Improve error handling in JsonFileReader