Type Override example

At the core of the JSON Toolkit is a method that maps baja object types to JSON. This determines, for example, how any encountered BControlPoint, Facets, BAbsTime etc. should be encoded in the output.

The payload includes many variations for the supported Niagara types. Our approach to accommodating this is to allow a developer or power user the ability to override specific types as they are converted to JSON.

For a small JsonSchema, the example in the jsonToolkit palette demonstrates how to use a program object[^1] to replace units:

/**
   * Allows Json types to to be overridden when placed under JsonSchema/config/overrides/
   */
  public BValue onOverride(final BValue input)
  {
    if (input instanceof BUnit)
    {
      javax.baja.units.UnitDatabase unitDB = javax.baja.units.UnitDatabase.getDefault()
      javax.baja.units.UnitDatabase.Quantity quantity = 
      unitDB.getQuantity(input.as(BUnit.class)) 
      if (quantity != null)
      {
        return BString.make(input.as(BUnit.class).getSymbol() + ":" + quantity.getName())
      }
    }

    // If we can't override the value then just return it as we found it
    return input
  }

[^1]: To improve maintainability and station loading time in the event that a program object is duplicated repeatedly, use the ProgramBuilder.

To use the program, drag this component into the Config > Overrides folder of the schema.

Figure 24.   TypeOverride component in jsonSchema
Image

Developers could also override the doOverride(BValue value) method in their own BTypeOverride variant.