[WSO2Con EU 2017] Manipulating XML, JSON and SQL Data Types with Ballerina

Preview:

Citation preview

Associate Technical Lead, WSO2

Manipulating XML, JSON and SQL Data Types with Ballerina

Shafreen Anfar

Let’s Begin With a Story!Time. Effort. Money.

A Developer’s Story

James (CEO of DataDump Inc.)

William (Conservative, butworking hard)

Henry(Open-minded andworking smart)

James wants his developers to develop a REST service that exposes employee data of his company as a service.

James: Oh! I also want the resource representation to be XML.

A Developer’s Story

• Willam’s approach– Chose Java– Decide on a JAX-RS implementation – Decide on a suitable XML library– Extract data from the database

A Developer’s Story

• Willam’s approach– Using XML library, convert it into

XML– Set it as the response of the service – Deploy it on some web container

A Developer’s Story

Not anymore!Thanks to Ballerina, things are different now.

A Developer’s Story Sounds Complex?

Henry, chose Ballerina and Bob is his uncle!

A Developer’s Story

Henry’s code is not only simple, but also gets the Job done effectively.

A Developer’s Story

Simplicity is something we strive to achieve in Ballerina.

“Simplicity is the ultimateSophistication” - Leonardo da Vinci

A Developer’s Story

Datatable

• Ballerina natively supports handling tabular data structures

• Datatable can be casted to a struct, XML or JSON• Supports data streaming• Work in progress

– Letting users populate datatables and directly write to the DB.

Datatable in Ballerina

XML

• No Need to Worry About Third-Party Libraries!• XML is part of the type system of Ballerina• Provides compile time validation• Ballerina has its extended version of XML spec

– James Clark (https://en.wikipedia.org/wiki/James_Clark_(programmer))

XML in Ballerina

XML in Ballerina : Constructs

XML in Ballerina : Interpolation

XML in Ballerina : Functions

Back to the Story!

Now, James wants William and Henry to develop another service to see which employees are eligible for awards for the current quarter.

But this time James wants it to be mobile friendly.

A Developer’s Story

William goes back to the whiteboard and start designing!

A Developer’s Story

• Decide on a JSON implementation • Use the previously used XML library and

construct payload from the response stream• Do the logic to get the list of eligible

employees• Serialize the XML payload to JSON

A Developer’s Story

A Developer’s Story

Sounds Like a Lot of Work!

Once again, Henry chose Ballerina and Bob is his uncle!

A Developer’s Story

Business logic

JSON

• No need to worry about third-party libraries• JSON is part of the type system of Ballerina• Provides compile time validation• Can easily add, remove, modify• Constrained JSON provides extra validation• Similar to maps and structs in Ballerina

JSON in Ballerina

JSON in Ballerina : Constructs

JSON in Ballerina : Constructs

JSON in Ballerina : Navigating

A Developer’s Story

Who’s the Smarter and Happier DeveloperNow?

Henry, the Ballerina developer ... – Can focus on the exact requirement– Saves time and effort– Ultimately saves money– Writes simple and readable code

A Developer’s Story

A Developer’s Story

wso2.com