Giter Club home page Giter Club logo

zetjsoncpp's Introduction

zetjsoncpp

ZetJsonCpp allows to load data in json format into C estructures.

As an example, let's supose we want to load the following json data that is saved in sample.json file,

{
    // Default encoding for text
    "encoding" : "UTF-8",
     "plug-ins" : [
        "python",
        "c++",
        "ruby"
        ],
    "indent" : { "length" : 3, "use_space": true }
}

List 1.1

To load the code saw on list 1.1 we have to declare the following C estructure,

typedef struct{
    CParserVarInt32<_CONST_CHAR("length")>  m_length;
    CParserVarBool<_CONST_CHAR("use_space")> m_use_space;
}tIdent;

typedef struct
{
    // Default encoding for text
    CParserVarString<_CONST_CHAR("encoding")>         m_encoding;
    
    // Plug-ins loaded at start-up
    CParserVarArrayString<_CONST_CHAR("plug-ins")>      m_plugins;
        
    // Tab indent size
    CParserVarPropertyGroup m_indent;
}tSampleJson;

List 1.2

And then we have to write the following code to load the data seen on list 1.1 into the estructure on list 1.2,

using namespace zetjsoncpp;

int main(int argc, char *argv[]){

    // declare our data var interface.
    CParserVarPropertyGroup * data_json;

    // create json-parser
    CParserJson * parser = new CParserJson();

    // parse our file
    if(parser->evalFile("sample.json")){
        // get data from parser.
        data_json = parser->getData();
  
        // Then you already have all data filled in the struct data_json :)
    }
    
    // deallocates parser
    delete parser;

    // ... and that's all :)
    return 0;
}

And then we can manage all data loaded into our C code,

using namespace zetjsoncpp;

int main(int argc, char *argv[]){

    // declare our data var interface.
    CParserVarPropertyGroup * data_json;

    // create json-parser
    CParserJson * parser = new CParserJson();

    // parse our file
    if(parser->evalFile("sample.json")){
        // get data from parser.
        data_json = parser->getData();

         // Show the values before modifications at screen.
        std::cout << " Before modifications:"<< std::endl;
        std::cout << data_json->cpp2json();       

        // From here we can operate with loaded data in our program using c++ operators
        // put m_use_space to false...
        data_json->m_indent.m_use_space = false;

        // iterate of all m_plugins var and replace with random strings...
        for(unsigned i = 0; i < data_json->m_plugins.size(); i++) {
            data_json->m_plugins[i] = "my_randomstring"+intToString(i+1);
        }
        std::cout << "---------------------------------------------------" << std::endl;
        std::cout << " After modifications:"<< std::endl;
               
        // show the modifications at screen (it can be saved in file too)
        std::cout << data_json->cpp2json();

    }
    
    // deallocates parser
    delete parser;

    // that's all :)
    return 0;

}

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.