eteran/cpp-json

How to read an optional value?

Closed this issue · 3 comments

I have this fragment, could you please provide a sample code to read the "validator" field, which may or may not be present.

	{
		"uri": "/ms/categ/delete",
		"sql": "execute sp_categ_delete ${categ_id}",
		"function": "dbexec",
		"fields": [
			{"name": "categ_id", "type": "integer", "required": "true"}
		],
		"validator": { "function": "dbexist", "sql": "sp_categ_inuse ${categ_id}" }
	}

Thank you very much!
Martin

This should do it:

    json::value obj = json::parse(file);
    if (has_key(obj, "validator")) {
        auto validator = obj["validator"];
        // use validator
    }

It's not heavily advertised, but the WIKI also has more information on the API:

https://github.com/eteran/cpp-json/wiki/API

Relevant section here: https://github.com/eteran/cpp-json/wiki/API#object-inspection