【Rust】Rustでjsonに読み書き

### jsonの読み込み

use serde::{Serialize, Deserialize};
use std::fs;
use std::io::prelude::*;

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
struct Rotator {
    Pitch:f32,
    Roll:f32,
    Yaw:f32,
}

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
struct CharaParam {
    Name: String,
    Id: i32,
    ItemIdList: Vec<u32>,
    RotData:Rotator,
}

fn main(){
    let input_fn = fs::read_to_string("src/test_input.json")
        .expect("JSON Read Failed.");
    let deserialized: Vec<CharaParam> = serde_json::from_str(&input_fn).unwrap();

    for data in &deserialized {
        println!("{:?}", data);
    }
}

### jsonの書き出し

use serde::{Serialize, Deserialize};
use std::fs::File;
use std::io::prelude::*;

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
struct Rotator {
    Pitch:f32,
    Roll:f32,
    Yaw:f32,
}

#[derive(Serialize, Deserialize, Debug)]
#[allow(non_snake_case)]
struct CharaParam {
    Name: String,
    Id: i32,
    ItemIdList: Vec<u32>,
    RotData:Rotator,
}

fn main(){
    let chara00 = CharaParam{Name:String::from("Apple"), Id:0x01, ItemIdList:vec![1000, 1001], RotData:Rotator{Pitch:0.0, Roll:0.0, Yaw:32.0} };
    let chara01 = CharaParam{Name:String::from("Banana"), Id:0x02, ItemIdList:vec![1002, 1003], RotData:Rotator{Pitch:0.0, Roll:-70.0, Yaw:66.0} };

    let mut param_list:Vec<CharaParam> = Vec::new();
    param_list.push(chara00);
    param_list.push(chara01);

    let output_fn = String::from("src/test_output.json");
    let result = output_json(&output_fn, param_list);
    match result {
        Ok(..) => {println!("Json Output Finished. [{}]", output_fn)}
        Err(err) => {println!("Error! : {}", err)}
    }
}

fn output_json(output_fn: &str, charaparam_list: Vec<CharaParam>) -> std::io::Result<()> {
    let serialized: String = serde_json::to_string(&charaparam_list).unwrap();
    let mut file = File::create(output_fn)?;
    file.write_all(serialized.as_bytes())?;
    Ok(())
}

serde_json::to_string(&charaparam_list).unwrap(); でstring型にしていますね。

つまりこういうことかな?

use serde::{Serialize, Deserialize};
use std::io::prelude::*;
use hex_literal::hex;
use k256::{ecdsa::{SigningKey, Signature, signature::Signer, signature::Verifier, VerifyingKey}};
use chrono::{Utc, Local, DateTime, Date};

#[derive(Serialize, Deserialize, Debug)]
struct UnsignedTransaction {
    time: String,
    sender: String,
    receiver: String,
    amount: i32,
}

#[derive(Serialize, Deserialize, Debug)]
struct SignedTransaction {
    time: String,
    sender: String,
    receiver: String,
    amount: i32,
    signature: String,
}

fn hex(bytes: &[u8]) -> String {
    bytes.iter().fold("".to_owned(), |s, b| format!("{}{:x}", s, b))
}

fn main(){
    let private_key: SigningKey = SigningKey::from_bytes(&hex!(
        "DCDFF4B7CA287CC7BD30ECAEF0622265DB4E14054E12954225457C3A6B84F135"
    ).into()).unwrap();
    let public_key: &VerifyingKey = private_key.verifying_key();
    let public_key_str = hex(&public_key.to_encoded_point(false).to_bytes());
    let public_key_b_str = "4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991124";

    let utc_datetime: DateTime<Utc> = Utc::now();
    let ut1 = UnsignedTransaction {time: utc_datetime.to_string(), sender: public_key_str.to_string(), receiver: public_key_b_str.to_string(), amount: 10};
    println!("{:?}", ut1);
    let serialized: String = serde_json::to_string(&ut1).unwrap();
    let sig1: Signature = private_key.sign(serialized.as_bytes());
    let signed_ut1 = SignedTransaction {time: utc_datetime.to_string(), sender: public_key_str.to_string(), receiver: public_key_b_str.to_string(), amount: 10, signature: sig1.to_string()};
    println!("{:?}", signed_ut1);

}

UnsignedTransaction { time: “2024-12-22 00:30:45.843926032 UTC”, sender: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991123”, receiver: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991124”, amount: 10 }
SignedTransaction { time: “2024-12-22 00:30:45.843926032 UTC”, sender: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991123”, receiver: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991124”, amount: 10, signature: “301DC132F45B1F8D0734A51E73F6725260B5365FBC9C2C405B9A12C70B97E47842366C2EC589F05AF8715268BC5A954B487F234F5FD3157ADBE8940AF2037B60” }

シリアライズしてバイトにしてsignする。。
しかし、これだとうまくいかん…

    let pubkey = signed_ut1.sender;
    let sig = signed_ut1.signature;

    let verified = pubkey.verify(ut1, &sig).is_ok();

    if verified {
        println!("文章は改竄されていません。");
    } else {
        println!("文章が改竄されています。");
    }