Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Several SRP JsonX routines rely on paths to reference elements within a document. Moreover, every document maintains a current path that references an object or array. When you call SRP_JsonX, for example, to add a value, that value is added to the current object or array. To change the current path, users can call SRP_JsonX_Go, SRP_JsonX_GoBack, or SRP_JsonX_GoRoot.

Path is specially formatted strings, akin to a mini query language, that tell SRP JsonX how to find an element.

Empty Path

The simplest path is an empty one.

Code Block
Result = SRP_JsonX_Get()

Calling SRP_JsonX_Get with no path is like calling it with a path set to "". When a path is empty, SRP JsonX uses the current path.

Simple Path

The next simplest path is a single name or number. If the current element is an object, we can pass a name to reference a member in that object

Code Block
languagebp
Result = SRP_JsonX_Get("firstname")

If the current element is an array, we can pass an index into that array.

Code Block
languagebp
Result = SRP_JsonX_Get(2)

In either case, if the member name is not found or the index is out of bounds, null is returned.

Complex Path

The real power of the path is to drill deep into the document without having to manage handles or pointers. Let's use the following json as an example:

Code Block
languagejs
{"menu": {
    "header": "SVG Viewer",
    "items": [
        {"id": "Open"},
        {"id": "OpenNew", "label": "Open New"},
        null,
        {"id": "ZoomIn", "label": "Zoom In"},
        {"id": "ZoomOut", "label": "Zoom Out"},
        {"id": "OriginalView", "label": "Original View"},
        null,
        {"id": "Quality"},
        {"id": "Pause"},
        {"id": "Mute"},
        null,
        {"id": "Find", "label": "Find..."},
        {"id": "FindAgain", "label": "Find Again"},
        {"id": "Copy"},
        {"id": "CopyAgain", "label": "Copy Again"},
        {"id": "CopySVG", "label": "Copy SVG"},
        {"id": "ViewSVG", "label": "View SVG"},
        {"id": "ViewSource", "label": "View Source"},
        {"id": "SaveAs", "label": "Save As"},
        null,
        {"id": "Help"},
        {"id": "About", "label": "About Adobe CVG Viewer..."}
    ]
}}

When we parse the above json, the current path points to the root of the document. So, all our paths will be relative to the root.

To get the entire menu, we user a simple path

Code Block
languagebp
Result = SRP_JsonX_Get("menu")

To get the member of a sub-object, separate each member name with a period. This example gets the items array of the menu object.

Code Block
languagebp
Result = SRP_JsonX_Get("menu.items")

To get a array element, surround the array index with square brackets or angle brackets. This examples gets item 12 in the items array.

Code Block
languagebp
Result = SRP_JsonX_Get("menu.items[12]")

And this is how we get the handle to the "label" member of the second item in the items array:

Code Block
languagebp
Result = SRP_JsonX_Get("menu.items[2].label")

Escaping Reserved Characters

The Path syntax reserves five characters: period, left square bracket, right square bracket, left angle bracket, and right angle bracket. If the member you want to access uses one of these characters, you must escape them by doubling up. Here's an example of a JSON document with periods in the member names and how to access them.

Code Block
languagebp
{
    "first.name" : "John"
    "last.name"  : "Doe"
}

// INCORRECT: This will get you "" because it will try to get "first" and then "name"
Name = SRP_JsonX_Get("first.name")

// CORRECT: This will get you "first.name"
Name = SRP_JsonX_Get("first..name")

...