Formats the given JSON Entity into a standardized JSON string.

Syntax

Result = SRP_Json(Handle, "Stringify", Style, TabSize, Sorted, EscapeSolidus)

Returns

The JSON output.

Parameters

ParameterDescription
HandleHandle to a JSON Entity. Required.
StyleThe formatting style of the final output: Fast, DropNulls, or Styled. Optional.
TabSizeThe size of tabs in characters. Optional.
SortedDetermines if object members should be sorted in the output. Optional.
EscapeSolidusDetermines if '\/' represents the solidus character. Optional.

Remarks

The Stringify service turns a JSON entity into standard JSON, which is just a string containing the JSON contents that can be consumed by clients that are JSON aware, particularly Javascript. The Style parameter allows you to control how the JSON looks. The "Styled" option is a tad slower to build but it puts line breaks and indents in the JSON to make it human readable. The "Fast" option uses no line breaks at all, making it faster to build and smaller. Smaller JSON means less waiting when it's sent over the internet. To make the fast JSON even smaller (and a tiny bit faster), you can use the "DropNulls" option which is the same as "Fast" but with all instances of 'null' replaced with no text at all. Technically, it's not standard JSON to omit nulls, but all browsers seems to handle it just fine. The default style is "Fast".

If you are styling the output, you can optionally set the "TabSize" parameter to establish the size of tabs in characters. The default is 4.

Whether you style the output or not, you can opt to have object members sorted in the output. By definition, JSON object members are unordered, and you should never expect a particular member to be in a particular order. However, for large objects, sorting members could be a nice convenience for some users. Set "Sorted" to 1 to make it happen.

The "EscapeSolidus" parameter was added to give you a choice on how to handle the '\/' sequence. In the JSON spec, this is supposed to be translated into unicode character 0x002F, also known as the solidus character. The JSON community, however, treats this as optional. By default, the parameter is 0, but you can set it to 1 if you need the solidus character. The solidus character is very rarely needed, which is why the community just ignores it most of the time. Hence, the default is 0.

Example

// Create a JSON array, add elements to it, and Stringify it fast
If SRP_Json(ArrayHandle, "New", "Array") then
   SRP_Json(ArrayHandle, "AddValue", "12345")
   SRP_Json(ArrayHandle, "AddValue", 67890, "Number")
   SRP_Json(ArrayHandle, "AddValue", 1, "Boolean")
   JSON = SRP_Json(ArrayHandle, "Stringify", "Fast")
   SRP_Json(ArrayHandle, "Release")
end

// Create a JSON object, add members to it, and Stringify it pretty
If SRP_Json(ObjectHandle, "New", "Object") then
   SRP_Json(ObjectHandle, "SetValue", "name", "John Doe")
   SRP_Json(ObjectHandle, "SetValue", "city", "Washington D.C.")
   JSON = SRP_Json(ObjectHandle, "Stringify", "Styled")
   SRP_Json(ObjectHandle, "Release")
end

See Also

Parse

  • No labels