Difference between revisions of "Json parser and writer"
From VCMI Project Wiki
(Created page with "Capable of error checking and basic error recovery. Also have optional schema validation. Located at lib/JsonNode.h. Currently supports only local encoding - no conversion from u...") |
|||
(6 intermediate revisions by 3 users not shown) | |||
Line 4: | Line 4: | ||
Main class JsonNode represents one "node" from Json file (array, string, etc.). | Main class JsonNode represents one "node" from Json file (array, string, etc.). | ||
Basic usage: | Basic usage: | ||
− | < | + | <syntaxhighlight lang="cpp"> |
const JsonNode node("some_config.json"); | const JsonNode node("some_config.json"); | ||
std::string value = node["value"].String(); | std::string value = node["value"].String(); | ||
− | </ | + | </syntaxhighlight> |
+ | |||
+ | Note: accessing not present in JSON fields is legal and returns correctly initialized default value. There is no need for any checks for entry presence. | ||
+ | Good: | ||
+ | <syntaxhighlight lang="cpp"> | ||
+ | int someField = config["someField"].Float(); | ||
+ | <someField correctly initialized> | ||
+ | </syntaxhighlight> | ||
+ | Bad: | ||
+ | <syntaxhighlight lang="cpp"> | ||
+ | int someField; | ||
+ | if (config["someField"].isNull()) | ||
+ | someField = config["someField"].Float(); | ||
+ | else | ||
+ | someField = 0; | ||
+ | <someField correctly initialized> | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ===File formats=== | ||
+ | *[[Artifact Format]] | ||
+ | *[[Creature Format]] | ||
+ | *[[Bonus Format]] | ||
+ | *[[Town Format]] |
Latest revision as of 17:43, 14 March 2013
Capable of error checking and basic error recovery. Also have optional schema validation. Located at lib/JsonNode.h. Currently supports only local encoding - no conversion from unicode.
Main class JsonNode represents one "node" from Json file (array, string, etc.). Basic usage:
const JsonNode node("some_config.json");
std::string value = node["value"].String();
Note: accessing not present in JSON fields is legal and returns correctly initialized default value. There is no need for any checks for entry presence. Good:
int someField = config["someField"].Float();
<someField correctly initialized>
Bad:
int someField;
if (config["someField"].isNull())
someField = config["someField"].Float();
else
someField = 0;
<someField correctly initialized>