A simplistic Json Reader in C++. Build will C++ 11 and uses STL features to read a Json File.
To use the Json Reader, Clone the repository or just download the Source folder and copy in your Project. You can also just cpoy the JsonParser Folder (Source/src/Json Parser) in src or lib folders of your project.
Include the JsonParser.h file in your project wherever you want to use the Reader.
Everything is included in the JsonReader namespace.
#include "JsonParser.h"
.
.
Following are some basic examples.
- Create an instance of the JsonDocument class.
JsonReader::JsonDocument doc;
- And call the LoadDocument() function which takes the full path of the file as an input.
doc.LoadDocument("C:\Project1\file.json");
This will store the Info of the Json file in the JsonDocument object.
IMPORTANT: Every Node/Child/Leaf/Object/Array in the Json Document is a JsonItem for this parser.
- Create a pointer to the JsonItem class. The parsed data will be stored in that pointer as a hierarchy.
JsonReader::JsonItem* root;
- Call the Parse() function to give data to the JsonItem* variable.
root = doc.Parse();
- Create a JsonItem object to store the first child in the object and call GetFirstChild().
JsonReader::JsonItem* item = root->GetFirstChild();
- To get the next element or object, call GetNextChild() from the parent item.
item = root->GetNextChild();
IMPORTANT: If the Item has no further childs, to prevent the assert, fuunction will return the same child over and over again.
You can get the value using the GetValue() function which returns the object of the value.
To get the value as an DataType and not a Location, use the following functions:-
-
asString(): Returns the value as a string. If no conversion possible, returns null.
-
asInt(): Returns value as an integer. If no conversions possible, returns 0.
-
asBool(): Returns value as an boolean. If no conversions possible, returns false.
-
asDouble: Returns value as an floating point number. If no conversions possible, returns 0.0f.
item->GetValue()->asString();
IMPORTANT: If no value found (for example if the value is an Array), it will return an empty string "";
- size(): Returns the number of elements in that particular object.
root->size()
- GetChildAt(int index): Returns the child object on the given index. If not present, returns nullptr. Actual value can be accessed further.
root->GetChildAt(2)->GetValue()->asBool();
For Further Implementation, see the Test.cpp file.
Test.cpp soon to be updated showing full functionality.
This project is licensed under the MIT License - see the LICENSE.md file for details