2. 2
Agenda
• Introduction to JSON
• JSON and Java
• Jackson Annotations
• Introduction to XML
• XML to JSON
• JSON in Mule
3. • JSON – Java Script Object Notation
• JSON is a data interchange format
• Interactive Web 2.0 applications, no more use
page replacement. Data transfer without
refreshing a page.
• The most important aspects of data transfer are
simplicity, extensibility, interoperability,
openness and human readability
• Key idea in AJAX – Asynchronous Java Script and
XML.
3
Introduction to JSON
4. • Data is in name/value pairs
• Data is separated by commas
• Curly braces hold objects
• Square brackets hold arrays
4
Syntax of JSON
5. Simplicity of JSON
• JSON is a subset of Java Script. JSON can be
parsed by a Java Script parser.
• It can represent either complex or simple data
as it has data types
• They are Strings, Number, Boolean, Objects
and Arrays
5
6. • Array
– ["Sunday", "Monday", "Tuesday", "Wednesday“]
– "employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
• Object
– { “firstname": “Santhosh ", “lastname": “Gowd”} All data
types are intuitive and similar to other programming
languages
• Also compatible with other languages like C, C++,
C#, ColdFusion, Python and many more.
6
JSON Compatibility
7. • JSON in AJAX
– Part of HTML Tags
– <html>... <script> var data = JSONdata; </script>...
</html>
• JSON in JavaScript
– XMLHttpRequest
– responseData = eval('(' + responseText + ')');
7
JSON in AJAX
11. JSON Schema
• JSON Schema is a specification for JSON based format
for defining the structure of JSON data
• It was written under IETF draft which expired in 2011
– Describes your existing data format.
– Clear, human- and machine-readable documentation.
– Complete structural validation, useful for automated
testing.
– Complete structural validation, validating client-submitted
data.
• Currently the most complete and compliant JSON
Schema validator available is JSV
11
14. Jackson Overview
• Jackson is a simple Java-based library to serialize Java
objects to JSON and vice versa
• Features of Jackson
– Easy to use − Jackson API provides a high-level facade to
simplify commonly used use-cases.
– No need to create mapping − Jackson API provides default
mapping for most of the objects to be serialized.
– Performance − Jackson is quite fast, consumes less memory
space, and is suitable for large object graphs or systems.
– Clean JSON − Jackson creates clean and compact JSON results
which are easy to read.
– No Dependency − Jackson library does not require any other
library apart from JDK.
– Open Source − Jackson library is open source and free to use
14
15. Set up Jackson Environment
• Nothing specific for Jackson
– Install JDK and make sure path and classpath are
set
– Download jackson-all-1.9.0.jar
– Make sure this file is in the classpath in libraries
15
16. Follow these steps
• Step1 : Create ObjectMapper Object
– ObjectMapper mapper = new ObjectMapper();
• Step 2: Deserialize JSON to Object
– //Object to JSON Conversion Student student =
mapper.readValue(jsonString, Student.class);
• Step 3: Serialize Object to JSON
– jsonString = mapper.writeValueAsString(student);
16
17. Jackson - Data Binding
• Simple Data Binding
– It converts JSON to and from Java Maps, Lists,
Strings, Numbers, Booleans, and null objects.
• Full Data Binding
– It converts JSON to and from any Java type.
17
21. Jackson Polymorphic Type
Handling Annotations
• @JsonTypeInfo is used to indicate details of
what type information is included in
serialization
• @JsonSubTypes is used to indicate sub-types
of annotated type
• @JsonTypeName is used to define logical type
name to use for annotated class
21