ValueTransformers

SmartJSONEditor edited this page Oct 10, 2018 · 6 revisions

For any String, Number, Boolean JSON tree node in Smart Json Editor one or more embedded or user defined value transformers can be appended.

Value transformers let you temporary override the editor value to process many kind of customizations like:

  • Generating fake testing data
  • Search & replace values
  • Combining values
  • Many other

How value transformers work

Value transformers is a simple Javascript code that interact with Smart Json Editor and passes the converted value back to editor. This value is generally stored and cached therefore your transformers in most cases will execute and cache values for all associated editor value fields in single operation upon editing parameters or code.

Remember to use Save and Execute button to:

  • Save your modified script text
  • Generate and cache values for all editor fields with associated ValueTransformer.

Smart Objects

Smart objects with value nodes will share single instance of ValueTransformer. You only use single editable instance on all value nodes in Smart Object.

You can use value transformers on childs of regular objects, however using smart objects is the most recommended. You are not allowed to add value transformer in multi selection !

Remember :

  • Only single instance is used in Smart Objects.
  • Use Smart objects in Array container for best use as possible.
  • Use arrayIndex to randomize or identify the position of smartObject value node.

Using value transformers in Smart Objects is the most preffered use for value ValueTransformers.

Basic Value Transformer

Quick script:

var ValueTransformer = function () {
    
    this.displayName = "Transform value";
    this.shortDescription = "My value transformer"
    this.transform = function (inputValue, jsonValue, arrayIndex, parameters, info) {
        return inputValue;
    };
}

function sjeClass() {
    return new ValueTransformer();
}

Default script:

/* Documentation : https://github.com/SmartJSONEditor/PublicDocuments/wiki/ValueTransformers */
var ValueTransformer = function () {

    this.displayName = "Transform value";
    this.shortDescription = "My value transformer."
    this.parameters = function () {
        return [];
    }
    // Requiered function transform
    // @param {String} inputValue - Value from editor or value generated from previous value transformer in queue.
    // @param {String} jsonValue - Original stored value in json editor.
    // @param {Number} arrayIndex - Index of this value node relative to first array container.
    // @param {Object} parameters - Your defined parameters as object.
    // @param {Object} info - Editor passed object with extra information.
    // @returns {String} - Override value for display or next value plugin.
    this.transform = function (inputValue, jsonValue, arrayIndex, parameters, info) {
        return inputValue;
    };
}
// Requiered global function
function sjeClass() {
    return new ValueTransformer();
}
Parameter/function Requiered Type Returns Description
displayName yes String none ValueTransformer display name. Will be presented in UI
shortDescription yes String none Short description of value transformer, use more detailed description.
parameters no Function Array of Objects Define your Interface parameters.
transform yes function String Main transform/generate function. Use transform logic and return String/Number/Boolean/Null representation back to editor.
isCacheDisabled no Boolean none Set to true if your ValueTransformer cannot cache values such timestamp generator. If you set to true, editor will process the ValueTransformer script on every display, webserver request etc.
isEditingDisabled no Boolean none Set to true you do not want to edit anymore the ValueTransformer in UI.
disabledValueTypes no Array Array of integers Prohibit use on specific value types. Return 2,3,4 as String, Number, Boolean to indicate forbidden types
disableLiveRefresh no Boolean none Prohibit use of DisableCache function as plugin uses remote fetch or other logic that is incompatible with live refresh

disableLiveRefresh

Main Function (transform)

this.transform = function (inputValue, jsonValue, arrayIndex, parameters, info) {
    return inputValue
};

Transform script function is a main transform function. The parameters are pased from Swift JSON Editor and you are able to transform the value using simple javascript.

inputValue

Input value is a parameter value passed from Swift JSON Editor as a main value to use for transform operations. This value is a result of previous value ValueTransformer output or output directly of the value from outline if this is a first value ValueTransformer. You should use this value as it is the input for transforming operations.

jsonValue

Json value is a parameter value that is representation of the inputted Json value in editor. This value represent the original value that actually exists and is passed unmodified to every value ValueTransformer in a row.

arrayIndex

Array index position of the first parent Array container. Use this value as a base for randomization or iteration.

parameters

If you define UI parameters, this parameter will contain Object of all parameter values defined by user. Read more about constructing the UI elements and usage in separate section here...

info

Object that defines other properties relevant to script such info.jsonNode (JsonNode class) property of additional information that can be used in script.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.