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


how change "setlinetokenizer(new delimitedlinetokenizer()...)" "jsonlinemapper" in first code below? basicaly, working csv want change read simple json file. found threads here asking complex json not case. firstly thought should use diferent approach csv way, after read sbiach05sample.pdf (see link , snippet @ bottom), understood flatfileitemreader can used read json format. in almost similiar question, can guess not in wrong direction. please, trying find simplest elegant , recommended way fixing snippet code. so, wrapper below, unless obligated work way, seems go further. additionally, wrapper seems me more java 6 style tentative takes advantage of anonimous method java 7 (as far can judge studies). please, advise 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 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;        } } 

really have 2 options parsing json within spring batch job:

  1. don't create linemapper, create linetokenizer. spring batch's defaultlinemapper breaks parsing of record 2 phases, parsing record , mapping result object. fact incoming data json vs csv impacts parsing piece (which handled linetokenizer). being said, you'd have write own linetokenizer parse json fieldset.
  2. use provided jsonlinemapper. spring batch provides linemapper implementation uses jackson deserialize json objects java objects.

in either case, can't map linemapper linetokenizer accomplish 2 different things.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -