T4EA  AIG_19.2 C8891 A24697
Teamcenter Gateway for Enterprise Applications API Reference
safejson Namespace Reference

Functions

 createSchema schemaOut input
 
 dict2json in ?schema?
 Turn a data object represented as a Tcl dict into its JSON counterpart. More...
 
 string2json in
 Turn a string into a representation that can safely be embedded into a JSON object. More...
 

Detailed Description

This namespace contains functions used to convert data objects represented as Tcl dicts into their JSON counterparts and vice versa.

Function Documentation

◆ createSchema()

safejson::createSchema   schemaOut input  

Create a dict that represents a schema from a given input string and write it to the variable whose name is given as well.

The schema dict is described at the dict2json command. It has two entries:

  1. type with the valuedict
  2. details where the value is a dict created from the input string by removing comments and redundant space characters
Parameters
schemaOutname of a variable to receive the schema dict
inputstring to be converted into the details dict of the schema dict

◆ dict2json()

safejson::dict2json   in ?schema?  

Turn a data object represented as a Tcl dict into its JSON counterpart.

Parameters
inthe dict be converted into JSON
schemaa dict specifying a schema. If omitted the dict is searched for the :schema entry. If given the parameter takes precedence over a :schema entry.
Returns
the JSON representation

In order to create a JSON representation of a possibly nested hierarchy of dicts which may contain numbers, strings, boolean values, lists of them, etc. the types of the dict entries need to be know. A dict entry is a key value pair in a dict. The type information is required because the formatting of the JSON output depends on it.

The type information needs to be provided by the caller of this command in some way.

There are two ways to do so:

  1. Keys can be in the format NAME:TYPE with TYPE representing the type of the entry. Possible types are listed below.

  2. A dict may contain a so called schema reference. This is an entry with the special :schema key. The corresponding value evaluates to another dict providing the type information for all other entries including any nested dicts. Nested dicts may also contain their own schema references so you can easily compose dicts from other dicts.

    A schema is in turn a dict with two optional entries:

    1. type denotes the type of the corresponding object. Possible types are listed below.

    2. details is a dict that recursively specifies the schema of nested dicts if the type is dict or dicts.

    The keys of the details dict are identical to those contained in the dicts it describes.

If a schema is given keys must not contain type suffixes. Entries that are not covered by the schema are omitted.

If no type is given string is assumed.

The following types can be specified:

string - The value is considered a string and will be enclosed in double quotes. Quotes and backslashes inside the string are escaped.

strings - The value is considered a list of strings and its items will be enclosed in double quotes. Quotes and backslashes inside the string are escaped.

safe - The value is considered a string and will be enclosed in double quotes. Quotes and backslashes inside the string are not escaped.

tohex - The value is considered a string and will be enclosed in double quotes after being converted into the base16 format.

allsafe - The value is considered a list of strings and its items will be enclosed in double quotes. Quotes and backslashes inside the string are not escaped.

tohex - The value is considered a string and will be enclosed in double quotes after being converted into the base16 format.

alltohex - The value is considered a list of strings and its items will be enclosed in double quotes after being converted into the base16 format.

num - The value is considered a number and will not be enclosed in double quotes.

bool - The value is considered a boolean literal and will not be enclosed in double quotes. 0, false, FALSE, and False will be translated to false, all other values to true.

nums - The value is considered a list of numbers and its items will not be enclosed in double quotes.

bools - The value is considered a list of boolean literals (see bool) and its items will not be enclosed in double quotes.

dict - The value is considered a dict and will recursively be prepared.

dicts - The value is considered a list of dicts and its items will be recursively prepared.

◆ string2json()

safejson::string2json   in  

Turn a string into a representation that can safely be embedded into a JSON object.

Parameters
inthe string to convert
Returns
a string to be embedded into a JSON object