InvalidJsonType class: Difference between revisions
No edit summary |
|||
(4 intermediate revisions by one other user not shown) | |||
Line 2: | Line 2: | ||
[[Json class|Json]] objects simulate JavaScript untyped variables. However, many Json functions only make sense for specific underlying Json data. For example, the [[Add (Json function)|Add function]] only makes sense if the underlying json object is an Array. If a function applied to a Json object is invalid for the underlying object, an <var>InvalidJsonType</var> exception is thrown. | [[Json class|Json]] objects simulate JavaScript untyped variables. However, many Json functions only make sense for specific underlying Json data. For example, the [[Add (Json function)|Add function]] only makes sense if the underlying json object is an Array. If a function applied to a Json object is invalid for the underlying object, an <var>InvalidJsonType</var> exception is thrown. | ||
To create an <var>InvalidjsonType</var> exception yourself, you typically use a SOUL <var>[[Exceptions#Using the Throw statement|Throw]]</var> statement with an <var> | To create an <var>InvalidjsonType</var> exception yourself, you typically use a SOUL <var>[[Exceptions#Using the Throw statement|Throw]]</var> statement with an <var>InvalidJsonType</var> <var>[[New_(InvalidJsonType_constructor)|New]]</var> constructor. This statement must be issued from within a method, and it can only be caught by the code that calls the method. For example, the following statement throws an <var>InvalidJsonType</var> exception: | ||
<p class="code">throw %(invalidJsonType):new(type=array, method="MyJsonMethod") | <p class="code">throw %(invalidJsonType):new(type=array, method="MyJsonMethod") | ||
</p> | </p> | ||
Line 8: | Line 8: | ||
Remember that you catch an exception with the <var>[[Exceptions#Try and Catch|Catch]]</var> statement; if an exception condition occurs outside a <var>Catch</var> for it, the request is cancelled. | Remember that you catch an exception with the <var>[[Exceptions#Try and Catch|Catch]]</var> statement; if an exception condition occurs outside a <var>Catch</var> for it, the request is cancelled. | ||
The <var>InvalidJsonType</var> class, like the Json class is available as of <var class="product">Model 204</var> 7.6. | The <var>InvalidJsonType</var> class, like the Json class, is available as of <var class="product">Model 204</var> 7.6. | ||
==The InvalidJsonTypes methods== | ==The InvalidJsonTypes methods== | ||
Line 19: | Line 19: | ||
<li>[[InvalidJsonType methods syntax|"InvalidJsonType methods syntax"]] is a single page that contains the syntax diagrams of all the methods in the class. | <li>[[InvalidJsonType methods syntax|"InvalidJsonType methods syntax"]] is a single page that contains the syntax diagrams of all the methods in the class. | ||
</ul> | </ul> | ||
==MethodName property== | |||
{{Template:InvalidJsonType:MethodName subtitle}} | |||
This <var>[[Classes and Objects#readWrite|ReadOnly]]</var> property returns a string that indicates the name of the failing method. | |||
===Syntax=== | |||
{{Template:InvalidJsonType:MethodName syntax}} | |||
====Syntax terms==== | |||
<table class="syntaxTable"> | |||
<tr><th>%string</th> | |||
<td>The name of the failing method.</td></tr> | |||
<tr><th>invalidJsonType</th> | |||
<td>A reference to an instance of an <var>[[InvalidJsonType_class|InvalidJsonType]]</var> object.</td></tr> | |||
</table> | |||
==Type property== | |||
{{Template:InvalidJsonType:Type subtitle}} | |||
This <var>[[Classes and Objects#readWrite|ReadOnly]]</var> property returns a [[JsonType enumeration]] value that indicates the underlying Json object type for the object against which the failing method was applied. | |||
===Syntax=== | |||
{{Template:InvalidJsonType:Type syntax}} | |||
====Syntax terms==== | |||
<table class="syntaxTable"> | |||
<tr><th>%jsonType</th> | |||
<td>The underlying Json object type for the object against which the failing method was applied.</td></tr> | |||
<tr><th>invalidJsonType</th> | |||
<td>A reference to an instance of an <var>[[InvalidJsonType_class|InvalidJsonType]]</var> object.</td></tr> | |||
</table> | |||
==New constructor== | ==New constructor== | ||
{{Template:InvalidJsonType:New subtitle}} | {{Template:InvalidJsonType:New subtitle}} | ||
This <var>Constructor</var> generates an instance of an <var>[[ | This <var>Constructor</var> generates an instance of an <var>[[InvalidJsonType_class|InvalidJsonType]]</var> exception. The <var>New</var> method format follows: | ||
===Syntax=== | ===Syntax=== | ||
{{Template:InvalidJsonType:New syntax}} | {{Template:InvalidJsonType:New syntax}} |
Latest revision as of 16:12, 23 August 2016
Json objects simulate JavaScript untyped variables. However, many Json functions only make sense for specific underlying Json data. For example, the Add function only makes sense if the underlying json object is an Array. If a function applied to a Json object is invalid for the underlying object, an InvalidJsonType exception is thrown.
To create an InvalidjsonType exception yourself, you typically use a SOUL Throw statement with an InvalidJsonType New constructor. This statement must be issued from within a method, and it can only be caught by the code that calls the method. For example, the following statement throws an InvalidJsonType exception:
throw %(invalidJsonType):new(type=array, method="MyJsonMethod")
Remember that you catch an exception with the Catch statement; if an exception condition occurs outside a Catch for it, the request is cancelled.
The InvalidJsonType class, like the Json class, is available as of Model 204 7.6.
The InvalidJsonTypes methods
The following are the available InvalidJsonType class methods.
Method | Description |
---|---|
MethodName | Type of object for invalid method |
New | Create a new InvalidJsonType object |
Type | Type of object for invalid method |
The methods in the class are described in the subsections that follow. In addition:
- "Notation conventions for methods" has information about the conventions followed.
- "InvalidJsonType methods syntax" is a single page that contains the syntax diagrams of all the methods in the class.
MethodName property
Type of object for invalid method (InvalidJsonType class)
This ReadOnly property returns a string that indicates the name of the failing method.
Syntax
%string = invalidJsonType:MethodName
Syntax terms
%string | The name of the failing method. |
---|---|
invalidJsonType | A reference to an instance of an InvalidJsonType object. |
Type property
Type of object for invalid method (InvalidJsonType class)
This ReadOnly property returns a JsonType enumeration value that indicates the underlying Json object type for the object against which the failing method was applied.
Syntax
%jsonType = invalidJsonType:Type
Syntax terms
%jsonType | The underlying Json object type for the object against which the failing method was applied. |
---|---|
invalidJsonType | A reference to an instance of an InvalidJsonType object. |
New constructor
Create a new InvalidJsonType object (InvalidJsonType class)
This Constructor generates an instance of an InvalidJsonType exception. The New method format follows:
Syntax
%invalidJsonType = [%(InvalidJsonType):]New( Type= jsonType, MethodName= string)
Syntax terms
%invalidJsonType | A reference to an instance of an InvalidJsonType object. |
---|---|
[%(InvalidJsonType):] | The class name in parentheses denotes a Constructor. |
jsonType | A JsonType enumeration value that indicates the underlying JSON type of the object to which the invalid method was applied. |
string | The name of the method that encountered the error. |
Usage notes
- Json object tree can be created from data received from an outside data source that was generated via the Json Parse function. If the external data source is considered 100% in terms of sending JSON data with a known format, the data could be extracted without worries about InvalidJsonType exceptions. However, if there is a concern that the data will not be exactly what is expected, one should either test each object for the expected datatype via the Type function or wrap the data extraction inside a Try/Catch InvalidJsonType. Obviously, the latter requires less effort and is more efficient. For example, if an application expects an order to come in as an array of objects with a product name, code, quantity, and price but the source is not 100% trustworthy, one can do something like:
try for %i from 1 to %order:count %name = %order(%i)("name"):stringValue %code = %order(%i)("code"):stringValue %quantity = %order(%i)("quantity"):numberValue %price = %order(%i)("price"):numberValue ... process the order item end for catch invalidJsonType ... deal with the error end try
The Try/Catch adds no overhead to the processing but makes it easy to trap a malformed request.