tamask1s / zax-parser

Zax is a very basic Json parser implemented in C++ 11 with probably the simplest possible interface.
Apache License 2.0
11 stars 3 forks source link

Arguments passing from command prompt. It can be stored and compare specific argument. #4

Open sateeshmamidala5 opened 10 months ago

sateeshmamidala5 commented 10 months ago

Its not a issue. I want specific feature support using pre built zax-parser library I want compare my string.

Example: assume I am giving inputs from command prompt.

argv[0] argv[1] argv[2]

argv[2] is consider data.

I want to store and compare my string using zaxparser library. For checking example

if(zaxparser::isData(argv[2]))

like isData type any checking condition pre built one exist please provide me data or already available check condition exist please update if its not exist please create it.

Thanks & Regards, Sateesh

tamask1s commented 10 months ago

Hi sateeshmamidala5,

(*a working example is on the second part below.) As the zax parser is mostly a template library, therefore it can't have a prebuilt package. It targets c++ structures to be serialized and deserialzed, but those c++ structures are created by the user of the library, and the user needs to build its own binary using the already known structures (which only the user can know). An example to start on with what you need I think is something like:

#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include "ZaxJsonParser.h"

using namespace std;

struct some_class
{
    int x = 9;
    string name = "some name";
    ZAX_JSON_SERIALIZABLE(some_class, JSON_PROPERTY(x), JSON_PROPERTY(name))
};

int main()
{
    vector<string> parser_output_stream;
    some_class some_obj;
    const char* json_string = R"({"some_int": 7, "name": "new name"})"; /// you can get the value from argv
    if (true) /// true: produces warning output, false: does not produce warning line.
        json_string = R"({"name": "new name"})";

    some_obj.zax_from_json(json_string, &parser_output_stream);
    for (string error_or_warning_line : parser_output_stream)
        cout << error_or_warning_line;
    return 0;
}

This will produce the output:

WARNING: JSON property is missing: 'x'

You can change this code to get the R"(...)" from argv, and then implement a function outside the library to check the output strem lines whether it contains an error or warning regarding your structure menber (you can have range errors for int types for example, but you only can have warnings for strings).

(As this code is dependent on the c++ structures, which needs to be built, this might not be what you want.)


If you want to have your implementation "structure independent", and have a prebuilt app for your arbitrary json strings and keys, then you need to use a more basic parser. In our case this is the ZaxJsonTopTokenizer. You can have for example an application whith this content in the main.cpp:

#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include "ZaxJsonParser.h"
using namespace std;

int main(int argc, char **argv)
{
    if (argc == 3)
    {
        const char* json_string = argv[1];
        const char* a_member_to_check = argv[2];
        bool success = false;
        ZaxJsonTopTokenizer* parsed_json = new ZaxJsonTopTokenizer(json_string, false, &success);
        if (!success)
            cout << "ERROR: error parsing JSON: '" << json_string << endl;
        else
        {
            auto it = parsed_json->m_values.find(a_member_to_check);
            if (it != parsed_json->m_values.end())
                cout << "member '" << a_member_to_check << "' found. its value is: " << it->second << endl;
            else
                cout << "member '" << a_member_to_check << "' not found." << endl;
        }
        delete parsed_json;
    }
    else
        cout << "wrong number of arguments. Must be 2." << endl;
    return 0;
}

You can compile this main.cpp if you have the ZaxJsonParser.h and ZaxJsonParser.cpp in the same folder with just issuing: g++ main.cpp ZaxJsonParser.cpp -o example by running it like: ./example '{"some_int":7,"name":"new name"}' some_int you will get the output: member 'some_int' found. its value is: 7

and by running it like: ./example '{"some_int":7,"name":"new name"}' something you will get the output: member 'something' not found.

However, the parser might not be complient with some standards. If you want to comply standards, I would advise to use another parser. This parser is meant to try to do its best to map some json to a c++ structure, and therefore I made some decisions. For eample a string like "7" will also be successfully parsed into an int. I mean: ./example '{"some_int":"7","name":"new name"}' some_int will also produce: member 'some_int' found. its value is: 7 without dropping an error or warning.

Br: Tamas

sateeshmamidala5 commented 10 months ago

I agree its a templet. But what I am thinking instead of creating new one existing only I want to reuse it in environment. I will just include header files.

tamask1s commented 10 months ago

You just need to add the ZaxJsonParser.h and ZaxJsonParser.cpp files to your project / build system. The above example can be built with gcc like g++ main.cpp ZaxJsonParser.cpp -o example Of course you might have a bigger build system, and you need to add the include path as well.