Skip to content

ItisCaleb/Rust-JSON

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust-JSON

A homemade JSON library

Usage

Simple JSON Parse

extern crate rjson;
use rjson::{JsonParser,Result};
fn main() -> Result<()>{
    let json = "[{
        \"login\":true,
        \"player\":\"Asuna\"
        }]";
    let result = JsonParser::parse(&json)?;
    if result.array()?.get(0)?.object()?.get("login")?.bool()?{
        println!("Link Start!");
    }
    //or you can use indexing
    println!("{}",result[0]["player"].string()?);
    //btw you can also use patter matching
    if let JsonType::String(admin) = result["admin"].get_type(){
        println!("{}",admin);
    }
    Ok(())
}

JSON Serialize

use rjson::{Serializable,to_json};
#[derive(Serializable)]
struct SAO{
    // you can add #[exclude] attribute to ignore 
    // this field when serializing
    #[exclude]
    admin:String,
    player:Vec<Player>,
}

#[derive(Serializable)]
struct Player{
    name: String,
    id: usize
}
fn main(){
    let sao = SAO{
        admin: "Kayaba Akihiko".to_string(),
        player: vec![
            Player{
                name:"Kirito".to_string(),
                id:0
            },
            Player{
                name:"Asuna".to_string(),
                id:1
            }
        ]
    };
    println!("{}", to_json(class));
}

About

A homemade JSON library for practice and fun

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages