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 documentsName it whatever. Names are for nameddocumentation only forand debuggingnever purposesused onlylater.
SRP_JsonX_Begin('MyDocument', '{')
	
	// Since we are in an object, the first parameter should bewe pass a member name followedand by a value.
	// In this case, the value is "[", which starts a new array and makes that array the current element
    SRP_JsonX('employees', '[')

		// Since we are in an array, the first parameter should bejust pass a value. In this case, we are starting a new object
		SRP_JsonX('{')
			SRP_JsonX('firstname', 'John')	; // Since we're in an object, thewe firstpass parametername is a member name and the second is its valueand 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 element 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 element, which is the employees array
		SRP_JsonX('{"firstname":"Peter", "lastname":"Jones", "age":43}')

	// This line closes the array and sets the current element 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 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')

...

pretty formatted json and end it at the same time
Json = SRP_JsonX_End('Pretty')

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. 

To get the final json output, we call called 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.

Let's look at another example.

Code Block
languagebp
titleExample 2
SRP_JsonX_BeginString('MyDocument', '{', JsonxPretty$)
    SRP_JsonX('employees', '[')
        SRP_JsonX('{')
            SRP_JsonX('firstname', 'John')
            SRP_JsonX('lastname', 'Doe')
            SRP_JsonX('age', 21)
        SRP_JsonX('}')
        SRP_JsonX('{')
            SRP_JsonX('firstname', 'Anna')
            SRP_JsonX('lastname', 'Smith')
            SRP_JsonX('age', 32)
        SRP_JsonX('}')
        SRP_JsonX('{"firstname":"Peter", "lastname":"Jones", "age":43}')
    SRP_JsonX(']')
    SRP_JsonX('count', 4)
    SRP_JsonX('active', 1, 'Bool')
    SRP_JsonX('alwaysnull')
    SRP_JsonX('alwaysstring', 4.321, 'String')
Json = SRP_JsonX_End()

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 a little 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:

...