Data Serialization and Storage

Web Programming Course

SUT • Fall 2018

Outline

  • Serialization

    • XML

    • JSON

    • ...

  • Data Storage

    • Server side Storage

    • SQL

Serialization

XML

  • XML is a markup language for encoding documents in a format that is both human-readable and machine-readable
  • Is designed to transport and store data
  • Emphasizes simplicity, generality, and usability over the Internet
  • Has strong support via Unicode for the languages of the world

XML

  • A simple XML example

 

 

 

<?xml version="1.0"?>
<message>
    <from>Hassan</from>
    <to>Hossein</to>
    <body>Please give me a call!</body>
</message>

XML

  • Another example:

 

 

 

<?xml version="1.0"?>
<books>
    <book>
        <title>Maktub</title>
        <author>Paulo Coelho</author>
    </book>
    <book>
        <title>Never Crashed!</title>
        <author>Microsoft</author>
    </book>
</books>

XML versus HTML

  • XML and HTML are both markup languages

  • HTML is for displaying data, while XML is for

    describing data

  • XML syntax differences

    • New tags may be defined at will
    • Tags may be nested to arbitrary depth
    • May contain an optional description of its grammar

 

JSON

  • JSON stands for JavaScript Object Notation
  • It is a lightweight text-data interchange

    format, commonly used as an alternative to XML

  • JSON is smaller, faster and easier to parse

  • Although JSON uses JavaScript syntax, it is still language and platform independent.

JSON

{
    "message": {
        "from": "Hassan",
        "to": "Hossein",
        "body": "Please give me a call!"
    }
}
{
    "books": [
        {
            "title": "Maktub", 
            "author": "Paulo Coelho"
        },
        {
            "title": "Crashed!",
             "author": "Microsoft"
        }
    ]
}

References

Data Storage

Introduction

  • Any (web) application needs persistence storage
  • There are three general storage strategies:
    • server-side storage
    • client-side storage
    • a hybrid strategy
Client-Side Storage
  • Client-side data is stored locally within the user's browser
  • A web page can only access data stored by itself
  • For a long time, cookies were the only option to store data locally
  • HTML5 introduced several new web storage options
    • Session Storage
    • Local Storage
Server-Side Storage
  • Server-side data is usually stored within a file or a

    database system

  • For large data, database systems are preferable

    over plain files

  • Database Management Systems (DBMSs)

    provide an efficient way to store and retrieve

    data

Text

  • A relational Database Written in C
  • Compatible with SQL language
  • Simple
  • Zero dependency
  • Zero configuration
$ sudo apt-get install sqlite
.

$ sqlite3 testDB.db
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

sqlite>.databases
seq  name             file
---  ---------------  ----------------------
0    main             /home/sqlite/testDB.db

Relational Databases

  • Components:
    • Database
    • Table
    • Row
    • Columns
  • Operations
    • create/drop database/tables/index
    • insert/update/delete table rows
    • select a set of rows

Table Operation

sqlite> CREATE TABLE COMPANY(
   ID             INT     PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

sqlite>.tables
COMPANY 

sqlite>.schema COMPANY
CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

sqlite>.tables
COMPANY 

sqlite>DROP TABLE COMPANY;

sqlite>.tables
sqlite>

Row Operation (Insert)

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (5, 'David', 27, 'Texas', 85000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (6, 'Kim', 22, 'South-Hall', 45000.00 );

INSERT INTO COMPANY VALUES (7, 'James', 24, 'Houston', 10000.00 );

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

Row Operation (Delete)

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

sqlite> DELETE FROM COMPANY WHERE ID = 7;

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0

Row Operation (Update)

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

sqlite> UPDATE COMPANY SET ADDRESS = 'Texas' WHERE ID = 6;

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          Texas       45000.0
7           James       24          Houston     10000.0

Row Operation (Select)

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0

sqlite> SELECT AVG(SALARY) FROM COMPANY WHERE AGE >= 25

AVG
---------
46250


Title Text

Data Serialization and storage

By Behnam Hatami

Data Serialization and storage

Data serialization and storage / Web Programming Course @ SUT, Fall 2018

  • 1,317