Имейте в виду, это использует lodash
для обнаружения массивов и объектов, вот еще один метод, который будет сохранять "листовые" объекты компактными в одну строку:
_.jsonPretty = function(obj, indent) {
if(!indent) indent = 2;
return JSON.stringify(obj, function(k,v) {
//Check if this is a leaf-object with no child Arrays or Objects:
for(var p in v) {
if(_.isArray(v[p]) || _.isObject(v[p])) {
return v;
}
}
return JSON.stringify(v);
//Cleanup the escaped strings mess the above generated:
}, indent).replace(/\\/g, '')
.replace(/\"\[/g, '[')
.replace(/\]\"/g,']')
.replace(/\"\{/g, '{')
.replace(/\}\"/g,'}');
};
И просто используйте это так:*
_.jsonPretty(yourObjectToStringify);
Вот пример до ...
{
"type": "light-item",
"name": "Waiting",
"ringSeqLooping": true,
"ringSeqHoldLast": false,
"ringSteps": [
{
"type": "light-step",
"time": 1,
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "FadeOn",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
},
{
"type": "light-step",
"time": "0.5",
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "FadeOff",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
}
],
"stripSeqLooping": true,
"stripSeqHoldLast": false,
"stripSteps": [
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "FadeOn",
"color": "#fff"
},
{
"state": "FadeOn",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
},
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "FadeOff",
"color": "#fff"
},
{
"state": "FadeOff",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
}
]
}
... и после :
{
"type": "light-item",
"name": "Waiting",
"ringSeqLooping": "true",
"ringSeqHoldLast": "false",
"ringSteps": [
{
"type": "light-step",
"time": "1",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"FadeOn","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
},
{
"type": "light-step",
"time": "0.5",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"FadeOff","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
}
],
"stripSeqLooping": "true",
"stripSeqHoldLast": "false",
"stripSteps": [
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"FadeOn","color":"#fff"},
{"state":"FadeOn","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
},
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"FadeOff","color":"#fff"},
{"state":"FadeOff","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
}
]
}