satauros
06/27/2023, 11:23 AMwebsolete
06/27/2023, 11:37 AMwebsolete
06/27/2023, 11:37 AMwebsolete
06/27/2023, 11:38 AMdavla
06/27/2023, 11:38 AMmyStruct = {
"MyKeyName": "this key value",
"MyOtherKeyName": "another key value"
};
serializeJSON(myStruct);
websolete
06/27/2023, 11:39 AMwebsolete
06/27/2023, 11:41 AM/**
* Takes a struct or array (which contains nested structs) and forces struct key names to upper or lowercase as specified; ensures predictable results when using serializeJSON et al
*
* @object any only structs and arrays (which contain structs) are operated upon
* @forcecase string ?: upper | lower - the case to force the struct's keys to
*
* @return any the object with struct key case modifications in place
*/
public any function forceStructKeyCase(
required any object,
string forcecase="upper"
) output=false {
var x = {};
var y = "";
var i = "";
var a = "";
var z = [];
if(isArray(arguments.object)) {
if(!arraylen(arguments.object)) {
return arguments.object;
}
else {
for(a = 1; a lte arraylen(arguments.object); a++) {
z[a] = forceStructKeyCase(object=arguments.object[a],forcecase=arguments.forcecase);
}
}
return z;
}
else if(isStruct(arguments.object)) {
if(!structisempty(arguments.object)) {
for(i in arguments.object) {
y = ucase(i);
if(arguments.forcecase eq "lower") {
y = lcase(i);
}
x[y] = forceStructKeyCase(object=arguments.object[i],forcecase=arguments.forcecase);
}
}
return x;
}
else {
return arguments.object;
}
}
satauros
06/27/2023, 11:57 AMproperty name="propOne";
property name="propTwo";
...
And this should be sent to an external API as json, but the external API expects the keys in PascalCase, but we define them as camelCase (as per our design standards / requirements).
I was looking for a way to make these two ends meet in the middle (custom serializer / etc...)websolete
06/27/2023, 12:01 PMwebsolete
06/27/2023, 12:01 PMwebsolete
06/27/2023, 12:02 PMzackster
06/27/2023, 12:14 PMsatauros
06/27/2023, 12:37 PMsatauros
06/27/2023, 12:39 PMsatauros
06/27/2023, 12:39 PMquetwo
06/27/2023, 1:12 PMmyVar = {};
myVar["myStuct"] = {};
myVar.myStruct.property = "value";
myVar.myStruct.property2 = "value;
myJSON = serializeJSON(myVar);
quetwo
06/27/2023, 1:13 PMmithlond
06/27/2023, 2:43 PMmithlond
06/27/2023, 2:44 PMNovember, 5 2023
and I'm all who even puts a comma after the month instead of the day? Not even a thing CF, c'mon.satauros
06/27/2023, 2:59 PMsatauros
06/27/2023, 3:00 PMquetwo
06/27/2023, 3:02 PMmithlond
06/27/2023, 3:04 PMnew Date(myString)
quetwo
06/27/2023, 4:00 PMKal
07/06/2023, 8:45 PM// Below function sorts nested structs & convert keys to lowercase
// v = incoming value(struct), s = flag to sort the struct
function structSortLowerCase(v, s=1) {
if (isStruct(v)){
if (s){ v = structToSorted(v,"text","asc"); } // sort the struct
structEach(v, function(key,value) { v["#lcase(key)#"] = value; } ); // lowecase the keys
//make a nested/circular call to structSortLowerCase on the values
return structMap(v, function(key,value) { return structSortLowerCase(value); } );
}
else if ( isArray(v) ) { return arrayMap(v, function (value) { return structSortLowerCase(value); } ); }
else { return v; }
}