Recently I want to write a Custom Animation data file. According to the original practice, I first define a data file in text format, then write a converter into binary, and then read and parse it in the game. However, I have recently been very lazy. Although I have already designed a "omnipotent" text format and binary conversion tool, it is still cumbersome to parse binary files. Based on past experience, the file format may be changed frequently, so I want to directly use the text format. The first thing I think of is XML, but it's too lazy recently, so I'm too lazy to parse XML. Well, there is also Google protocol buffers, which I have seen before, but it seems that there are not many people using it. There is also a JSON file. If you have seen it in the game, try it ..
Found several C ++ JSON parser, and finally selected jsoncpp (http://jsoncpp.sourceforge.net ). First, jsoncpp is public domain, so it's so nice to use it whenever I want. Then it uses the Standard C ++, there is no other dependency (some libraries must rely on boost and other things). At last, it was very small and only had 15 source files. I copied these files directly to the project and compiled them, saves the Lib configuration, compilation, and connection settings. (If you are too lazy, you will be lazy ...), Directly read the document. The usage is very simple. I wrote a simple JSON file for testing (in fact, it was written using the styled writer of jsoncpp ..)
[JavaScript] View plaincopyprint?
-
- {
-
- "Float test"1.0,
- "Interger test": 1,
-
- "OBJ":{
-
- "":"Obja",
-
- "B":"Objb",
-
- "C"0.130
-
- },
- "Str test":"Hello JSON! "
-
- }
{<Br/> "float test": 1.0, <br/> "interger test": 1, <br/> "OBJ": {<br/> "": "obja", <br/> "B": "objb", <br/> "C": 0.130 <br/>}, <br/> "str test ": "Hello JSON! "<Br/>}
TestCode:
[CPP] View plaincopyprint?
-
- # Include "JSON/JSON. H"
- # Include <fstream>
-
- VoidTestjson ()
-
- {
-
- STD: ifstream infile ("Test. JSON");
-
- JSON: Value root;
-
- JSON: reader;
-
- BoolOK = reader. parse (infile, root );// You can directly input an ifstream and resolve it all to the root. Of course, the file should not be too large .. This DOM method is suitable for me.
- If(! OK)
-
- {
-
- Return;
-
- }
-
- STD: string name = root. Get ("Str test",""). Asstring ();// Test the read Attribute Value
-
- STD: cout <name <STD: Endl;
- // Test the write property value. You can directly generate new key-value pairs and objects. In fact, my previous test. JSON is empty and is written here.
-
- Root ["Interger test"] = 1;
-
- Root ["Float test"] = 1.0f;
-
- Root ["OBJ"] [""] ="Obja";
-
- Root ["OBJ"] ["B"] ="Objb";
- Root ["OBJ"] ["C"] = 0.13;
-
- JSON: styledwriter writer;
-
- STD: String output = writer. Write (Root );
-
- STD: cout <output;
-
- // Write the file directly.
-
- STD: ofstream file ("Test. JSON");
-
- File <output;
-
- }
# Include "JSON/JSON. H "<br/> # include <fstream> <br/> void testjson () <br/> {<br/> STD: ifstream infile (" test. JSON "); <br/> JSON: Value root; <br/> JSON: reader; <br/> bool OK = reader. parse (infile, root); // you can directly input an ifstream and resolve it all to the root. Of course, the file should not be too large .. This DOM method is suitable for me <br/> If (! OK) <br/>{< br/> return; <br/>}< br/> STD: string name = root. get ("str test ",""). asstring (); // test the read attribute value <br/> STD: cout <name <STD: Endl; <br/> // test the write attribute value, you can directly generate new key-value pairs and objects. In fact, my previous test. JSON is empty and is written here <br/> root ["interger test"] = 1; <br/> root ["float test"] = 1.0f; <br/> root ["OBJ"] ["A"] = "obja"; <br/> root ["OBJ"] ["B"] = "objb "; <br/> root ["OBJ"] ["C"] = 0.13; <br/> JSON: styledwriter writer; <br/> STD: String output = writer. write (Root); <br/> STD: cout <output; <br/> // write the file directly. <br/> STD :: ofstream file ("test. JSON "); <br/> file <output; <br/>}
It's easy to use jsoncpp. I didn't write any parsing code, and I got a JSON object directly from the file, with JSON objects, you can write values to your game data objects, or use JSON objects directly as an early test.