- FlatBuffers 教程
- FlatBuffers - 首頁
- FlatBuffers - 簡介
- FlatBuffers - Schema
- FlatBuffers - 結構
- FlatBuffers - 表格
- FlatBuffers - 字串
- FlatBuffers - 數字
- FlatBuffers - 布林值
- FlatBuffers - 列舉
- FlatBuffers - 向量
- FlatBuffers - 結構體
- FlatBuffers - 聯合
- FlatBuffers - 巢狀表格
- FlatBuffers - 預設值
- FlatBuffers - JSON 轉二進位制
- FlatBuffers - 二進位制轉 JSON
- FlatBuffers - 可變緩衝區
- FlatBuffers - 向後相容性
- FlatBuffers - 語言獨立性
- FlatBuffers 有用資源
- FlatBuffers - 快速指南
- FlatBuffers - 有用資源
- FlatBuffers - 討論
FlatBuffers - 預設值
概述
我們已經在之前的例子中看到了如何序列化和反序列化FlatBuffers中的各種型別。如果沒有指定任何值,則會儲存預設值。如果我們為變數指定相同的預設值,則FlatBuffers不會分配額外的空間。
FlatBuffers根據下表支援其資料型別的預設值:
| 資料型別 | 預設值 |
|---|---|
| int16 / short / int / long | 0 |
| Float/double | 0.0 |
| 字串 | 空字串 |
| 布林值 | False |
| 列舉 | 第一個列舉項,即“index=0”的項 |
| 向量 | 空列表 |
| 巢狀類 | null |
因此,如果未指定這些資料型別的資料,則它們將採用上述預設值。現在,讓我們繼續我們的劇院示例來演示其工作原理。
在這個例子中,我們將讓所有欄位都使用預設值。唯一指定的欄位將是劇院的名稱。
繼續我們來自Flat Buffers - 字串章節的劇院示例,以下是我們需要使用的語法,以指示FlatBuffers我們將建立各種資料型別:
theater.fbs
namespace com.tutorialspoint.theater;
enum PAYMENT_SYSTEM: int { CASH = 0, CREDIT_CARD = 1, DEBIT_CARD, APP = 3 }
table Theater {
name:string;
address:string;
total_capacity:short;
mobile:int;
base_ticket_price:float;
drive_in:bool;
payment:PAYMENT_SYSTEM;
snacks:[string];
owner: TheaterOwner;
}
table TheaterOwner {
name:string;
address:string;
}
root_type Theater;
現在我們的劇院表格包含多個屬性。
從fbs檔案建立Java類
要使用FlatBuffers,我們現在必須使用flatc二進位制檔案從此“.fbs”檔案建立所需的類。讓我們看看如何做到這一點:
flatc --java theater.fbs
這將在當前目錄的com > tutorialspoint > theater資料夾中建立一個Theater、TheaterOwner和PAYMENT_SYSTEM類。我們在應用程式中使用此類,就像在Flat Buffers - Schema章節中所做的那樣。
使用從fbs檔案建立的Java類
TheaterWriter.java
package com.tutorialspoint.theater;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.google.flatbuffers.FlatBufferBuilder;
public class TheaterWriter {
public static void main(String[] args) throws FileNotFoundException, IOException {
// create a flat buffer builder
// it will be used to create Theater FlatBuffer
FlatBufferBuilder builder = new FlatBufferBuilder(1024);
// create offset for name
int name = builder.createString("Mery");
// create theater FlatBuffers using startTheater() method
Theater.startTheater(builder);
// add details to the Theater FlatBuffer
Theater.addName(builder, name);
// mark end of data being entered in Greet FlatBuffer
int theater = Theater.endTheater(builder);
// finish the builder
builder.finish(theater);
// get the bytes to be stored
byte[] data = builder.sizedByteArray();
String filename = "theater_flatbuffers_output";
System.out.println("Saving theater to file: " + filename);
// write the builder content to the file named theater_flatbuffers_output
try(FileOutputStream output = new FileOutputStream(filename)){
output.write(data);
}
System.out.println("Saved theater with following data to disk: \n" + theater);
}
}
接下來,我們將有一個讀取器來讀取劇院資訊:
TheaterReader.java
package com.tutorialspoint.theater;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
public class TheaterReader {
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = "theater_flatbuffers_output";
System.out.println("Reading from file " + filename);
try(FileInputStream input = new FileInputStream(filename)) {
// get the serialized data
byte[] data = input.readAllBytes();
ByteBuffer buf = ByteBuffer.wrap(data);
// read the root object in serialized data
Theater theater = Theater.getRootAsTheater(buf);
// print theater values
System.out.println("Name: " + theater.name());
System.out.println("Address: " + theater.address());
System.out.println("Total Capacity: " + theater.totalCapacity());
System.out.println("Mobile: " + theater.mobile());
System.out.println("Base Ticket Price: " + theater.baseTicketPrice());
System.out.println("Drive In: " + theater.driveIn());
System.out.println("Snacks: ");
if(theater.snacksLength() != 0) {
for(int i = 0; i < theater.snacksLength(); i++ ) {
System.out.print(" " + theater.snacks(i));
}
}else {
System.out.println("Snacks are empty.");
}
System.out.println("Payment Method: " + PAYMENT_SYSTEM.name(theater.payment()));
System.out.println("Owner Details: ");
TheaterOwner owner = theater.owner();
if(owner != null) {
System.out.println("Name: " + owner.name());
System.out.println("Address: " + owner.address());
}else {
System.out.println("Owner " + owner);
}
}
}
}
編譯專案
現在我們已經設定了讀取器和寫入器,讓我們編譯專案。
mvn clean install
序列化Java物件
現在,編譯後,讓我們首先執行寫入器:
java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter Saving theater to file: theater_flatbuffers_output Saved theater with following data to disk: 20
反序列化已序列化的物件
現在,讓我們執行讀取器從同一檔案中讀取:
java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_flatbuffers_output Name: Mery Address: null Total Capacity: 0 Mobile: 0 Base Ticket Price: 0.0 Drive In: false Snacks: Snacks are empty. Payment Method: CASH Owner Details: Owner null
因此,正如我們所看到的,我們能夠透過將二進位制資料反序列化為劇院物件來讀取預設值。
廣告