Tuesday, December 23, 2014

Serialization and its meaning in JSON

Serialization and its meaning in JSON:


Serialization in JSON is similar to parsing; serialization of JSON can be done in almost all modern programming languages like PHP, ASP.NET etc. Serialization procedure of JSON differs to languages used. JSONpickle is a python library for serialization and Deserialization of complex python objects to and from JSON. Similarly ASP.NET, PHP etc. has their own libraries and methods for serialization of data/objects in JSON.
Syntax:
Example:
PHP Script (json.php)
 <?PHP   
  if($_SERVER['REQUEST_METHOD'] == "POST") {   
   // Gets data from form   
   $name = $_POST['username'];   
   $password = $_POST['password'];   
   // checks if condition matches or not   
   if ($name == $password) {   
    $json = array("status" => 200, "msg" => "Sucessfully logged in");   
   } else {   
    $json = array("status" => 400, "msg" => "Username and Password do not match");   
   }   
  }else {   
   $json = array("status" => 404, "msg" => "Request method not accepted");   
  }   
  /* Output header */   
  header('Content-type: application/json');   
  echo json_encode($json);   
  //echo json_last_error() ; this gievs the last error found by json and 0 value count if not found   
  ?> 

HTML Document (serialization.html)
  <!DOCTYPE html>   
  <html lang="en">   
  <head>   
  <meta charset="utf-8"/>   
  <title>Nirajan Ghimirey's Workshop</title>   
  </head>   
  <body>   
  <div align="center">   
  <h2>Please insert the data to see the encoded JSON message</h2>   
  <form method="post" action="json.php">   
  <input type="text" name="username" placeholder="Username"><br/><br/>   
  <input type="password" name="password" placeholder="Password"><br/><br/>   
  <input type="submit" value="Login">   
  </form>   
  </div>   
  </body>   
  </html>


No comments:

Post a Comment