Sunday, March 20, 2011

Which format is for RESTful service, JSON or XML?

I was facing question about what format I need to use for my RESTful Web service. Some persons said they want JSON. Their reason is very simple, compared with XML, JSON is more compact. Therefore, it can save bandwidth. This sounds reasonable. But, it is not an insightful and completed thought. My points are here,

1) The RESTful web service may not be consumed by JavaScript code. Most likely, it will be consumed by other languages like Java, PHP, ASP .Net etc.

2) The size of data is not only concern for the system. Encoding and decoding performance should be considered too.

3) XML parser has been developed and improved for years. JSON parse engine newer than XML.

4) I saw Zend Framework JSON encode and decode functions are pure PHP code. It's performance needs to be investigated.

5) More importantly, one of reasons for me to chose RESTful is that the service going to be implemented is more about sharing resource than about exposing RPC functions. Concept about Resource need to be always with people who design and implements RESTful Web Service. When we talk about resource, we should not forget that the Internet is invented to share resource among people. Furthermore, hyperlink is invented to be key feature of the Internet. Hyperlink feature makes the Internet to be called as Web though HTTP protocol is not the only thing used to transfer data over the Internet.

We have hypertext, hypermedia etc. Now, we have RESTful Web service. Will we have something like hyper-RESTful? Obviously, the answer will be YES. In fact, this should be one of major reasons for us to use XML as output format as we have XLink in XML already. Below is one sample output of RESTful Web service in XML format.

CUSTOMER
INVOICE
ITEM
PRODUCT

As we can see, this output is not a simple one "page" of XML document. It contains hyperlinks to other resources, which could be another RESTful Web service. With this in mind, RESTful designer can borrow methodology of creating Web site to design and "expanding" their RESTful service. That is, we do not need to develop a dedicated service, which will do every thing by itself and it wont share resource with other service. One RESTful web service could be implemented as a piece of "page". Then, another RESTful web service can be composed by using multiple RESTful web service. This architecture will give RESTful Web service more flexibility in terms of deployment and network optimizing.

Won't we lost this great hyperlink feature, which is nature for the Internet if we use JSON? I think 1) JSON may not give whole system better performance if we consider not only data size but also encoding/decoding speed and memory usage. 2) XML will give us great feature of hyplelink from the Web. XLink is just the technique from XML world.

Below is a picture linked from a blog post Native JSON Support is Required.
It is an article written and experiment made in 2007. Now, the things has been different as modern browsers have native JSON support already. However, it is still helpful to remind people who know JSON has more compact size than XML and missed other important factors in the system.

JSON or XML? I think there are different answers for different software system. However, it is thoughtless to assert JSON is better than XML just because of the uncompressed data size. Back to the topic, I will say it is good to use XML as output format of RESTful when the service is not consumed by JavaScript running in Web browser. Of course, it won't be difficult to make RESTful service to be configurable to output XML or JSON.

Wednesday, March 16, 2011

buit-in datatype supported by DB2 for i5/OS and MySQL

To complete one of my work, I need to know list of data type supported by RDBMS. So, I wrote a simple Java application to fetch it. Below is java code and output.

Db2MetaData.java using jt400.jar. (This is just a quick work)
package jia.blog.db2;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Yiyu Jia
 */
public class Db2MetaData {
   Connection conn = null;
   DatabaseMetaData dmd = null;

    public Db2MetaData() {
     
    }

  public void openConnection () {

      try {                  
          String url = "jdbc:as400://172.0.0.1"; //change ip address
          DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
          conn = DriverManager.getConnection(url, "accountName", "password");
      }
    catch (Exception e) {
         Logger.getLogger(Db2MetaData.class.getName()).log(Level.SEVERE, null, e);
       }
  }  

 
  public void closeConnection () {

    if (conn != null) {
       try {
           conn.close();           
           System.out.println("Connection closed");
           }
       catch(SQLException e) {
          Logger.getLogger(Db2MetaData.class.getName()).log(Level.SEVERE, null, e);
          }
       }
  }  


    public void displayMaxConnection() {
        try {
            DatabaseMetaData metadata = conn.getMetaData();
            int maxConnection = metadata.getMaxConnections();
            System.out.println("Maximum Connection = " + maxConnection);
        } catch (SQLException ex) {
            Logger.getLogger(Db2MetaData.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void displayTypeInfo() {
        try {
            DatabaseMetaData metadata = conn.getMetaData();
            ResultSet resultSet = metadata.getTypeInfo();
            while (resultSet.next()) {
                String typeName = resultSet.getString("TYPE_NAME");
                System.out.println(typeName);
            }
            resultSet.close();
        } catch (SQLException ex) {
            Logger.getLogger(Db2MetaData.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

  public static void main(String args[])  {
      Db2MetaData db2Meta = new Db2MetaData();
      db2Meta.openConnection();
      db2Meta.displayMaxConnection();
      db2Meta.displayTypeInfo();
      db2Meta.closeConnection();
  }

}

Supported data type printed by JDBC
DB2 i5 R6V1 MySQL 5.5.8

CHAR
CHAR() FOR BIT DATA
GRAPHIC
LONG VARGRAPHIC
LONG VARCHAR FOR BIT DATA
VARCHAR
VARCHAR() FOR BIT DATA
VARGRAPHIC
DATE
TIMESTAMP
TIME

DECIMAL
DOUBLE
FLOAT
INTEGER
NUMERIC
REAL
SMALLINT
BIGINT
ROWID
DECFLOAT
DATALINK
BLOB
CLOB
DBCLOB

BIT
BOOL
TINYINT
TINYINT UNSIGNED
BIGINT
BIGINT UNSIGNED
LONG VARBINARY
MEDIUMBLOB
LONGBLOB
BLOB
TINYBLOB
VARBINARY
BINARY
LONG VARCHAR
MEDIUMTEXT
LONGTEXT
TEXT
TINYTEXT
CHAR
NUMERIC
DECIMAL
INTEGER
INTEGER UNSIGNED
INT
INT UNSIGNED
MEDIUMINT
MEDIUMINT UNSIGNED
SMALLINT
SMALLINT UNSIGNED
FLOAT
DOUBLE
DOUBLE PRECISION
REAL
VARCHAR
ENUM
SET
DATE
TIME
DATETIME
TIMESTAMP
Below is a picture linked from IBM web site. It listed out built-in data type supported by DB2 for i5/OS RvV4.source
Here are links for DB2 data type to jdbc data type mapping and MySQL data type to JDBC data type mapping