serialisasi data: json, json schema, dan bson

Download Serialisasi Data: JSON, JSON Schema, dan BSON

If you can't read please download the document

Upload: bambang-purnomosidi-d-p

Post on 14-Apr-2017

333 views

Category:

Internet


4 download

TRANSCRIPT

Format Serialisasi Data: JSON dan BSON (+Schema)

Bambang Purnomosidi D. P.http://bpdp.xyz

Agenda

Serialisasi Data dan Stream

JSON

JSON Schema

BSON

Serialisasi Data dan Stream

Serialisasi: proses menterjemahkan struktur data (atau object state dalam OOP) ke dalam format yang bisa disimpan (ke file atau memory) atau ditransmisikan melalui jaringan. Sering juga disebut marshalling. Contoh: proses transmisi data JSON melalui jaringan TCP/IP dalam mekanisme request-response dari aplikasi web.

Deserialisasi: proses ekstraksi dari penyimpanan atau pengiriman byte data ke suatu struktur data. Sering juga disebut demarshalling. Contoh: membaca hasil data stream pemrosesan dari aplikasi web.

Stream: elemen data yang tersedia secara kontinyu seiring berjalan waktu, mulai dari awal waktu tertentu sampai akhir waktu tertentu.

JSON (http://json.org)

Singkatan dari Javascript Object Notation, pertama kali dibuat oleh Douglas Crockford.

Standar: RFC7159 (https://tools.ietf.org/pdf/rfc7159.pdf) dan ECMA-404 (http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf)

Meski mengandung nama Javascript, format ini programming language independent, implementasi terdapat pada hampir semua mainstream programming language: Java, C#, Javascript, C, C++, Go, Rust, /etc

MIME type: application/json, file: .json (tidak harus).

Tipe data dasar: angka (notasi e => 6.022E23 atau 6.022e23 utk 6.02210 pangkat 23, integer, floating-point), string, boolean, array, object / associative array, null.

Contoh JSON (https://en.wikipedia.org/wiki/JSON)

{ "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" } ], "children": [], "spouse": null}

JSON dan NodeJS

var fs = require('fs');

fs.readFile(__dirname + '/person.json', function(err, data) { var jsonObj = JSON.parse(data); console.log("Nama = " + jsonObj.firstName + " " + jsonObj.lastName); console.log("Usia = " + jsonObj.age); if (jsonObj.isAlive) { console.log("Masih hidup"); } else { console.log("Sudah meninggal"); } if (jsonObj.spouse == null) { console.log("Belum menikah"); } else { console.log("Sudah menikah dengan " + jsonObj.spouse); } console.log("Jumlah anak = " + jsonObj.children.length); var jmlTelpon = jsonObj.phoneNumbers.length; for (var i = 0; i < jmlTelpon; i++) { var z = i+1; console.log('Tipe: ' + jsonObj.phoneNumbers[i].type); console.log('Nomor: ' + jsonObj.phoneNumbers[i].number); }});

Nama = John SmithUsia = 25Masih hidupBelum menikahJumlah anak = 0Tipe: homeNomor: 212 555-1234Tipe: officeNomor: 646 555-4567

JSON Schema (http://json-schema.org)

Mendefinisikan struktur dari suatu serialisasi JSON untuk menentukan validitas serialisasi tersebut.

Internet draft 4: http://tools.ietf.org/html/draft-zyp-json-schema-04

JSON Schema (taken from https://en.wikipedia.org/wiki/JSON)

{ "$schema": "http://json-schema.org/schema#", "title": "Product", "type": "object", "required": ["id", "name", "price"], "properties": { "id": { "type": "number", "description": "Product identifier" }, "name": { "type": "string", "description": "Name of the product" }, "price": { "type": "number", "minimum": 0 }, "tags": { "type": "array", "items": { "type": "string" } }, "stock": { "type": "object", "properties": { "warehouse": { "type": "number" }, "retail": { "type": "number" } } } }}}

{ "id": 1, "name": "Foo", "price": 123, "tags": [ "Bar", "Eek" ], "stock": { "warehouse": 300, "retail": 20 }}

BSON (Binary JSON - http://bsonspec.org/)

Serialisasi JSON yang dikodekan dalam format biner

Dibuat oleh MongoDB dan biasanya memang hanya digunakan di dalam MongoDB

Mengapa ada BSON? (1) Traversability (2) Cepat untuk encode maupun decode (3) Tipe data format tambahan BinData dan Date.

{"hello": "world"} \x16\x00\x00\x00 // total document size \x02 // 0x02 = type String hello\x00 // field name \x06\x00\x00\x00world\x00 // field value \x00 // 0x00 = type EOO ('end of object')

{"BSON": ["awesome", 5.05, 1986]} \x31\x00\x00\x00 \x04BSON\x00 \x26\x00\x00\x00 \x02\x30\x00\x08\x00\x00\x00awesome\x00 \x01\x31\x00\x33\x33\x33\x33\x33\x33\x14\x40 \x10\x32\x00\xc2\x07\x00\x00 \x00 \x00

Penggunaan JSON

JSON-RPC

AJAX => AJAJ

RDF/JSON

JSON-LD

File konfigurasi

Output dari pemrosesan di aplikasi web

Data