Reprint-C ++ use JSON in game development.

Source: Internet
Author: User

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?
  1. {
  2. "Float test"1.0,
  3. "Interger test": 1,
  4. "OBJ":{
  5. "":"Obja",
  6. "B":"Objb",
  7. "C"0.130
  8. },
  9. "Str test":"Hello JSON! "
  10. }

{<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?
  1. # Include "JSON/JSON. H"
  2. # Include <fstream>
  3. VoidTestjson ()
  4. {
  5. STD: ifstream infile ("Test. JSON");
  6. JSON: Value root;
  7. JSON: reader;
  8. 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.
  9. If(! OK)
  10. {
  11. Return;
  12. }
  13. STD: string name = root. Get ("Str test",""). Asstring ();// Test the read Attribute Value
  14. STD: cout <name <STD: Endl;
  15. // 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.
  16. Root ["Interger test"] = 1;
  17. Root ["Float test"] = 1.0f;
  18. Root ["OBJ"] [""] ="Obja";
  19. Root ["OBJ"] ["B"] ="Objb";
  20. Root ["OBJ"] ["C"] = 0.13;
  21. JSON: styledwriter writer;
  22. STD: String output = writer. Write (Root );
  23. STD: cout <output;
  24. // Write the file directly.
  25. STD: ofstream file ("Test. JSON");
  26. File <output;
  27. }

# 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.