Core functionality for converting JSON configurations into deck.gl layer props with class instantiation, function parsing, and constant resolution.
Main converter class that processes JSON configurations and converts them into JavaScript objects suitable for deck.gl.
/**
* Main converter class for processing JSON configurations into deck.gl props
*/
class JSONConverter {
constructor(props: JSONConverterProps);
/**
* Converts JSON to deck.gl props, handling class instantiation, function parsing, and constant resolution
* @param json - JSON configuration to convert (object or JSON string)
* @returns Converted JavaScript objects ready for deck.gl
*/
convert(json: any): any;
/**
* Updates converter configuration and callbacks
* @param props - New properties to set
*/
setProps(props: JSONConverterProps): void;
/**
* Merges additional configuration into existing configuration
* @param config - Configuration object to merge
*/
mergeConfiguration(config: any): void;
/** Cleanup method for resource disposal */
finalize(): void;
/** @deprecated Use convert() instead */
convertJson(json: any): any;
}
interface JSONConverterProps {
/** Configuration object or JSONConfiguration instance */
configuration: JSONConfiguration | Record<string, any>;
/** Optional callback triggered when JSON changes */
onJSONChange?: Function;
}Usage Examples:
import { JSONConverter, JSONConfiguration } from "@deck.gl/json";
import { ScatterplotLayer } from "@deck.gl/layers";
// Basic conversion
const converter = new JSONConverter({
configuration: {
classes: { ScatterplotLayer }
}
});
const jsonConfig = {
"layers": [{
"@@type": "ScatterplotLayer",
"id": "my-layer",
"data": []
}]
};
const result = converter.convert(jsonConfig);
// Returns: { layers: [ScatterplotLayerInstance] }
// With JSON string input
const jsonString = JSON.stringify(jsonConfig);
const result2 = converter.convert(jsonString);
// Merging additional configuration
converter.mergeConfiguration({
constants: { MY_CONSTANT: 42 }
});The JSONConverter supports several special conversion features:
Objects with @@type property are instantiated as classes:
// JSON input
{
"@@type": "ScatterplotLayer",
"id": "layer-1",
"data": []
}
// Converts to: new ScatterplotLayer({ id: "layer-1", data: [] })String values starting with @@= are converted to functions:
// JSON input
{
"getRadius": "@@=d => d.radius * 2"
}
// Converts to: { getRadius: (d) => d.radius * 2 }String values starting with @@# resolve to constants or enums:
// JSON input
{
"coordinateSystem": "@@#COORDINATE_SYSTEM.DEFAULT"
}
// Resolves to: { coordinateSystem: COORDINATE_SYSTEM.DEFAULT }Objects with @@function property execute registered functions:
// JSON input
{
"@@function": "calculateRadius",
"base": 10,
"multiplier": 2
}
// Executes: calculateRadius({ base: 10, multiplier: 2 })The converter processes JSON recursively with the following steps:
@@type key@@function keyThe converter includes built-in error handling:
The converter implements several security measures:
CallExpression nodes in parsed expressions