Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization doesn't support Serde alternative enum representations #4

Closed
StyMaar opened this issue Sep 23, 2021 · 2 comments
Closed

Comments

@StyMaar
Copy link

StyMaar commented Sep 23, 2021

Serde allows different enum representations: Internally tagged, Adjacently tagged and Untagged in addition to the default Externally tagged representation see Enum representations in Serde's doc.

Serde gura properly support seralization of such enum representation, but fails to perform deserialization of such representation.

Example:

Internally tagged

use serde::{Deserialize, Serialize};
use serde_gura::Result;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "lowercase",tag = "type")]
enum MyEnum{
    Database(Database)
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Database {
    ip: String,
    port: Vec<u16>,
    connection_max: u32,
    enabled: bool,
}

fn main() -> Result<()> {


    // You have some type.
    let database = MyEnum::Database(Database {
        ip: "127.0.0.1".to_string(),
        port: vec![80, 8080],
        connection_max: 1200,
        enabled: true,
    });

    let sss_str = serde_gura::to_string(&database)?;
    
    println!("{}", &sss_str);
 

    // Deserialize it back to a Rust type
    let deserialized_database: MyEnum = serde_gura::from_str(&sss_str)?;
    
    println!("{:#?}", deserialized_database);

    Ok(())
}
type: "database"
ip: "127.0.0.1"
port: [80, 8080]
connection_max: 1200
enabled: true
Error: ExpectedIdentifier

Untagged

use serde::{Deserialize, Serialize};
use serde_gura::Result;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "lowercase",untagged)]
enum MyEnum{
    Database(Database)
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Database {
    ip: String,
    port: Vec<u16>,
    connection_max: u32,
    enabled: bool,
}

fn main() -> Result<()> {


    // You have some type.
    let database = MyEnum::Database(Database {
        ip: "127.0.0.1".to_string(),
        port: vec![80, 8080],
        connection_max: 1200,
        enabled: true,
    });

    let sss_str = serde_gura::to_string(&database)?;
    
    println!("{}", &sss_str);
 

    // Deserialize it back to a Rust type
    let deserialized_database: MyEnum = serde_gura::from_str(&sss_str)?;
    
    println!("{:#?}", deserialized_database);

    Ok(())
}

output

ip: "127.0.0.1"
port: [80, 8080]
connection_max: 1200
enabled: true
Error: InvalidType

Adjacently tagged

use serde::{Deserialize, Serialize};
use serde_gura::Result;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "lowercase",tag = "t", content = "c")]
enum MyEnum{
    Database(Database)
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Database {
    ip: String,
    port: Vec<u16>,
    connection_max: u32,
    enabled: bool,
}

fn main() -> Result<()> {


    // You have some type.
    let database = MyEnum::Database(Database {
        ip: "127.0.0.1".to_string(),
        port: vec![80, 8080],
        connection_max: 1200,
        enabled: true,
    });

    let sss_str = serde_gura::to_string(&database)?;
    
    println!("{}", &sss_str);
 

    // Deserialize it back to a Rust type
    let deserialized_database: MyEnum = serde_gura::from_str(&sss_str)?;
    
    println!("{:#?}", deserialized_database);

    Ok(())
}

output

t: "database"
c:
    ip: "127.0.0.1"
    port: [80, 8080]
    connection_max: 1200
    enabled: true
Error: ExpectedIdentifier
@Genarito
Copy link
Member

Thank you for the report! I'll analyze the problem and publish the fix along with the changes listed in #3

Regards!

@Genarito
Copy link
Member

All the 3 enums alternatives reported are now supported in version 0.1.3. This issue also allowed me to find some little mistakes that I took the opportunity to correct 👌

Thanks for making Gura better!
Best regards!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants