parsing xml & json in apex

25
Handling XML / JSON in Apex t: @abhinavguptas www: tgerm.com

Upload: abhinav-gupta

Post on 22-May-2015

7.114 views

Category:

Technology


9 download

DESCRIPTION

Slides about tips of XML and JSON parsing in Apex. These slides were used in Force.com chennai meetup.

TRANSCRIPT

Page 1: Parsing XML & JSON in Apex

Handling XML / JSON in Apex

t: @abhinavguptas

www: tgerm.com

Page 2: Parsing XML & JSON in Apex

Agenda

Motivation

Classic approaches

Better approaches

Make XML/JSON Parsing Simple

Page 3: Parsing XML & JSON in Apex

Motivation

Developers not aware of recent platform changes.

Google gives pointers to obsolete APIs.

Obsolete APIs are slow and hurt on governor limits like “script statements consumption”

New APIs use platform capabilities for resource hungry tasks like parsing.

Why we are discussing this ?

Page 4: Parsing XML & JSON in Apex

Native Lib vs

Apex Lib

Native Lib ?

Provided by Force.com platform aka System libraries, like XMLStreamReader

Doesn’t hurt on resources like script statements consumption.

Apex Lib ?

Wrapper on Native Lib, like XMLDOM.cls

Written from scratch in Apex only, like JSONObject.cls

Page 5: Parsing XML & JSON in Apex

XML Parsing

Page 6: Parsing XML & JSON in Apex

Sample XML

<books>

<book author="Chatty">Foo bar</book>

<book author="Sassy">Baz </ book>

</books>

Page 7: Parsing XML & JSON in Apex

Apex Model

public class Book {

String name;

String author;

}

Parse XML to this Model

Page 8: Parsing XML & JSON in Apex

Classic Approaches

XMLStream Classes

Native Lib

Stream based approach to parse XML

XMLDom.cls (OpenSource)

Apex Lib, wrapper on above XMLStream classes

Create in memory DOM for XML Structure.

Written by Ron Hess (Salesforce)

Page 9: Parsing XML & JSON in Apex

BetterApproaches

Dom.Document/XmlNode Classes

Native Lib

Came in Spring’10 release

DOM based approach to parse XML

FastXmlDom (OpenSource)

Apex Lib, wrapper on above Dom.XmlNode classes

Exposes pretty common DOM API for XML Parsing

Tested & used in many projects.

Page 10: Parsing XML & JSON in Apex

Sample Code

Over to sublime code snippet

Lets see some code about all of these approaches

Page 11: Parsing XML & JSON in Apex

XmlStream Classes

Pros

Native Lib (Not an Apex implementation)

Heap/Memory efficient like usual stream parsers.

Good for big XML string, parse only the required part of XML.

Cons

Complex code to parse XML.

Not so developer friendly

Sometimes can consume lots of script statements in iterations for big XMLs.

Pros/Cons

Page 12: Parsing XML & JSON in Apex

XMLDOM.cls

Pros

Simple W3C DOM library, which developers are used to of using in HTML DOM as well.

Simple and more readable code for XML Parsing.

Cons

Creates in memory DOM for complete XML structure, so heavy on heap

Consumes too many script statements in the above process.

Not suitable for large XML parsing.

Page 13: Parsing XML & JSON in Apex

Dom class Document &

XmlNode

Pros

Native DOM Lib

Relatively simpler to use as compared to XMLStreamReader.

Consumes least script statements.

Cons

DOM API is not following w3c model, so learning curve for developers.

Mandates namespace usage, that complicates simple xml parsing.

Page 14: Parsing XML & JSON in Apex

FastXMLDom

Pros

Exposes W3C DOM API (no learning curve for developers)

Simpler to use as compared to XMLStreamReader and DOM Classes.

Consumes less script statements.

Cons

An Apex Lib, so consumes a bit more script statements and heap as compared to Dom.XmlNodePros/Cons

Page 15: Parsing XML & JSON in Apex

JSON Parsing

Page 16: Parsing XML & JSON in Apex

Classic Approaches

JSONObject.cls

Apex Lib

Tokenizes the string to create in memory JSON Structure

Written by Ron Hess(Salesforce)

System.JSONParser

Native lib

Streaming and token based parsing of JSON

Page 17: Parsing XML & JSON in Apex

BetterApproaches

JSON

Native Lib

Mapped strong and loose parsing with Apex UDT(User Defined Types) and Collections like Maps.

Page 19: Parsing XML & JSON in Apex

JSONObject.cls

Pros

None as of now

Cons

Consumes script statements and heap for large JSON files

Not as stable as compared to Native libs.

Page 20: Parsing XML & JSON in Apex

JSONParser

Pros

Native lib, hurts less on resources.

Good option when JSON automated serialization/deserialization fails.

Came with Winter’12 release

Cons

Complex code and less developer friendly way to parse.

Page 21: Parsing XML & JSON in Apex

JSON

Pros

Native lib, hurts less on resources.

Serialize/De-serialize big JSON strings in one line of code.

Easy transformation of JSON from String > UDT and vice versa

Gives option of loose parsing to Maps i.e. deserializeUntyped()

Came with winter’12 release

Improved a bit with every next release

Cons

Special care if required if JSON contains reserved words or conflicting stuff with Apex.

Page 22: Parsing XML & JSON in Apex

General Tips

Searching for solutions in this order

Consult platform guides, like apex dev guide.

developer.force.com

Salesforce.stackexchange.com

Google

Stay on latest and greatest API

Don’t download pdf for documentation use online guides.

Use latest API version of Apex class and VF page when creating new ones.

Page 23: Parsing XML & JSON in Apex

Q & A

Page 25: Parsing XML & JSON in Apex

Thanks !@abhinavguptas

www.tgerm.com