Вход на сайт
Воспоминание сериализация 26.06.2025
29
NEW 5 дней назад, 20:49
Всем привет! Сегодня хотел бы вспомнить про сериализацию на разных языках программирования.
На бейсик в Qbasic пример serdemo.bas:
TYPE DATASER gender AS STRING * 1 firstname AS STRING * 80 lastname AS STRING * 80 birthday AS STRING * 10 END TYPE DIM ds AS DATASER ds.gender = "M" ds.firstname = "Anton" ds.lastname = "Popov" ds.birthday = "01.01.1982" OPEN "demo.bin" FOR RANDOM AS #1 LEN = LEN(ds) PUT #1, 1, ds CLOSE #1 OPEN "demo.bin" FOR RANDOM AS #1 LEN = LEN(ds) GET #1, 1, ds PRINT ds.gender; ds.firstname; ds.lastname; ds.birthday CLOSE 1
Результат:
M Anton Popov 01.01.1982
На Си пример serdemo.c:
#include <stdio.h> #include <stdlib.h> typedef struct { unsigned short int day; unsigned short int month; unsigned int year; } DATE; typedef struct { long id; char gender; char firstname[80]; char lastname[80]; DATE birthday; } PERSON; int main(int argc, char *argv[]) { FILE *f; PERSON p; strcpy(p.firstname, "Anton"); strcpy(p.lastname, "Popov"); p.gender = 'M'; p.id = 1; p.birthday.day = 1; p.birthday.month = 1; p.birthday.year = 1982; f = fopen("serdemo.bin", "w+b"); fwrite(&p, sizeof(p), 1, f); fclose(f); f = fopen("serdemo.bin", "r+b"); fread(&p, sizeof(p), 1, f); printf("\n%ld %c %s %s d.d.d", p.id, p.gender, p.firstname, p.lastname, p.birthday.day, p.birthday.month, p.birthday.year); fclose(f); return 0; }
Результат:
1 M Anton Popov 01.01.1982
На PHP index.php:
<!DOCTYPE html> <html> <head> <title>Пример сериализации/десериализации</title> </head> <body> <p> <?php class Date { private $int_day; private $int_month; private $int_year; public function __construct($int_day, $int_month, $int_year) { $this->int_day = $int_day; $this->int_month = $int_month; $this->int_year = $int_year; } public function set_day($int_day) { $this->int_day = $int_day; } public function get_day() { return $this->int_day; } public function set_month($int_month) { $this->int_month = $int_month; } public function get_month() { return $this->int_month; } public function set_year($int_year) { $this->int_year = $int_year; } public function get_year() { return $this->int_year; } public function toString() { return sprintf('d.d.d', $this->int_day, $this->int_month, $this->int_year); } } class Person { private $lng_id; private $ch_gender; private $str_firstname; private $str_lastname; private $obj_birthday; public function __construct($lng_id, $ch_gender, $str_firstname, $str_lastname, $obj_birthday) { $this->lng_id = $lng_id; $this->ch_gender = $ch_gender; $this->str_firstname = $str_firstname; $this->str_lastname = $str_lastname; $this->obj_birthday = $obj_birthday; } public function set_id($lng_id) { $this->lng_id = $lng_id; } public function get_id() { return $this->lng_id; } public function set_gender($ch_gender) { $this->ch_gender = $ch_gender; } public function get_gender() { return $this->ch_gender; } public function set_firstname($str_firstname) { $this->firstname = $str_firstname; } public function get_firstname() { return $this->firstname; } public function set_lastname($str_lastname) { $this->str_lastname = $str_lastname; } public function get_lastname() { return $this->str_lastname; } public function set_birthday($obj_birthday) { $this->obj_birthday = $obj_birthday; } public function get_birthday() { return $this->obj_birthday; } public function toString() { return sprintf('%ld %s %s %s %s', $this->lng_id, $this->ch_gender, $this->str_firstname, $this->str_lastname, $this->obj_birthday->toString()); } } $p = new Person(1, 'M', 'Anton', 'Popov', new Date(1, 1, 1982)); file_put_contents('demo.bin', serialize($p)); $str_ser = file_get_contents('demo.bin'); $p2 = unserialize($str_ser); echo $p2->toString(); ?> </p> </body> </html>
Результат:
<!DOCTYPE html> <html> <head> <title>Пример сериализации/десериализации</title> </head> <body> <p>1 M Anton Popov 01.01.1982</p> </body> </html>
1 M Anton Popov 01.01.1982
Если я кому-то отвечаю, это не значит что я ему симпатизирую, каждый остаётся при своём мнение
5 дней назад, 20:53
в ответ uscheswoi_82 5 дней назад, 20:49
Сериализация подходит если нет базы данных. Допустим я приводил пример см. Телефонная книжка Swing, JNI, OpenJDK v2 как сделать записную книжку. Я там на Си делал сериализацию.
Если я кому-то отвечаю, это не значит что я ему симпатизирую, каждый остаётся при своём мнение