8000 Q: Use rfl::Variant / rfl::TaggedUnion with XML tag name. · Issue #419 · getml/reflect-cpp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Q: Use rfl::Variant / rfl::TaggedUnion with XML tag name. #419

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

Closed
GregorySech opened this issue Apr 28, 2025 · 3 comments
Closed

Q: Use rfl::Variant / rfl::TaggedUnion with XML tag name. #419

GregorySech opened this issue Apr 28, 2025 · 3 comments

Comments

@GregorySech
Copy link
GregorySech commented Apr 28, 2025

I would like to (de)serialize XML files that are similar to the following:

<Basket>
  <Banana width=10 height=2 color="yellow" />
  <Apple radius=3 weight=0.5/>
  <Ananas radius=3.5 height=12/>
  <Apple radius=3.2 weight=0.5/>
  <Banana width=11 height=2 color="green" />
  <Banana width=11.2 height=2 color="green" />
</Basket>

I would like to model this using a rfl::Variant / TaggedUnion type like so:

struct Banana {
	rfl::Attribute<float> width;
	rfl::Attribute<float> height;
	rfl::Attribute<std::string> color;
};

struct Apple {
	rfl::Attribute<float> radius;
	rfl::Attribute<float> weight;
};

struct Ananas {
	rfl::Attribute<float> radius;
	rfl::Attribute<float> height;
};

using Fruits = rfl::Variant<Banana, Apple, Ananas>;

struct Basket {
	std::vector<Fruits> fruits;
};

What I'm missing is how can I associate the Variant Tag to the Tag name instead of having the <Basket> tag be composed of <fruits \> tags.
Is there some way of doing it?

@liuzicheng1987
Copy link
Contributor

@GregorySech , I don't think you could use variants for that.

The only solution I could think of out of the top of my head is this:

struct Basket {
	std::vector<Banana> Banana;
	std::vector<Pineapple> Pineapple;
	std::vector<Apple> Apple;
};

@GregorySech
Copy link
Author
GregorySech commented May 5, 2025

@GregorySech , I don't think you could use variants for that.

The only solution I could think of out of the top of my head is this:

struct Basket {
std::vector Banana;
std::vector Pineapple;
std::vector Apple;
};

@liuzicheng1987 thanks for the quick answer, I thought it would have not worked with an XML with unordered tags but today I could try it and it's working! With this solution any written XML will always have the tags grouped by type but for my use case that should be fine.

Thank you again!

P.S. should I close this issue? It still is highlighting a limitation of the currently implemented XML de-serialization with respect to other formats. In JSON the set of classes presented in the issue works using tagged unions.

@liuzicheng1987
Copy link
Contributor
liuzicheng1987 commented May 5, 2025

Yeah, please close the issue. I think this is more of a limitation of the XML format.

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
0