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

Fix the README #5

Open
Joonho-Hwang opened this issue Mar 11, 2023 · 0 comments
Open

Fix the README #5

Joonho-Hwang opened this issue Mar 11, 2023 · 0 comments

Comments

@Joonho-Hwang
Copy link

In the Serialize custom type example, write and read function should be a template specialization.
So,

// Let's have a 3D vector struct:
struct vec3 { float x, y, z; };

// Let's have a triangle struct with 'vec3' members
struct Triangle
{
	vec3 a, b, c;
};

JSON5_CLASS(Triangle, a, b, c)

namespace json5::detail {

// Write vec3 as JSON array of 3 numbers
template<>
inline json5::value write( writer &w, const vec3 &in )
{
	w.push_array();
	w += write( w, in.x );
	w += write( w, in.y );
	w += write( w, in.z );
	return w.pop();
}

// Read vec3 from JSON array
template<>
inline error read( const json5::value &in, vec3 &out )
{
	return read( json5::array_view( in ), out.x, out.y, out.z );
}

} // namespace json5::detail

This is the right way to do this.

Also, please make a note about the #include order. It seems like json5_input.hpp should be included in prior to json5_reflect.hpp.

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

1 participant