Versions Compared

Key

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

...

Info

Available in SRP Utilities 2.2 or later.

Stateful

To avoid dealing with handles, SRP JsonX relies on state. Under the hood, SRP JsonX maintains a stack of JsonX documents. Whenever you start a JsonX document, that document is placed at the top of the stack and becomes the current document. Every document has a current path, which defaults to the root of the document. These two states are important to navigating, updating, and building json documents with SRP JsonX. When you are done with a document, you call SRP_JsonX_End, which removes the current document from memory and makes the previous document on the stack the current one.

Creating Json

There are two routines for creating new json documents: SRP_JsonX_Begin and SRP_JsonX_BeginString. SRP_JsonX_Begin creates a new json data structure that can be built, navigated, and modified at will. SRP_JsonX_BeginString creates a new json string that can only be built top down.

...

Example 2 makes the same json as Example 1, but this time we started with SRP_JsonX_BeginString. Note how the calls to SRP_JsonX don't behave any differently. The only difference is that behind the scenes, SRP JsonX is producing pretty json text directly. If you know you are making a json string and you plan to build it from the top down, then SRP_JsonX_BeginString will be faster.

Parsing Json

SRP JsonX is the fastest parser available to OI developers, and now it's easier to navigate, modify, and extract json documents thanks to the elimination of cumbersome handles. Let's parse the following json:

...

Code Block
languagebp
titleExample 3
$insert SRPJSONX

// Create a new document by parsing the json text
SRP_JsonX_Parse('MyParsedDoc', Json)
	
	// Get the first employee's last name using SRP JsonX path syntax
	LastName = SRP_JsonX_Get('employees[1].lastname')
	
	// If our path points to an object or an array, we'll get the entire object or array as json text
	Employee = SRP_JsonX_Get('employees[1]')
	
	// SRP_JsonX_Get doesn't change the current path. If we want to change it, we use the Go routine.
	// Let's make the second employee the current path.
	SRP_JsonX_Go('employees[2]')
	
	// Now, if I want to get the last name, I must use a path relative to the current one
	LastName = SRP_JsonX_Get('lastname')
	
	// If I try to use a full path, I'll get null because all paths are relative to the current one
	// In this case, 'employees' doesn't exist inside 'employees[2]'
	LastName = SRP_JsonX_Get('employees[3].lastname')
	
	// We can go back up the json heirarchy one level at a time. This call makes 'employees' the current path 
	SRP_JsonX_GoBack()
	
	// Now I can get the third employee's last name
	LastName = SRP_JsonX_Get('[3].lastname')
	
	// We can make the root the current path at any time
	SRP_JsonX_GoRoot()
	
	// When a value is null or not found, '' is returned by default. If you want a different value,
	// then set the DefaultIfNull parameter to your desired default
	Value = SRP_JsonX_Get('alwaysnull', 'This is null!!!')
	
	// We can also query the document for information (remember, we are currently at the root)
	NumEmployees = SRP_JsonX_Count('employees')
	MemberExists = SRP_JsonX_Has('employees[2].age')
	MemberNames  = SRP_JsonX_Members('employees[3]')
	Type         = SRP_JsonX_Type('alwaysbool')
	Numbers      = SRP_JsonX_Values('nums', ',')
	
	// We can also modify the document
	SRP_JsonX_Clear('nums')
	SRP_JsonX_Delete('employees[2]')
	SRP_JsonX_Sort()
	
// When we're done, we end it. Note that we can call this as a subroutine when we don't want json returned
SRP_JsonX_End()

Debugging

Given that SRP JsonX relies so heavily on state, it's important to give you the tools you need to troubleshoot issues. If you want to view the current state of SRP JsonX, call SRP_JsonX_State or SRP_JsonX_Trace. Both routines give you the stack and the current paths for each document on the stack. The difference between the two is that SRP_JsonX_Trace displays a message box whereas SRP_JsonX_State returns the stack as an @FM delimited array suitable for viewing in the debugger.

When an SRP JsonX method fails, you can call SRP_JsonX_Error immediately afterwards to get a detailed error message.

Reference

Here are links to all the SRP JsonX functions.

...