Is there a best practice for serializing arbitrary...
# cfml-general
t
Is there a best practice for serializing arbitrary cfcs to JSON? I'm trying to refactor around
this.serialization.preservecaseforstructkey = true
being ignored in CF2023. For things that are already structs, that's easy enough -- quote the struct keys, and everything's fine. But for CFCs, I need to convert the component to a struct first in order to do that. I took a stab at the "Custom Serializer" introduced in CF11, but came to a similar conclusion to Adam in his blog that the implementation here is "rubbish." (https://blog.adamcameron.me/2014/04/coldfusion-11-custom-serialisers-more.html) implementing a
serialize
method with 10-15 if/else's in it just makes me want to cry. and then I have to remember to maintain that when I add new components in the future. I'm hoping that in the last 10 years, there's become a standard "better way" before I go reinventing the wheel.
e
Well, there is this (shameless plug) which I wrote centuries ago, you could try and see if it still works or maybe borrow some ideas from it: https://github.com/evagoras/ezCFJSON
c
You might look at https://forgebox.io/view/mementifier which can turn business objects (CFCs) into structs or array of structs.
t
actually, it appears that I should have tested more thoroughly... It looks like
preservecaseforstructkey
only applies to structs, and not to component properties... So It looks like CF2023 is still preserving case on component properties correctly. It's just structs that it's not doing it for. Now I wonder where my misunderstanding came from. But I think that means that things are mostly okay as-is.
thanks for the suggestions though