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

MDAST: serde Serialization/Deserialization not working #72

Open
nicoburniske opened this issue Jul 3, 2023 · 2 comments · May be fixed by #74
Open

MDAST: serde Serialization/Deserialization not working #72

nicoburniske opened this issue Jul 3, 2023 · 2 comments · May be fixed by #74

Comments

@nicoburniske
Copy link

The serialization of the mdast fails to deserialize because of a duplicate field type

#[test]
fn test_serde() {
    let markdown = "This is a **test**";
    let tree =
    markdown::to_mdast(&markdown, &markdown::ParseOptions::default()).unwrap();
    let json = serde_json::to_string(&tree).unwrap();
    let tree2: Node = serde_json::from_str(&json).unwrap();
    assert!(tree == tree2);
}

I get the following error from serde

Error("duplicate field `type`", line: 1, column: 21)

This is the generated json. It has duplicate type fields everywhere.

{
   "type":"Root",
   "type":"root",
   "children":[
      {
         "type":"Code",
         "type":"code",
         "value":"This is a **test**",
         "position":{
            "start":{
               "line":2,
               "column":1,
               "offset":1
            },
            "end":{
               "line":3,
               "column":5,
               "offset":28
            }
         },
         "lang":null,
         "meta":null
      }
   ],
   "position":{
      "start":{
         "line":1,
         "column":1,
         "offset":0
      },
      "end":{
         "line":3,
         "column":5,
         "offset":28
      }
   }
}

Looking at the AST enum and related structs they all have serde(tag, rename) macro configs, which seem to be conflicting.

#[derive(Clone, Eq, PartialEq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(tag = "type", rename = "type")
)]
pub enum Node {
    // Document:
    /// Root.
    Root(Root),
//..
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(tag = "type", rename = "root")
)]
pub struct Root {
    // Parent.
    /// Content model.
    pub children: Vec<Node>,
    /// Positional info.
    pub position: Option<Position>,
}
@wooorm
Copy link
Owner

wooorm commented Jul 3, 2023

Definitely possible! PR welcome! :)

@sshwy sshwy linked a pull request Jul 11, 2023 that will close this issue
@wflixu
Copy link

wflixu commented Mar 8, 2024

I have the same problem, I hope the fixing PR can join in as soon as possible!

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

Successfully merging a pull request may close this issue.

3 participants