ColdFusion's ArrayEach and Struct JSON Serialization

Ok, technically, the two topics mentioned in the subject have nothing to do with each other, but both were interesting things I ran into during my presentation on Sunday so I thought I'd call them out.

First - I had a question about arrayEach. Does it recalculate the size of the array on every iteration? The answer is yes and you can see it with this code sample.

names = ["Ray","Scott","Dave","Todd"];

names.each(function(x) {
	writeoutput(x & "
"); if(names.len() < 10) { names.append("foo"); } });

This will loop over the array and while it is fewer than ten items long, actually append to the array as well. The output is what you expect, the names followed by some foo items. Again, totally expected I think but I wanted to verify it when asked.

The next question I got was about the new struct format of query serialization. I blogged about this back in May. The question was - will this apply if the query is a subkey of the data being serialized? The answer is yes. I did not expect this and I'm happy it does work. An example:

q = queryNew("name,age","varchar,integer");

q.addRow({name:"ray",age:33});
q.addRow({name:"3",age:33});
s = {};
s.x=q;
writeoutput(serializejson(s,"struct"));

To be clear, struct as used above is a flag to the CF compiler to serialize queries in struct format. It has nothing to do with the fact that I'm passing a struct in. An array with a query as an item would have worked as well.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for Extend by Auth0. He focuses on serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments