Discuss this help topic in SecureBlackbox Forum

Handle simple types

JSON supports values of four simple types: boolean, number, string and null. All of these value types are handled by TElJsonValue class. This class has two properties which show the current state of the entity: ValueType (TSBJsonType) and Value (string). ValueType property returns the current type of the stored value. Value property returns the string representation of the current value.

There are two ways to create a new entity for a certain type:

  1. Call an appropriate constructor and pass the necessary value to it:
    // creates a null entity; ValueType is jsonNull, Value is "null"
    var nullValue = new TElJsonValue();
    
    // creates a boolean entity; ValueType is jsonBoolean, Value is "false"
    var boolValue = new TElJsonValue(false);
    
    // creates a number entity; ValueType is jsonNumber, Value is "100"
    var intValue = new TElJsonValue(100);
    
    // creates a number entity; ValueType is jsonNumber, Value is "236.94"
    var numberValue = new TElJsonValue(236.94);
    
    // creates a string entity; ValueType is jsonString, Value is "test"
    var stringValue = new TElJsonValue("test");
    
  2. Set the necessary value to the appropriate property:
    var value = new TElJsonValue();
    
    // set a boolean value; ValueType is jsonBoolean, Value is "true"
    value.AsBoolean = true;
    
    // set an integer value; ValueType is jsonNumber, Value is "100"
    value.AsInteger = 100;
    
    // set a double value; ValueType is jsonNumber, Value is "236.94"
    value.AsNumber = 236.94;
    
    // set a string value; ValueType is jsonString, Value is "test"
    value.AsString = "test";
    
    // set a null value; ValueType is jsonNull, Value is "null"
    value.IsNull = true;
    

To get the current type of the value stored in a TElJsonValue object, check its ValueType property. Also it's possible to use its IsBoolean, IsNull, IsNumber and IsString properties - they return True if the value is of correspondent type.

There are four properties that return the value stored in a TElJsonValue object:

  • AsBoolean property returns the stored value as a boolean; if TElJsonValue.ValueType is not jsonBoolean and TElJsonValue.Value cannot be converted to a boolean, an exception is raised
  • AsInteger property returns the stored value as an integer; if TElJsonValue.ValueType is jsonNumber, a rounded value is returned; if TElJsonValue cannot be converted to an integer, an exception is raised
  • AsNumber property returns the stored value as a number (double/extended); if TElJsonValue.ValueType is not jsonNumber and TElJsonValue cannot be converted to a number (double/extended), an exception is raised
  • AsString property returns the same as the Value property

How To articles about basic JSON questions

Discuss this help topic in SecureBlackbox Forum