SlideShare una empresa de Scribd logo
1 de 20
Jackson Beyond JSON:
Data format modules (XML, CSV)
Tatu Saloranta
@cowtowncoder
tatu@fasterxml.com
Beyond JSON: Modules
● Modules extend Jackson functionality
o Most new features outside core
o Data type modules for supporting Joda, Guava & more
o JAX-RS provider for using Jackson in Jersey (et al)
o Data formats (this talk) for XML, CSV & more
o Other modules
 Afterburner (code generation, +30% faster)
 Mr Bean (generate impls of interfaces)
 JSON Schema generator
 JAXB Annotation support
Beyond JSON: data formats
Extensions to Jackson that
o implement Streaming API, to expose token/event
streams for reading, token-based writing
o sometimes augmented at data-binding (XML)
o some use ‘format schema’ helper objects (CSV, Avro,
Protobuf)
o allow full range of data-binding (read <X> into POJOs,
write POJOs as <X>)
Beyond JSON: data formats
● Existing data format modules
o Binary JSON: Smile, BSON, CBOR
o JSON-like: MessagePack, YAML
o Textual: CSV, XML
o Binary: Avro, Protobuf (soon!)
● Level of support for processing models
o All support data-binding
o Most support tree model (XML only partial)
o All expose streaming (XML partial)
non-JSON: from DropWizard
Two main ways to use from DropWizard:
● JAX-RS Provider (implement
MessageBodyReader/-Writer):
○ JSON, XML, Smile/CBOR (binary JSON)
○ Simply add a Maven dep (or jar), will auto-register
● DIY
○ InputStream for reading
○ StreamingOutput for writing
○ Can also use with JSON
From DropWizard: automatic
When auto-registering, simply annotate end
points with @Produces, @Consumes…
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
… and things will “Just Work”
From DropWizard: DIY
public class Resource {
private final static ObjectMapper mapper = new ObjectMapper();
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path(“/stuff”)
public StreamingOutput getStuff(InputStream input) throws IOException {
RequestObject req = mapper.readValue(input, RequestObject.class);
final ResponseObject response = …;
return new StreamingOutput() {
public void write(OutputStream out) throws IOException {
mapper.writeValue(out, response);
}
};
// although most commonly, would wrap in ‘Response’ object
}
}
From DropWizard: DIY
Things to note on DIY:
● More work but also more control over
configuration, error handling
● StreamingOutput
o Good for streaming large responses
o Usually wrapped in `Response` (to control headers,
response codes)
● InputStream:
o Need to invoke Bean Validation manually
XML from DropWizard
Jackson XML module, JAX-RS provider:
https://github.com/FasterXML/jackson-dataformat-xml
https://github.com/FasterXML/jackson-jaxrs-providers
and optional JAXB annotation support
https://github.com/FasterXML/jackson-module-jaxb-
annotations
XML from DropWizard
● Due to auto-registration, not much to add
● XML-specific annotations:
o element vs attribute (Jackson / JAXB annotations)
o wrapper vs unwrapped lists
● Jackson vs JAXB
o Both work, viable alternatives
o Jackson has more powerful POJO handling
o JAXB supports “XML-centric” approach
o Beware of XML-providers’ “JSON solutions”
Jackson XML, DIY
If you want to do XML outside of DropWizard:
public class Person {
@JacksonXmlProperty(isAttribute=true)
public int age;
public String name;
}
XmlMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(new Person(...));
=> <Person age=”25”>
<name>Jason Jackson</name>
</Person>
CSV from DropWizard
Jackson CSV module:
https://github.com/FasterXML/jackson-dataformat-csv
No JAX-RS provider yet (need to figure out how to provide
schema); may be added in future.
CSV is Different
Main differences from other formats:
1. Shallow 2-dimensional (tabular) structure
2. Positional (column 1, 2, …)
3. Only native data type String
So how to map to Jackson’s model, POJOs?
1. Handle as “raw” content (List<String[]>)
2. Use schema objects to map position->name
CSV usage, “untyped”
Simple “untyped” usage:
CsvMapper mapper = new CsvMapper();
// or: new ObjectMapper(new CsvFactory());
// read into String[][], or Object[]:
String[][] rows = mapper.readValue(
"1,xyzn2,abcn", String[][].class);
// write similarly; one of:
String csv = mapper.writeValueAsString(rows);
mapper.writeValue(new File(“output.csv”), rows);
CSV usage, “untyped”
// or stream through, for large documents
MappingIterator<String[]> it = mapper
.reader(String[].class)
.readValues(csv);
while (it.hasNext()) {
String[] row = it.nextValue();
// do something with it
}
CSV usage, POJOs
But to get to POJOs, we need CsvSchema:
● simple mapping between column positions, names
o manually:
CsvSchema schema = CsvSchema.builder().addColumn("firstName")
.addColumn("lastName").build()
o or from input (first row) -- see next slide
o or from Java class:
CsvSchema schema = mapper.schemaFor(Pojo.class);
● also contains doc properties: linefeed, column separator...
● NOT formal “CSV Schema”
CSV usage, POJOs
Typically as simple as:
Value[] values = mapper.readerWithSchemaFor(Value.class)
.readValue(new File(“input.csv”), Value[].class);
// or with MappingIterator
mapper.writer(mapper.schemaFor(Value.class)
.writeValue(new File(“output.csv”, values);
// or using 1st line as header for mapping
CsvSchema sch = CsvSchema.emptySchema().withHeader();
values = mapper.reader(Value.class).with(schema)
.readValue(source);
CSV: variations
Many variations:
● some CSV documents define column names in first row
(enable/disable via CsvSchema object)
● Different column separator (“TSV” for tab-delimited),
line separator
● Optional quoting, and/or escaping
● Null value to output (can not omit -- default “”)
● Settings part of CsvSchema object
Beyond JSON, take-away
Key takeaway: one tool, usage as close to
baseline of JSON as possible.
Some follow-up ideas:
● dynamic enabling of binary JSON (HTTP
format negotation)
● YAML for configuration (DropWizard
already uses Jackson YAML)
Beyond JSON, the End
That’s All, Folks! Questions?
Visit Jackson home at
https://github.com/FasterXML/jackson
and/or join mailing lists at
https://groups.google.com/forum/#!forum/jackson-user

Más contenido relacionado

La actualidad más candente

JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and StatementsWebStackAcademy
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
C# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XMLC# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XMLMohammad Shaker
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Ramamohan Chokkam
 
Native XML processing in C++ (BoostCon'11)
Native XML processing in C++ (BoostCon'11)Native XML processing in C++ (BoostCon'11)
Native XML processing in C++ (BoostCon'11)Sumant Tambe
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scalaRuslan Shevchenko
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptWalid Ashraf
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript BasicsMindfire Solutions
 
JavaScript objects and functions
JavaScript objects and functionsJavaScript objects and functions
JavaScript objects and functionsVictor Verhaagen
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJWORKS powered by Ordina
 
Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02Ramamohan Chokkam
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOMJalpesh Vasa
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic FunctionsWebStackAcademy
 

La actualidad más candente (20)

Ajax
AjaxAjax
Ajax
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
C# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XMLC# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XML
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268
 
Native XML processing in C++ (BoostCon'11)
Native XML processing in C++ (BoostCon'11)Native XML processing in C++ (BoostCon'11)
Native XML processing in C++ (BoostCon'11)
 
Week3
Week3Week3
Week3
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Json the-x-in-ajax1588
Json the-x-in-ajax1588Json the-x-in-ajax1588
Json the-x-in-ajax1588
 
JavaScript objects and functions
JavaScript objects and functionsJavaScript objects and functions
JavaScript objects and functions
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 

Destacado

オブジェクト指向っぽい話
オブジェクト指向っぽい話オブジェクト指向っぽい話
オブジェクト指向っぽい話Tomohiro Shinden
 
Officeで使うPerl Excel編
Officeで使うPerl Excel編Officeで使うPerl Excel編
Officeで使うPerl Excel編risou
 
Perl 6 Object-Oliented Programming
Perl 6 Object-Oliented ProgrammingPerl 6 Object-Oliented Programming
Perl 6 Object-Oliented Programmingrisou
 
北斗の拳の世界をオブジェクト指向で
北斗の拳の世界をオブジェクト指向で北斗の拳の世界をオブジェクト指向で
北斗の拳の世界をオブジェクト指向でyaju88
 
エクストリームエンジニア5
エクストリームエンジニア5エクストリームエンジニア5
エクストリームエンジニア5T-arts
 
第2回勉強会 オブジェクト指向
第2回勉強会 オブジェクト指向第2回勉強会 オブジェクト指向
第2回勉強会 オブジェクト指向hakoika-itwg
 
第3回勉強会 オブジェクト指向
第3回勉強会 オブジェクト指向第3回勉強会 オブジェクト指向
第3回勉強会 オブジェクト指向hakoika-itwg
 

Destacado (7)

オブジェクト指向っぽい話
オブジェクト指向っぽい話オブジェクト指向っぽい話
オブジェクト指向っぽい話
 
Officeで使うPerl Excel編
Officeで使うPerl Excel編Officeで使うPerl Excel編
Officeで使うPerl Excel編
 
Perl 6 Object-Oliented Programming
Perl 6 Object-Oliented ProgrammingPerl 6 Object-Oliented Programming
Perl 6 Object-Oliented Programming
 
北斗の拳の世界をオブジェクト指向で
北斗の拳の世界をオブジェクト指向で北斗の拳の世界をオブジェクト指向で
北斗の拳の世界をオブジェクト指向で
 
エクストリームエンジニア5
エクストリームエンジニア5エクストリームエンジニア5
エクストリームエンジニア5
 
第2回勉強会 オブジェクト指向
第2回勉強会 オブジェクト指向第2回勉強会 オブジェクト指向
第2回勉強会 オブジェクト指向
 
第3回勉強会 オブジェクト指向
第3回勉強会 オブジェクト指向第3回勉強会 オブジェクト指向
第3回勉強会 オブジェクト指向
 

Similar a Jackson beyond JSON: XML, CSV

Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationAjax Experience 2009
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLsintelliyole
 
JSON Fuzzing: New approach to old problems
JSON Fuzzing: New  approach to old problemsJSON Fuzzing: New  approach to old problems
JSON Fuzzing: New approach to old problemstitanlambda
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorialKat Roque
 
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemWprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemSages
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on AndroidSven Haiges
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript WorkshopPamela Fox
 
Scalable and Flexible Machine Learning With Scala @ LinkedIn
Scalable and Flexible Machine Learning With Scala @ LinkedInScalable and Flexible Machine Learning With Scala @ LinkedIn
Scalable and Flexible Machine Learning With Scala @ LinkedInVitaly Gordon
 
Cascading Through Hadoop for the Boulder JUG
Cascading Through Hadoop for the Boulder JUGCascading Through Hadoop for the Boulder JUG
Cascading Through Hadoop for the Boulder JUGMatthew McCullough
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBantoinegirbal
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introductionantoinegirbal
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for youSimon Willison
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side JavascriptJulie Iskander
 
Scripting GeoServer with GeoScript
Scripting GeoServer with GeoScriptScripting GeoServer with GeoScript
Scripting GeoServer with GeoScriptJustin Deoliveira
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)Beau Lebens
 

Similar a Jackson beyond JSON: XML, CSV (20)

Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
 
Json generation
Json generationJson generation
Json generation
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
JSON Fuzzing: New approach to old problems
JSON Fuzzing: New  approach to old problemsJSON Fuzzing: New  approach to old problems
JSON Fuzzing: New approach to old problems
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorial
 
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemWprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
 
Text processing by Rj
Text processing by RjText processing by Rj
Text processing by Rj
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript Workshop
 
Scalable and Flexible Machine Learning With Scala @ LinkedIn
Scalable and Flexible Machine Learning With Scala @ LinkedInScalable and Flexible Machine Learning With Scala @ LinkedIn
Scalable and Flexible Machine Learning With Scala @ LinkedIn
 
JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2
 
Cascading Through Hadoop for the Boulder JUG
Cascading Through Hadoop for the Boulder JUGCascading Through Hadoop for the Boulder JUG
Cascading Through Hadoop for the Boulder JUG
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for you
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Scripting GeoServer with GeoScript
Scripting GeoServer with GeoScriptScripting GeoServer with GeoScript
Scripting GeoServer with GeoScript
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)
 

Último

SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 

Último (20)

SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 

Jackson beyond JSON: XML, CSV

  • 1. Jackson Beyond JSON: Data format modules (XML, CSV) Tatu Saloranta @cowtowncoder tatu@fasterxml.com
  • 2. Beyond JSON: Modules ● Modules extend Jackson functionality o Most new features outside core o Data type modules for supporting Joda, Guava & more o JAX-RS provider for using Jackson in Jersey (et al) o Data formats (this talk) for XML, CSV & more o Other modules  Afterburner (code generation, +30% faster)  Mr Bean (generate impls of interfaces)  JSON Schema generator  JAXB Annotation support
  • 3. Beyond JSON: data formats Extensions to Jackson that o implement Streaming API, to expose token/event streams for reading, token-based writing o sometimes augmented at data-binding (XML) o some use ‘format schema’ helper objects (CSV, Avro, Protobuf) o allow full range of data-binding (read <X> into POJOs, write POJOs as <X>)
  • 4. Beyond JSON: data formats ● Existing data format modules o Binary JSON: Smile, BSON, CBOR o JSON-like: MessagePack, YAML o Textual: CSV, XML o Binary: Avro, Protobuf (soon!) ● Level of support for processing models o All support data-binding o Most support tree model (XML only partial) o All expose streaming (XML partial)
  • 5. non-JSON: from DropWizard Two main ways to use from DropWizard: ● JAX-RS Provider (implement MessageBodyReader/-Writer): ○ JSON, XML, Smile/CBOR (binary JSON) ○ Simply add a Maven dep (or jar), will auto-register ● DIY ○ InputStream for reading ○ StreamingOutput for writing ○ Can also use with JSON
  • 6. From DropWizard: automatic When auto-registering, simply annotate end points with @Produces, @Consumes… @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_XML) … and things will “Just Work”
  • 7. From DropWizard: DIY public class Resource { private final static ObjectMapper mapper = new ObjectMapper(); @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path(“/stuff”) public StreamingOutput getStuff(InputStream input) throws IOException { RequestObject req = mapper.readValue(input, RequestObject.class); final ResponseObject response = …; return new StreamingOutput() { public void write(OutputStream out) throws IOException { mapper.writeValue(out, response); } }; // although most commonly, would wrap in ‘Response’ object } }
  • 8. From DropWizard: DIY Things to note on DIY: ● More work but also more control over configuration, error handling ● StreamingOutput o Good for streaming large responses o Usually wrapped in `Response` (to control headers, response codes) ● InputStream: o Need to invoke Bean Validation manually
  • 9. XML from DropWizard Jackson XML module, JAX-RS provider: https://github.com/FasterXML/jackson-dataformat-xml https://github.com/FasterXML/jackson-jaxrs-providers and optional JAXB annotation support https://github.com/FasterXML/jackson-module-jaxb- annotations
  • 10. XML from DropWizard ● Due to auto-registration, not much to add ● XML-specific annotations: o element vs attribute (Jackson / JAXB annotations) o wrapper vs unwrapped lists ● Jackson vs JAXB o Both work, viable alternatives o Jackson has more powerful POJO handling o JAXB supports “XML-centric” approach o Beware of XML-providers’ “JSON solutions”
  • 11. Jackson XML, DIY If you want to do XML outside of DropWizard: public class Person { @JacksonXmlProperty(isAttribute=true) public int age; public String name; } XmlMapper mapper = new XmlMapper(); String xml = mapper.writeValueAsString(new Person(...)); => <Person age=”25”> <name>Jason Jackson</name> </Person>
  • 12. CSV from DropWizard Jackson CSV module: https://github.com/FasterXML/jackson-dataformat-csv No JAX-RS provider yet (need to figure out how to provide schema); may be added in future.
  • 13. CSV is Different Main differences from other formats: 1. Shallow 2-dimensional (tabular) structure 2. Positional (column 1, 2, …) 3. Only native data type String So how to map to Jackson’s model, POJOs? 1. Handle as “raw” content (List<String[]>) 2. Use schema objects to map position->name
  • 14. CSV usage, “untyped” Simple “untyped” usage: CsvMapper mapper = new CsvMapper(); // or: new ObjectMapper(new CsvFactory()); // read into String[][], or Object[]: String[][] rows = mapper.readValue( "1,xyzn2,abcn", String[][].class); // write similarly; one of: String csv = mapper.writeValueAsString(rows); mapper.writeValue(new File(“output.csv”), rows);
  • 15. CSV usage, “untyped” // or stream through, for large documents MappingIterator<String[]> it = mapper .reader(String[].class) .readValues(csv); while (it.hasNext()) { String[] row = it.nextValue(); // do something with it }
  • 16. CSV usage, POJOs But to get to POJOs, we need CsvSchema: ● simple mapping between column positions, names o manually: CsvSchema schema = CsvSchema.builder().addColumn("firstName") .addColumn("lastName").build() o or from input (first row) -- see next slide o or from Java class: CsvSchema schema = mapper.schemaFor(Pojo.class); ● also contains doc properties: linefeed, column separator... ● NOT formal “CSV Schema”
  • 17. CSV usage, POJOs Typically as simple as: Value[] values = mapper.readerWithSchemaFor(Value.class) .readValue(new File(“input.csv”), Value[].class); // or with MappingIterator mapper.writer(mapper.schemaFor(Value.class) .writeValue(new File(“output.csv”, values); // or using 1st line as header for mapping CsvSchema sch = CsvSchema.emptySchema().withHeader(); values = mapper.reader(Value.class).with(schema) .readValue(source);
  • 18. CSV: variations Many variations: ● some CSV documents define column names in first row (enable/disable via CsvSchema object) ● Different column separator (“TSV” for tab-delimited), line separator ● Optional quoting, and/or escaping ● Null value to output (can not omit -- default “”) ● Settings part of CsvSchema object
  • 19. Beyond JSON, take-away Key takeaway: one tool, usage as close to baseline of JSON as possible. Some follow-up ideas: ● dynamic enabling of binary JSON (HTTP format negotation) ● YAML for configuration (DropWizard already uses Jackson YAML)
  • 20. Beyond JSON, the End That’s All, Folks! Questions? Visit Jackson home at https://github.com/FasterXML/jackson and/or join mailing lists at https://groups.google.com/forum/#!forum/jackson-user