Skip to content

Custom Data Handler

Jack Brookes edited this page Jun 3, 2021 · 7 revisions

If any of the current Data Handler (listed here) do not meet your needs (e.g. you wish to upload data to a different online service) then you can create a custom Data Handler. This would be used to handle all data operations UXF makes (e.g. saving trial results, tracker outputs, etc).

Note: If you wish to output custom data e.g. a custom text file, but are OK with the current Data Handlers, see DIY data collection.

Custom Data Handler Template

To make a new Data Handler, you must inherit from the DataHandler class and override the required methods. The methods required to override are:

  • HandleDataTable
  • HandleJSONSerializableObject (2 methods for each of the .json types, one with List<object> and the other with Dictionary<string, object> arguments)
  • HandleText
  • HandleBytes

Then, you can attach your script to an object and reference it in the Data Handlers field of the Session inspector.

Here is a template for a custom data handler that simply prints the data as text to the console:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

/// This is an example custom data handler. It implements the required DataHandler methods and just prints out data to the console. 
/// You can copy this script, but do some other action instead of printing out the data.
/// You can attach this script to a GameObject then reference it in the Data Handling tab of the UXF Session component.

public class ExampleCustomDataHandler : DataHandler
{

    public override bool CheckIfRiskOfOverwrite(string experiment, string ppid, int sessionNum, string rootPath = "")
    {
        return false; // You could write a condition to return true when a ppid already exists.
    }

    public override void SetUp()
    {
        // No setup is needed in this case. But you can add any code here that will be run when the session begins.
    }

    public override string HandleDataTable(UXFDataTable table, string experiment, string ppid, int sessionNum, string dataName, UXFDataType dataType, int optionalTrialNum = 0)
    {
        // get data as text
        string[] lines = table.GetCSVLines();
        string text = string.Join("\n", lines);

        // here we "write" our data, you could upload to database, or do whatever.
        Debug.Log(text);

        // return a string representing the location of the data. Will be stored in the trial_results output.
        return "Data printed to console."; 
    }

    public override string HandleJSONSerializableObject(List<object> serializableObject, string experiment, string ppid, int sessionNum, string dataName, UXFDataType dataType, int optionalTrialNum = 0)
    {
        // get data as text
        string text = MiniJSON.Json.Serialize(serializableObject);

        // here we "write" our data, you could upload to database, or do whatever.
        Debug.Log(text); 

        // return a string representing the location of the data. Will be stored in the trial_results output.
        return "Data printed to console."; 
    }

    public override string HandleJSONSerializableObject(Dictionary<string, object> serializableObject, string experiment, string ppid, int sessionNum, string dataName, UXFDataType dataType, int optionalTrialNum = 0)
    {
        // get data as text
        string text = MiniJSON.Json.Serialize(serializableObject);

        // here we "write" our data, you could upload to database, or do whatever.
        Debug.Log(text); 

        // return a string representing the location of the data. Will be stored in the trial_results output.
        return "Data printed to console."; 
    }

    public override string HandleText(string text, string experiment, string ppid, int sessionNum, string dataName, UXFDataType dataType, int optionalTrialNum = 0)
    {
        // here we "write" our data, you could upload to database, or do whatever.
        Debug.Log(text); 

        // return a string representing the location of the data. Will be stored in the trial_results output.
        return "Data printed to console."; 
    }

    public override string HandleBytes(byte[] bytes, string experiment, string ppid, int sessionNum, string dataName, UXFDataType dataType, int optionalTrialNum = 0)
    {
        // here we "write" our data, you could upload to database, or do whatever.
        string text = System.Text.Encoding.UTF8.GetString(bytes);
        Debug.Log(text); 

        // return a string representing the location of the data. Will be stored in the trial_results output.
        return "Data printed to console."; 
    }

    public override void CleanUp()
    {
        // No cleanup is needed in this case. But you can add any code here that will be run when the session finishes.
    }

}

๐Ÿง  Core topics

โ“ More help


๐Ÿ‘ฉโ€๐Ÿ’ป Programming reference

Unit tests

Clone this wiki locally