Sunday 22 February 2015

JSON Read & Write with Java (part II)

There are 2 API to perform JSON processing:

  • Object model ( JsonReader/JsonWritter)
  • Streaming (JsonParser/ JsonGenerator)

In the previous post we reviewed the use of JSON processing using object model : create objects with JsonBuilder, and I/O actions with JsonReader and JsonWritter.

Today, we will take a look to the other alternative of processing JSON with JsonParse and JsonGenerator.

Remember that working with Json processing requires to import the package javax.json.* (Available if we are working with Java7 enterprise edition). 

Write with JsonGenerator

The output will be:
{"type":"cheeseburguer","drink":"coke","sides":["chips","salad"]}

Read with JsonParse

The output will be:
type
cheeseburguer
drink
coke
sides
chips
salad



Saturday 21 February 2015

JSON Read & Write with Java


JSON processing in Java is a new specification in JavaEE7 (JSR353).

Overview:
The acronym JSON means Javascript Object notation, and is a format for data interchange, as XML.

Below there is an example of a object : burguermenu represented in both JSON and in XML syntax:

Json syntax
{"menu":  {"type":"cheeseburguer", 
 "drink":"coke",
 "side":["chips","salad"]    
 }
}
XML syntax
<menu>
<type>cheeseburguer</type>    <drink>coke</drink>    <side>chips</side>    
<side>salad</side>
</menu>


To be able with deal with Json directly in Java we will need to import the package javax.json.* (Remeber this package is included in javaEE 7, and is not available in previous version). Alternatively you can download this library from https://jsonp.java.net/

There are two ways to perform I/O actions : ObjectModel and Streaming model
In this post we will explore the first one, with simple examples of reading and writing.

Create Json object in Java:

In the example a json object menu is created with JsonObjectBuilder:


Write a JsonObject --> obtain Json text

The method write the above object using JsonWriter
The output will be:
{"menu":{"type":"cheeseburguer","drinks":"coke","side":["chips","salad"]}}

Read Json text --> obtain a JsonObject

This method read the above output string using JsonReader and it will obtain a JsonObject The output will be:
{"type":"cheeseburguer","drinks":"coke","side":["chips","salad"]}
cheeseburguer
chips
salad


Monday 16 February 2015

Java8 Method Enhancements

Add a body to a method in a interface?
No, because all methods in an interface are abstract.

Add a static method to an interface?? 
No, because static methods cannot be abstract and be overridden in subclasses. 

...but it was before Java8

In Java8 interfaces can contain abstract method, static methods an default methods. 

  • Static and default methods contain implementation.
  • All abstract, default, and static methods in an interface are implicitly public

Default method:

A default method has the word default and it includes a body with the implementation as we can see the method isAvailable in the following example:


When you extend an interface that contains a default method, you can do the following:

- Override : default boolean isAvailable(LocalDate date){//new implementation}
- Make it abstract: boolean isAvailable(LocalDate date);
- Inherit the default method: not including nothing in the extended interface

In the following example the above interface Agenda is extended in the interface WorkAgenda. The default method is overriden because a new implementation is included.  

In case we would prefer to make it abstract we could write the method signature but without body. A class that implements an interface must implement all the abstract methods declared in the interface.


Static Methods
Including static methods in interfaces replaces the idea of defining utilities in separate Utility class.

The static method is part of the interface and not of the implementing class. So the way to call the method is using the interface name. In the following example the static method formatDate is used in the main method. As formatDate is defined in the interface Agenda, the notation is Agenda.formatDate(Date date);

And remember that when an interface is modified to add a new default or static method, the classes that already implemented the interface do not need to be modified.



Sunday 15 February 2015

Java8 DateTime: Conversion and Formatter

We have seen in a recent post how to deal with the new API DateTime in Java8. 
In this post we will explore the formatting options and how to do the conversion from LocalDate (Java8) to Date (legacy API) and viceversa

Example1- How to convert from legacy to new API 
The following examples are the conversion from Date and Calendar to LocalDate.

Example2- How to convert from new API to legacy
Conversion from LocalDate to Date.


Example3- Use of formatter
How to convert from LocalDate to String and viceversa.
It is supported by class Formatter (equivalent of SimpleDateFormat for the legacy API).

Saturday 7 February 2015

Java8 New API Date Time: Overview

The new API Date Time for java 8 is included in the package java.time.

  • Class LocalDate --> date
  • Class LocalTime --> time
  • Class LocalDateTime --> timestamp

The new date and time classes are thread safe (they are not mutable and can be used in multithread). This is an advantage respect from the previous API (Date and Calendar classes).

Now, let's see some easy examples about how to apply this new DateTime classes.

Examples with dates (LocalDate class)
    • get current date
    • set date methods of/with
    • dayOfWeek, yearMonth, year
    • parsing dates
    • calculations (plus/minus)
    • comparing dates
    • Period class



Examples with time (LocalTime class)
    • LocalTime current time, constants
    • Duration : calculation with times
    • LocalDateTime : obtain timestamp