create, modify and distribute jsonfiles.
npm install grunt-jsonfilenpm install grunt-jsonfiles --save-dev
grunt.loadNpmTasks( "jsonfiles" );
grunt jsonfiles
javascript
const jsonfile = {
options: {
EOF: true
}
};
`
$3
The following example defines two templates in the options section of 'jsonfile'.
`javascript
// Templates are identified by name. This example defines two templates, which
// can be referenced by "tmpl1" and "tmpl2"
const jsonfile = {
options:{
templates: {
tmpl1: "config/template.json", // strings will be interpreted as path to
// json files, which will be required and
// used as template.
tmpl2: { // objects will directly be used as
pname1: 5, // template
pname2: true,
pname3: "value",
aproperty: "a aproperty will be deleted"
}
}
}
};
`
The following examples define templates directly within their targets.
`javascript
const BUILD = "...some path";
const jsonfile = {
target1: {
template: "config/template.json", // template is a string value, and does not
// reference a template in options.templates.
// Therefor it will be interpreted as path to
// a json file, which will be required.
dest: ${ BUILD }/file.json, // write the result to this file.
}
};
`
`javascript
const BUILD = "...some path";
const jsonfile = {
target1: {
template: { // a template object
pname1: 5,
pname2: true,
pname3: "value",
aproperty: "a aproperty will be deleted"
},
dest: ${ BUILD }/file.json, // write the result to this file.
}
};
`
$3
`javascript
// Target 'target1' references a template in options.templates
const BUILD = "...some path";
const jsonfile = {
options:{
templates: {
tmpl1: "config/template.json" // read the json file and use the resulting
// object as template.
}
},
target1: {
template: "tmpl1", // template is a string value, and references
// a template in options.templates which will
// be used.
dest: [ // write the result to all files in this array
${ BUILD }/1/file.json,
${ BUILD }/2/file.json,
${ BUILD }/3/file.json
]
}
};
`
$3
Setting a value to a property of a template can mean:
* creating a template property if it did not yet exist
* overwriting the value of an existing template property
`javascript
const BUILD = "...some path";
const jsonfile = {
target1: {
template: { // a template object
pname1: 5,
pname2: true,
pname3: { key: "value" },
aproperty: "a aproperty will be deleted"
},
dest: ${ BUILD }/file.json, // write the result to this file.
set: {
pname1: "value 5 replaced", // this will set template.pname1 to
// "value 5 replaced"
pname2: undefined, // this will set template.pname2 to
// undefined
pname3: { other: "instance" }, // this will set template.pname3 to
// another instance.
aproperty: null // this will set template.aproperty to
// null
}
};
`
$3
Merging properties into templates means
* inserting properties which did not yet exist
* setting values to properties that do exist
* deleting templates properties if value to merge is null
* mering will iterate into object trees
`javascript
const BUILD = "...some path";
const jsonfile = {
target1: {
template: { // a template object
pname1: 5,
pname2: true,
pname3: { key: "value" },
aproperty: "a aproperty will be deleted"
},
dest: ${ BUILD }/file.json, // write the result to this file.
merge: {
pname3: { key: { test: "fun" }}, // this will replace value by { test: "fun" }
aproperty: undefined // this will remove aproperty from template
}
}
}
`
$3
Updating template properties means
* setting a value to a template property that exists
* ignoring properties that do not exist in the template
`javascript
const BUILD = "...some path";
const jsonfile = {
target1: {
template: { // a template object
pname1: 5,
pname2: true,
pname3: { key: "value" },
aproperty: "a aproperty will be deleted"
},
dest: ${ BUILD }/file.json, // write the result to this file.
update: {
pname1: "fun", // will set template.pname1 to "fun"
xproperty: "uups" // will change nothing, because there is
// no xproperty in template.
}
}
}
``