- ๐ Hi, Iโm @P-i-N
- ๐ Iโm interested in ...
- ๐ฑ Iโm currently learning ...
p-i-n / json5 Goto Github PK
View Code? Open in Web Editor NEWHeader only JSON/JSON5 parser and serializer for C++
License: MIT License
Header only JSON/JSON5 parser and serializer for C++
License: MIT License
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
.
Given a json document with multiple sub levels, for example:
{
"foo": {
"bar": {
alpha: 1.0,
beta: 2.1
}
}
}
If I navigate such that json5::value points to foo/bar
, how can I see this portion of the document as a string:
{ "alpha": 1.0, "beta": 2.1 }
I took a look at the the to_string`` functionality in
json5_reflect, but did not see a way to use this to convert the
json5::value``` subtree to its associated json string. Does this functionality exist?
In json, if an identifier is quoted, it may contain any non-string terminating character. This parser will not parse the following for example:
{ "this*test": 123 }
due to the "*" character not being in alpha-numeric set. However if quoted, the above is valid. The following code needs to be changed in json5_input.hpp::parse_identifier. Change:
while ( !eof() )
{
string_buffer_add( next() );
int ch = peek();
if ( !isalpha( ch ) && !isdigit( ch ) && ch != '_' )
break;
}
to this:
while ( !eof() )
{
string_buffer_add( next() );
int ch = peek();
if ((!isString && !isalpha(ch) && !isdigit(ch) && ch != '_' ) || (isString && ch == firstCh))
break;
}
A flag was set previously in this function isString
which indicates whether the identifier is quoted or not.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.