Jim C
Jim C

Reputation: 4395

SpringBatch - how to set up via java config the JsonLineMapper for reading a simple json file

How to change from "setLineTokenizer(new DelimitedLineTokenizer()...)" to "JsonLineMapper" in the first code below? Basicaly, it is working with csv but I want to change it to read a simple json file. I found some threads here asking about complex json but this is not my case. Firstly I thought that I should use a very diferent approach from csv way, but after I read SBiAch05sample.pdf (see the link and snippet at the bottom), I understood that FlatFileItemReader can be used to read json format. In almost similiar question, I can guess that I am not in the wrong direction. Please, I am trying to find the simplest but elegant and recommended way for fixing this snippet code. So, the wrapper below, unless I am really obligated to work this way, seems to go further. Additionally, the wrapper seems to me more Java 6 style than my tentative which takes advantage of anonimous method from Java 7 (as far as I can judge from studies). Please, any advise is higly appreciated.

//My Code

                @Bean
                @StepScope
                public FlatFileItemReader<Message> reader() {
                                log.info("ItemReader >>");
                                FlatFileItemReader<Message> reader = new FlatFileItemReader<Message>();
                                reader.setResource(new ClassPathResource("test_json.js"));
                                reader.setLineMapper(new DefaultLineMapper<Message>() {
                                                {
                                                                setLineTokenizer(new DelimitedLineTokenizer() {
{
                setNames(new String[] { "field1", "field2"...

//Sample using a wrapper

http://www.manning.com/templier/SBiAch05sample.pdf

import org.springframework.batch.item.file.LineMapper;
import org.springframework.batch.item.file.mapping.JsonLineMapper;
import com.manning.sbia.ch05.Product;
public class WrappedJsonLineMapper implements LineMapper<Product> {
       private JsonLineMapper delegate;
       public Product mapLine(String line, int lineNumber) throws Exception {
                Map<String,Object> productAsMap
                               = delegate.mapLine(line, lineNumber);
              Product product = new Product();
              product.setId((String)productAsMap.get("id"));
              product.setName((String)productAsMap.get("name"));
              product.setDescription((String)productAsMap.get("description"));
              product.setPrice(new Float((Double)productAsMap.get("price")));
              return product;
       }
       public void setDelegate(JsonLineMapper delegate) {
              this.delegate = delegate;
       }
}

Upvotes: 0

Views: 2084

Answers (1)

Michael Minella
Michael Minella

Reputation: 21473

Really you have two options for parsing JSON within a Spring Batch job:

  1. Don't create a LineMapper, create a LineTokenizer. Spring Batch's DefaultLineMapper breaks up the parsing of a record into two phases, parsing the record and mapping the result to an object. The fact that the incoming data is JSON vs a CSV only impacts the parsing piece (which is handled by the LineTokenizer). That being said, you'd have to write your own LineTokenizer to parse the JSON into a FieldSet.
  2. Use the provided JsonLineMapper. Spring Batch provides a LineMapper implementation that uses Jackson to deserialize JSON objects into java objects.

In either case, you can't map a LineMapper to a LineTokenizer as they accomplish two different things.

Upvotes: 1

Related Questions