Versions Compared

Key

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

...

Code Block
languagebp
titleExample 1
$insert SRPJSONX

// Create a new document with a json object as the root. All documents are named for debugging purposes only
SRP_JsonX_Begin('MyDocument', '{')
	
	// Since we are in an object, the first parameter should be a member name followed by a value
	// In this case, the value is "[", which starts a new array and makes that array the current pathelement
    SRP_JsonX('employees', '[')

		// Since we are in an array, the first parameter should be a value. In this case, we are starting a new object
		SRP_JsonX('{')
			SRP_JsonX('firstname', 'John')	; // Since we're in an object, the first parameter is a member name and the second is its value
			SRP_JsonX('lastname', 'Doe')
			SRP_JsonX('age', 21)
		SRP_JsonX('}')

		// Note on the previous line that passing "}" closes an object and moves the current pathelement back to the parent, so when we call this next
		// line, we are back in the array. Once again, we'll add an employee object
		SRP_JsonX('{')
		    SRP_JsonX('firstname', 'Anna')
		    SRP_JsonX('lastname', 'Smith')
		    SRP_JsonX('age', 32)
		SRP_JsonX('}')

		// We can also pass json strings as a value, which get fully parsed and added to the current pathelement, which is currently the employees array
		SRP_JsonX('{"firstname":"Peter", "lastname":"Jones", "age":43}')

	// This line closes the array and sets the current pathelement back to the root object
    SRP_JsonX(']')

	// When you pass numbers as values, JsonX assumes you want that value to be unquoted in the final json
 	SRP_JsonX('count', 4)

	// To pass a boolean value, we need to add a hint parameter. Hints always come after values
 	SRP_JsonX('active', 1, 'Bool')

	// To set a something to null, omit the value. If you cannot omit the value, you can set the hint to 'Null'
 	SRP_JsonX('alwaysnull')

	// If you want a number to be quoted in the json, use the 'String' hint
 	SRP_JsonX('alwaysstring', 4.321, 'String')

// We're all done, so let's turn the document into pretty formatted json and end it at the same time
Json = SRP_JsonX_End('Pretty')

In this example, we start a new document called MyDocument. The document name is for debugging purposes only, so use whatever you want. The second parameter defines the new document's root. It must be "{" or "[". Note that this new document is now the active one and it's current path element is pointing to the root of the document.

SRP_JsonX is a special routine whose parameters are interpreted according to the current state. The comments above help explain how SRP_JsonX makes its decisions. As you can see, 

To get the final json output, we call SRP_JsonX_End. This routine does two things. It optionally returns the current document as a json string, then it removes the current document from memory and makes the previous document on the stack the current one.

...

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 pathelement. If we want to change it, we use the Go routine.
	// Let's make the second employee the current pathelement.
	SRP_JsonX_Go('employees[2]')
	
	// Now, if I want to get the last name, I must use a path relative to the current oneelement
	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 oneelement
	// In this case, 'employees' doesn't exist inside 'employees[2]'
	LastName = SRP_JsonX_Get('employees[3].lastname')
	
	// We can go back up the jsonhierarchy heirarchy one level at a time. This call makes 'employees' the current path element
	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 pathelement 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()

...

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 elements 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.

...