JSONReader

Introduction

vtkJSONReader is a source object that reads JSON files.

Methods

extend

Method used to decorate a given object (publicAPI+model) with vtkJSONReader characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues IJSONReaderInitialValues No (default: {})

getNumberOfOutputPorts

getUrl

Get the url of the object to load.

invokeBusy

Argument Type Required Description
busy Boolean Yes

isBusy

loadData

Load the object data.

Argument Type Required Description
options IJSONReaderOptions No

newInstance

Method used to create a new instance of vtkJSONReader

Argument Type Required Description
initialValues IJSONReaderInitialValues No for pre-setting some of its content

onBusy

Argument Type Required Description
callback Yes

parseAsText

Parse data as text.

Argument Type Required Description
content String Yes The content to parse.

requestData

Argument Type Required Description
inData Yes
outData Yes

setUrl

Set the url of the object to load.

Argument Type Required Description
url String Yes the url of the object to load.
option IJSONReaderOptions No The JSON reader options.

Source

index.d.ts
import { vtkAlgorithm, vtkObject, vtkSubscription } from "../../../interfaces";


interface IJSONReaderOptions {
binary?: boolean;
compression?: string;
progressCallback?: any;
}

/**
*
*/
export interface IJSONReaderInitialValues { }

type vtkJSONReaderBase = vtkObject & Omit<vtkAlgorithm,
| 'getInputData'
| 'setInputData'
| 'setInputConnection'
| 'getInputConnection'
| 'addInputConnection'
| 'addInputData'>;

export interface vtkJSONReader extends vtkJSONReaderBase {

/**
*
*/
getNumberOfOutputPorts(): number;

/**
* Get the url of the object to load.
*/
getUrl(): string;

/**
*
* @param {Boolean} busy
*/
invokeBusy(busy: boolean): void;

/**
*
*/
isBusy(): number;

/**
* Load the object data.
* @param {IJSONReaderOptions} [options]
*/
loadData(options?: IJSONReaderOptions): Promise<any>;

/**
*
* @param callback
*/
onBusy(callback: (busy: boolean) => any): vtkSubscription;

/**
* Parse data as text.
* @param {String} content The content to parse.
*/
parseAsText(content: string): void;
/**
*
* @param inData
* @param outData
*/
requestData(inData: any, outData: any): void;

/**
* Set the url of the object to load.
* @param {String} url the url of the object to load.
* @param {IJSONReaderOptions} [option] The JSON reader options.
*/
setUrl(url: string, option?: IJSONReaderOptions): Promise<string>;
}

/**
* Method used to decorate a given object (publicAPI+model) with vtkJSONReader characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {IJSONReaderInitialValues} [initialValues] (default: {})
*/
export function extend(publicAPI: object, model: object, initialValues?: IJSONReaderInitialValues): void;

/**
* Method used to create a new instance of vtkJSONReader
* @param {IJSONReaderInitialValues} [initialValues] for pre-setting some of its content
*/
export function newInstance(initialValues?: IJSONReaderInitialValues): vtkJSONReader;


/**
* vtkJSONReader is a source object that reads JSON files.
*/
export declare const vtkJSONReader: {
newInstance: typeof newInstance;
extend: typeof extend;
}
export default vtkJSONReader;
index.js
import macro from 'vtk.js/Sources/macros';

// ----------------------------------------------------------------------------
// vtkJSONReader methods
// ----------------------------------------------------------------------------

function vtkJSONReader(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkJSONReader');

// Internal method to fetch Array
function fetchData(url, option = {}) {
return model.dataAccessHelper.fetchText(publicAPI, url, option);
}

// Set DataSet url
publicAPI.setUrl = (url, option = {}) => {
model.url = url;

// Fetch metadata
return publicAPI.loadData(option);
};

// Fetch the actual data arrays
publicAPI.loadData = (option = {}) =>
fetchData(model.url, option).then(publicAPI.parseAsText);

publicAPI.parseAsText = (content) => {
if (!content) {
return false;
}
model.data = JSON.parse(content);
publicAPI.modified();
return true;
};

publicAPI.requestData = (inData, outData) => {
outData[0] = model.data;
};

// return Busy state
publicAPI.isBusy = () => false;

publicAPI.getNumberOfOutputPorts = () => model.numberOfOutputs;
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
// url: null,
};

// ----------------------------------------------------------------------------

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

// Build VTK API
macro.obj(publicAPI, model);
macro.get(publicAPI, model, ['url']);
macro.algo(publicAPI, model, 0, 1);
macro.event(publicAPI, model, 'busy');

// Object methods
vtkJSONReader(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(extend, 'vtkJSONReader');

// ----------------------------------------------------------------------------

export default { newInstance, extend };