Custom query style

Third–party systems may require query results to be formatted in a manner other than the options provided in the JSON Toolkit.

To render query data differently, extend BQueryResultWriter and register the class as an agent on jsonToolkit:JsonSchemaBoundQueryResult.

This example shows how to format the contents of the QueryResultHolder for an external system:

package com.tridiumx.jsonToolkit.outbound.schema.query

import static com.tridiumx.jsonToolkit.outbound.schema.support.JsonSchemaUtil.toJsonType

import java.util.concurrent.atomic.AtomicInteger
import javax.baja.nre.annotations.AgentOn
import javax.baja.nre.annotations.NiagaraType
import javax.baja.sys.BString
import javax.baja.sys.Sys
import javax.baja.sys.Type

import com.tridiumx.jsonToolkit.outbound.schema.query.style.BQueryResultWriter
import com.tridium.json.JSONWriter

/**
 * An example custom query result writer.
 *
 * @author Nick Dodd
 */
@NiagaraType(agent = @AgentOn(types = "jsonToolkit:JsonSchemaBoundQueryResult"))
public class BCowSayJson extends BQueryResultWriter
{
/*+ ------------ BEGIN BAJA AUTO GENERATED CODE ------------ +*/
/*@ $com.tridiumx.jsonToolkit.outbound.schema.query.style.BObjectsArray(4046064316)1.0$ @*/
/* Generated Thu Dec 13 11:24:58 GMT 2018 by Slot-o-Matic (c) Tridium, Inc. 2012 */

////////////////////////////////////////////////////////////////
// Type
////////////////////////////////////////////////////////////////
  
  @Override
  public Type getType() { return TYPE }
  public static final Type TYPE = Sys.loadType(BCowSayJson.class)

/*+ ------------ END BAJA AUTO GENERATED CODE -------------- +*/

  @Override
  public BString previewText()
  {
    return BString.make("A demonstration result writer")
  }

  @Override
  public void appendJson(JSONWriter jsonWriter, QueryResultHolder result)
  {
    jsonWriter.object()

    try
    {
      jsonWriter.key("mooo01").value("____________________________")
      headerCsv(jsonWriter, result)
      dataCsv(jsonWriter, result)
      jsonWriter.key("mooo02").value("----------------------------")
      jsonWriter.key("mooo03").value("    \\   ^__^               ")
      jsonWriter.key("mooo04").value("     \\  (oo)\\_______      ")
      jsonWriter.key("mooo05").value("         (__)\\       )\\/\\")
      jsonWriter.key("mooo06").value("              ||----w |     ")
      jsonWriter.key("mooo07").value("              ||     ||     ")
    }
    finally
    {
      jsonWriter.endObject()
    }
  }

  private void headerCsv(JSONWriter jsonWriter, QueryResultHolder result)
  {
    jsonWriter.key("columns").value(String.join(",", result.getColumnNames()))
  }

  private void dataCsv(JSONWriter jsonWriter, QueryResultHolder result)
  {
    AtomicInteger rowCount = new AtomicInteger()

    result.getResultList().forEach( map - {

      jsonWriter.key("data" + rowCount.incrementAndGet())
      jsonWriter.array()
      try
      {
        map.values()
          .forEach(value - jsonWriter.value(toJsonType(value, getSchema().getConfig())))
      }
      finally
      {
        jsonWriter.endArray()
      }
    })

    processChildJsonMembers(jsonWriter, false)  // append any nested members content 
    to the json
  }
}