pub enum Value {
}Variants§
F32(f32)
F64(f64)
I32(i32)
I64(i64)
U32(u32)
U64(u64)
I8(i8)
U8(u8)
I16(i16)
U16(u16)
String(String)
Array(Array)
Map(Map)
Bool(bool)
Null
Binary(Binary)
TimeStamp(TimeStamp)
Id(Id)
Implementations§
Source§impl Value
impl Value
pub fn from_bytes(bytes: &[u8]) -> DecodeResult<Value>
Source§impl Value
impl Value
Sourcepub fn to_bytes(&self) -> EncodeResult<Vec<u8>>
pub fn to_bytes(&self) -> EncodeResult<Vec<u8>>
Examples found in repository?
examples/to_bytes.rs (line 6)
1fn main() {
2 use nson::m;
3 use nson::Value;
4
5 let a = Value::I32(123);
6 println!("{:?}", a.to_bytes());
7
8 let m = m! {
9 "a": 123i32,
10 "b": {
11 "c": 456
12 }
13 };
14 println!("{:?}", m.to_bytes());
15 let m: Value = m.into();
16 println!("{:?}", m.to_bytes());
17
18 let a = 123;
19 let bytes = nson::encode::to_bytes(&a).unwrap();
20 println!("{:?}", bytes);
21
22 let b: i32 = nson::decode::from_bytes(&bytes).unwrap();
23 println!("{:?}", b);
24 assert_eq!(a, b);
25}Source§impl Value
impl Value
pub fn element_type(&self) -> DataType
Sourcepub fn bytes_size(&self) -> usize
pub fn bytes_size(&self) -> usize
Examples found in repository?
examples/basic_types.rs (line 75)
7fn main() {
8 println!("=== NSON 基本类型示例 ===\n");
9
10 // 1. 所有数字类型
11 let numbers = m! {
12 "i8": -128i8, // 8位有符号整数
13 "u8": 255u8, // 8位无符号整数
14 "i16": -32768i16, // 16位有符号整数
15 "u16": 65535u16, // 16位无符号整数
16 "i32": -2147483648i32, // 32位有符号整数
17 "u32": 4294967295u32, // 32位无符号整数
18 "i64": -9223372036854775808i64, // 64位有符号整数
19 "u64": 18446744073709551615u64, // 64位无符号整数
20 "f32": 3.14159f32, // 32位浮点数
21 "f64": 2.718281828459045f64, // 64位浮点数
22 };
23
24 println!("数字类型 Map:");
25 println!("{}\n", numbers);
26
27 // 2. 编码和解码
28 let bytes = numbers.to_bytes().unwrap();
29 println!("编码后大小: {} 字节", bytes.len());
30
31 let decoded = Map::from_bytes(&bytes).unwrap();
32 println!("解码成功: {:?}\n", decoded == numbers);
33
34 // 3. 类型访问
35 println!("访问各种类型:");
36 println!(" i8 value: {:?}", decoded.get_i8("i8"));
37 println!(" u8 value: {:?}", decoded.get_u8("u8"));
38 println!(" i16 value: {:?}", decoded.get_i16("i16"));
39 println!(" u16 value: {:?}", decoded.get_u16("u16"));
40 println!(" i32 value: {:?}", decoded.get_i32("i32"));
41 println!(" u32 value: {:?}", decoded.get_u32("u32"));
42 println!(" i64 value: {:?}", decoded.get_i64("i64"));
43 println!(" u64 value: {:?}", decoded.get_u64("u64"));
44 println!(" f32 value: {:?}", decoded.get_f32("f32"));
45 println!(" f64 value: {:?}", decoded.get_f64("f64"));
46
47 println!("\n=== 数组中的类型 ===\n");
48
49 // 4. 数组中使用不同类型
50 let mixed_array = Array::from_vec(vec![
51 Value::I8(-10),
52 Value::U8(200),
53 Value::I16(-1000),
54 Value::U16(50000),
55 Value::F32(1.5),
56 Value::from("text"),
57 ]);
58
59 println!("混合类型数组: {:?}", mixed_array);
60
61 let array_bytes = mixed_array.to_bytes().unwrap();
62 let decoded_array = Array::from_bytes(&array_bytes).unwrap();
63 println!("数组解码成功: {:?}\n", decoded_array == mixed_array);
64
65 // 5. 类型大小比较
66 println!("=== 类型存储大小 ===");
67 let size_demo = m! {
68 "i8": -1i8,
69 "i16": -1i16,
70 "i32": -1i32,
71 "i64": -1i64,
72 };
73
74 for (key, value) in size_demo.iter() {
75 println!("{}: {} 字节 + 1字节类型标记", key, value.bytes_size());
76 }
77}pub fn as_f32(&self) -> Option<f32>
pub fn as_f64(&self) -> Option<f64>
pub fn as_i32(&self) -> Option<i32>
pub fn as_u32(&self) -> Option<u32>
pub fn as_i64(&self) -> Option<i64>
pub fn as_u64(&self) -> Option<u64>
pub fn as_i8(&self) -> Option<i8>
pub fn as_u8(&self) -> Option<u8>
pub fn as_i16(&self) -> Option<i16>
pub fn as_u16(&self) -> Option<u16>
pub fn as_str(&self) -> Option<&str>
pub fn as_array(&self) -> Option<&Array>
pub fn as_map(&self) -> Option<&Map>
pub fn as_bool(&self) -> Option<bool>
pub fn as_id(&self) -> Option<&Id>
pub fn as_timestamp(&self) -> Option<TimeStamp>
pub fn as_null(&self) -> Option<()>
pub fn as_binary(&self) -> Option<&Binary>
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize<D>(deserializer: D) -> Result<Value, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Value, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl FromIterator<Value> for Array
impl FromIterator<Value> for Array
impl Eq for Value
impl StructuralPartialEq for Value
Auto Trait Implementations§
impl Freeze for Value
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.