hello all, I have dumb idea - there's JSON that co...
# lucee
r
hello all, I have dumb idea - there's JSON that comes from trusted source, and this JSON has somewhat different fields that are filled by users, sometimes loosely, sometimes strictly per documnetation. I want to read JSON and build an object using introspection(?) with fields corresponding to data: if something like {"shelter": {"pet": "dog", "names": "Huey, Louie, Dewey"}} comes in, I create object Shelter with Array of dogs objects with Dog.Name() property populated. Maybe not the best example 🙂 but idea is that I create an object on the fly, without creating CFC first and setting its structure. JSON is good enough to figure out what information comes in, but not good enough to have CFC pre-defined and populated with data. Maybe no need to create objects and work directly with data?
w
i can't say i can see the benefit of creating an object instance given what you describe, but it's certainly possible to do what you're describing. just not seeing the benefit from what you describe
fact is, you can create a generic cfc instance and add 'properties' and getter/setters into that created instance
r
right - that is mainly interested in - create 'generic' object and add necessary properties later
this way they do it in python or js
w
so have a basic prototype.cfc with an addProperty( name, default ) method. in that method you'll just create variables[name] and then inject a getter and setter to interact with it
this["set#Name#"] = function definition, etc
inside that addProperty method
r
getter and setter to be created manually as well? Having variables.somethingVariable dont create getter automatically ?
w
no
i don't know if it's possible to add a property as a true 'property' after the object is initialized
but once it IS initialized, they're all variables scoped variables anyway
i suppose you could handle it in an onMissingMethod but i could see that getting messy really quickly
✅ 2
but may even be cleaner
anyway, ultimately i believe you will HAVE TO start with an actual .cfc file to instantiate. may as well add the utility functions you'll need to mimic the behavior in python/js
but smarterer people may have better ideas
r
thanks, that's what I need. Probably someone will tell me - since there's no CFC defined in first place, little sense create objects then, just work with structures or arrays. I thought that there could be common behavior shared by all these new objects, despite not having all the data, so using objects could have benefits.
a
As soon as you go "common behaviour"... that implies you need a class to define what that behaviour is. It sounds to me like you want an ActiveJsonObject.cfc or something like that. Init it with a json string, deserialise that into the variables scope. Use onMissingMethod to check for properties in the variables-scoped struct, and if one's there: act on it, otherwise throw an exception. Add methods for
toJson
/
toStruct
etc.
👍🏼 1
👍 1
TBH though i am not really sure of the merits of the whole notion, without hearing a concrete non-theoretical use case. I'd always err towards more well-defined classes.
Another approach there might be to define a BuildFromJson interface that dictates a
createFromJson
method (and maybe
toJson
/
toStruct
methods too) that one can implement from any existing class. In CF at least (not sure about Lucee though...) one can even define default behaviour for these methods in the interface itself.
But I think unless one is writing a library... generic behaviour is seldom the right approach to whatever problem is to hand.