MuleSoft Java-JSON
2 Agenda • Introduction to JSON • JSON and Java • Jackson Annotations • Introduction to XML • XML to JSON • JSON in Mule
• 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
• Data is in name/value pairs • Data is separated by commas • Curly braces hold objects • Square brackets hold arrays 4 Syntax of JSON
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
• 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
• 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
Mapping 8 JSON Java string java.lang.String number java.lang.Number true|false ava.lang.Boolean null null array java.util.List object java.util.Map
Encoding JSON in Java 9
Decoding Java in JSON 10
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
Parsing JSON • Streaming • Tree Traversing • Data Binding 12
JACKSON ANNOTATIONS 13
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
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
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
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
Jackson Serialization Annotations • @JsonAnyGetter • @JsonGetter • @JsonPropertyOrder • @JsonRawValue • @JsonValue • @JsonRootName • @JsonSerialize 18
Jackson Deserialization Annotations • @JsonCreator • @JsonProperty • @JacksonInject • @JsonAnySetter • @JsonSetter • @JsonDeserialize 19
Jackson Property Inclusion Annotations • @JsonIgnoreProperties • @JsonIgnore • @JsonIgnoreType • @JsonInclude • @JsonAutoDetect 20
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
Jackson General Annotations • @JsonProperty • @JsonFormat • @JsonUnwrapped • @JsonView • @JsonManagedReference • @JsonBackReference • @JsonIdentityInfo • @JsonFilter 22
JSON in Mule • JSON Converters • Configuring the components • Mix ins • Inbuilt Jackson API in Mule. 12/16/2015 23
References • www.json.org • http://www.w3schools.com/json/json_intro.asp • http://www.tutorialspoint.com/json/index.htm • http://www.tutorialspoint.com/jackson/jackson_quic k_guide.htm • http://www.baeldung.com/jackson-annotations 24

JSON Processing and mule

  • 1.
  • 2.
    2 Agenda • Introduction toJSON • 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 isin 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 inAJAX – Part of HTML Tags – <html>... <script> var data = JSONdata; </script>... </html> • JSON in JavaScript – XMLHttpRequest – responseData = eval('(' + responseText + ')'); 7 JSON in AJAX
  • 8.
    Mapping 8 JSON Java string java.lang.String numberjava.lang.Number true|false ava.lang.Boolean null null array java.util.List object java.util.Map
  • 9.
  • 10.
  • 11.
    JSON Schema • JSONSchema 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
  • 12.
    Parsing JSON • Streaming •Tree Traversing • Data Binding 12
  • 13.
  • 14.
    Jackson Overview • Jacksonis 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 JacksonEnvironment • 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 - DataBinding • 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
  • 18.
    Jackson Serialization Annotations •@JsonAnyGetter • @JsonGetter • @JsonPropertyOrder • @JsonRawValue • @JsonValue • @JsonRootName • @JsonSerialize 18
  • 19.
    Jackson Deserialization Annotations •@JsonCreator • @JsonProperty • @JacksonInject • @JsonAnySetter • @JsonSetter • @JsonDeserialize 19
  • 20.
    Jackson Property Inclusion Annotations •@JsonIgnoreProperties • @JsonIgnore • @JsonIgnoreType • @JsonInclude • @JsonAutoDetect 20
  • 21.
    Jackson Polymorphic Type HandlingAnnotations • @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
  • 22.
    Jackson General Annotations •@JsonProperty • @JsonFormat • @JsonUnwrapped • @JsonView • @JsonManagedReference • @JsonBackReference • @JsonIdentityInfo • @JsonFilter 22
  • 23.
    JSON in Mule •JSON Converters • Configuring the components • Mix ins • Inbuilt Jackson API in Mule. 12/16/2015 23
  • 24.
    References • www.json.org • http://www.w3schools.com/json/json_intro.asp •http://www.tutorialspoint.com/json/index.htm • http://www.tutorialspoint.com/jackson/jackson_quic k_guide.htm • http://www.baeldung.com/jackson-annotations 24