#binary-data #binary #json #serialization #data-structures

datastruct-rs

DataStruct 通过统一的接口简化了二进制数据和 JSON 的处理

0.1.0 2024年7月29日

#1890解析器实现

Download history 129/week @ 2024-07-28 4/week @ 2024-08-04

每月下载量 133

MIT 许可证

20KB
457

DataStruct

DataStruct 是一个 Rust 库,提供了一套用于处理二进制数据、JSON 和其他格式的数据结构和实用工具。它包括一个用于处理二进制数据的 Binary 结构体,一个用于表示各种数据类型的 DValue 枚举,以及一个解析字符串数据的解析器。

特性

  • 二进制数据处理Binary 结构体允许您处理二进制数据,包括将其编码和解码为 base64 字符串。
  • 数据值表示DValue 枚举表示各种数据类型,包括字符串、数字、布尔值、列表、字典和元组。
  • JSON 序列化:DataStruct 为 DValue 枚举提供 JSON 序列化和反序列化功能。
  • 解析器:该库包括一个解析器,可以将字符串数据解析为 DValue 实例。

使用方法

将以下内容添加到您的 Cargo.toml

[dependencies]
datastruct-rs = "0.1.0"

二进制工具

use datastruct::binary_util::Binary;
use std::path::PathBuf;

// Create a new Binary instance from a vector of bytes
let binary = Binary::new(vec![72, 101, 108, 108, 111]);

// Read binary data from a file
let path = PathBuf::from("path/to/file");
let binary_from_file = Binary::from_file(path)?;

// Decode base64-encoded string to binary
let base64_string = "SGVsbG8gd29ybGQ=";
let binary_from_b64 = Binary::from_b64(base64_string.to_string())?;

DValue 枚举

use datastruct::DValue;
use std::collections::HashMap;

// Create different DValue instances
let string_value = DValue::String("Hello World".to_string());
let number_value = DValue::Number(42.0);
let boolean_value = DValue::Boolean(true);
let list_value = DValue::List(vec![DValue::Number(1.0), DValue::Number(2.0)]);
let dict_value = DValue::Dict(HashMap::new());

// Convert a DValue instance to JSON
let json_string = string_value.to_json();

// Parse a string to a DValue instance
let parsed_value = DValue::from("b:SGVsbG8gV29ybGQ=:");
let size = string_value.size();


许可证

MIT 许可证

依赖项

~1.7–2.7MB
~56K SLoC