company logo

TypedString - Instance and key value

Typed strings are ODABA strings based on a complex data type definition. By means of the complex data type definition, string values can be interpreted and values from the string can be assigned to complex instances.

Typed strings can be provided in different string formats. Default is ESDFString, the extended self delimiter format (ESDF), which is an extension of the widely used CSV format.

Even though, ';', '|', or tabulators (\t) can be used as property delimiters in ESDF strings simultaneously, it is suggested to use ';' as property delimiter, since this is the separator used, when typed strings are created by the system.

Although the specialization to ODABA::Key can be used with all defined string types, the default type for Key is CSVString and the default property delimiter is '|'.

Typed strings can be created from string variables or constants, which have to correspond syntactically string type set in the typed string. Later, when applying the string on a property or value handle, the type definition is retrieved from the handle in order to convert the string properly.

Typed strings need not to contain all properties of the related complex data type. Missing properties (values) will be created and initialized as empty values.

Notes:

Instead of object interchange format (OIF), ESDF is used as default, which is structured in a similar way but does nor require property names, which is sufficient for internal use. In order to exchange data between databases, however, OIF should be used by setting the string type to OIFString (stringType(OIFString)), instead.

Type definition

In order to convert instances or keys into string or reverse, a type definition must have been set in the typed string. Type definitions are usually set, when the instance is passed as parameter to a Property or Value function.

Since type definition may change always when passed to a function, one has to be aware, that the currently associated type is the type resulting from the last operation. When using typed strings for data conversion (string to instance or reverse), it is a save way to set the required type definition before calling conversion functions.

Instance conversion

Instances can be converted from internal to string format and reverse. Since ODABA supports several string formats, converting from internal to string depends on the selected string type (stringType()).

When assigning a string value to a typed string, the string is analyzed and the string type is derived from the string value. When no valid string has been passed, an exception is thrown. String types are recognized as follows:

  • xml - first non-whitespace character is <
  • oif - first token is an identifier
  • esdf - first token is { or ( or constant

Changing the string type (e.g. stringType(OIFString)) will automatically change the string value for the instance, when a valid type definition has been set.

In order to set the string type for a a string value to be set, the string type should be set before assigning the string value.

// fragment: TypeDefinition td;

  Instance   inst;

  inst.typeDefinition(td);

  inst.setType(ESDFString);

  inst = "Miller|Paul"; // assign esdf string value

  inst.setType(OIFString); // convert to OIF string

  output(inst); // name = "Miller"; first_name = "Paul";

String with unknown type

Typed strings allow parsing several types of formatted object data (XML, JSON, OIF, ESDF and CSV) regardless on the specific string format. All formats provide values at elementary data on lowest level, i.e. values are atomic instances.

collection := instance(*)

instance := property(*)

property := value | collection | instance

Depending on the exchange type, properties and instances may support names, which may be obtained by elementName(). Depending on the string format, additional information is available for properties and instances.

  • name - a property name is available in JSON, XML and OIF. Instance names (type names) are supported in OIF and XML.
  • locator - an instance locator (key or identifier) is supported in OIF and ESDF
  • position - in case of arrays/collections, the array position (relative 0) is provided.

In order to get instances and properties from typed strings with unknown type, one may create a VOID string and assigning string value and format. Elements of the string may be retrieved sequentially by alternativly calling nextelement() or nextValue().

Since nextValue() and nextElement() provide a typed (sub)string, this may be checked for subordinated elements etc. The type of the typed string (instance, collection or value) may be obtained by appropriate TypedString properties. The top string to be analyzed must always be an instance, a value or an array (collection of instances or values).

Values for selected instances and properties are returned in the passed type string. In addition, name and locator may be obtained for several string formats, String formats not supporting names or locators return an empty string when being requested.

// well known nesting level

void funct(odaba::Property &person, odaba::String xString) {

  odaba::TypedString   tsTop(xString,JSON_String); // top string

  odaba::TypedString   tsInstance;

  odaba::TypedString   tsProperty;

  odaba::TypedString   tsValue;

  odaba::Value         vh;

  odaba::String        sName;

  while ( tsInstance.nextElement(tsTop) ) {        // get instance

    pos = 0;

    while ( tsProperty.nextElement(tsInstance) ) { // get properties

      sName = tsProperty.elementName();

      vh.open(person,sName);

      while ( tsValue.nextElement(tsProperty) ) {    // get property values

        if ( tsValue != "null" )

          vph(pos) = tsValue;

      }

    }

  }

}

// generic nesting level

void funct(odaba::Property &person, odaba::String xString) {

  odaba::TypedString   tsTop(xString,JSON_String); // top string

  odaba::TypedString   tsValue;

  odaba::Value         vh;

  while ( tsValue.nextValue(tsTop) ) {        // get value recursive

    if ( tsValue != "null" ) {

      vh.open(tsValue.elementPath());

      vph(pos) = tsValue;

      vh.close();

    }    

  }

}

Function Groups
    Functions