Giter VIP home page Giter VIP logo

littlejsonreader's Introduction

LittleJsonReader

A simplistic Json Reader in C++. Build will C++ 11 and uses STL features to read a Json File.

Getting Started

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.

Usage:

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

Examples

Following are some basic examples.

Loading the Document

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

Parsing the File

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();

Accessing the Elements

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

Getting the Values

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.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

littlejsonreader's People

Contributors

codenameone-akshat avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.