Skip to content
JsonBlock

ShorthandSet

JsonData& ShorthandSet(const std::string shorthand, const char delimiter = '.')
JsonData& ShorthandSet(const std::string shorthand, const std::string delimiter)

Shorthand function.
A shorthand function is designed to make working with deep, nested json structures easier and faster.

Does the same as Set(), but with a shorthand label.
Furthermore, will it create every path element necessary.

If you, for whatever reason, don't want to use the '.' delimiter, you can specify a custom one. It can even be a string!

Shorthand validity

If the shorthand is invalid, a JsonShorthandInvalidException will be thrown.
The validity of a shorthand depends on the function it is given to. In this case, a shorthand is invalid, if

  • The shorthand is empty ("")
  • At least one existing non-last path segment is not of type JSON

Examples:

C++:
JsonBlock myJsonBlock;
myJsonBlock.ShorthandSet("id.names.firstname").SetStringData("Joseph");
Resulting json:
{
    "id": {
        "names": {
            "firstname": "Joseph"
        }
    }
}
Custom delimiter (C++):
myJsonBlock.ShorthandSet("id->names->firstname", "->").SetStringData("Joseph");
myJsonBlock.ShorthandSet("id/names/firstname, '/'").SetStringData("Joseph");

Overwrite it! (C++):
myJsonBlock.ShorthandSet("id.names.firstname").SetStringData("Max");
Resulting json:
{
    "id": {
        "names": {
            "firstname": "Max"
        }
    }
}
Get it (C++):
std::string fname = myJsonBlock.ShorthandSet("id.names.firstname").AsString;
Add a new element (C++):
myJsonBlock.ShorthandSet("id.names.lastname").SetStringData("Stoleit");
Resulting json:
{
    "id": {
        "names": {
            "firstname": "Max",
            "lastname": "Stoleit"
        }
    }
}