considering using an ordered struct for something ...
# cfml-general
w
considering using an ordered struct for something in lucee for the first time and i'm wondering if the ordered nature of the struct will be inherited by any nested structs inside of that main ordered struct, or whether any nested structs themselves must be declared as ordered prior to adding them?
w
ok. any syntactical tricks you can think of to not have to pre-init a struct as ordered and then adding? i have a var where i'm stipulating a struct with nested structs that gets passed around and populated with values. the main struct has like five different nested structs in it and it won't be as graceful decomposing it to init five ordered struct and then reassemble them into the main one
Copy code
var result = { "masters" : { 
                "total" : 0,
                "live" : 0, 
                "generated" : 0, 
                "deliverable_live" : 0,
                "deliverable_archive" : 0, 
                "delivered_live" : 0,
                "delivered_archive" : 0,
                "archived" : 0,  
                "purged" : 0
            }, 
            "children" : { 
                "total" : 0,
                "live" : 0, 
                "archived" : 0, 
                "purged" : 0
            }, 
            "aggregates" : { 
                "live" : {
                    "avgChildrenPerMaster" : 0 
                },
                "archive" : {
                    "avgChildrenPerMaster" : 0 
                }
            }
        }
z
is think json uses ordered structs...
w
working with something like that
right, this is for a rest api that returns json, so i want it to serialize ordered
if possible without making the cf code overly verbose
z
[a=123]
message has been deleted
w
so use square brackets instead of curly and i can do it inline as above?
z
pretty much
w
ok, and no restrictions on quoting the keys with the square notation?
i can test, just thought i'd ask
z
nothing I'm aware of
w
thank you very much
a
NB: an object in JSON ought to be considered unordered, irrespective of any implementation quirks.
(ref https://www.json.org/json-en.html, para just above the first diagram)
w
sure. it's just a nice-to-have, preserving the ordering is just for readability during dev (for me). not critical if it doesn't pan out
1
a
Not admonishing you one bit. Just saw the Q being asked, and had ot look it up myself one time as I had misremembered it the other way around.
w
i'm not even sure that i'd do it this way moving forward, but was curious about how ordered structs do/don't inherit and all that stuff, haven't really worked with them before
a
There's no way of expressing an "ordered object" in JSON, so Lucee will just treat it like an ordinary struct when deserialising, I'd've thought. ANd when dumping... keys are alpha-ordered?
w
in my case, the 'end' of the sequence is looking at the json in postman, so while i hadn't thought about the deserializing back to cf after, it doesn't affect my use case
a
gotcha. I know there are long-hand versions of ceclaringing them (some variant of
structNew
) but I always use
[]
notation, and obvs (?) that wouldn't inherit as one needs to literally specify each element / value of the struct
✔️ 1
Oh Lucee might not support
structNew("ordered")
anyhow: https://docs.lucee.org/reference/functions/structnew.html. It's just a ColdFusion thing: https://helpx.adobe.com/uk/coldfusion/cfml-reference/coldfusion-functions/functions-s/structnew.html One has to use the literal notation in Lucee I guess.
z
ordered does work in lucee
a
OK, so just a doc update then
a
"linked". sigh.
z
i wanna add old school non thread safe structs too (if you wanna shot yourself in the foot, why not! ) https://luceeserver.atlassian.net/browse/LDEV-4082
💯 1
w
well, as it turns out this is all academic in this case. the ordering does NOT survive the serialization to json, the json is just 'whatever' as if it was a normal non-ordered struct. oh well
😕 1
lucee allows custom serializers like acf? not hugely important, but i imagine a custom serializer could be created to preserve the order, no?
in the serialized json, that is
i can confirm that the ordered nature of the struct exists right up to the point of serialization to json, after which that string is a representation of an unordered struct
a
I'm seeing the JSON reflect the struct-ordering on both CF and Lucee? https://trycf.com/gist/528813caaf2d7e4309c8852affc7adc1/lucee5?theme=monokai

https://i.paste.pics/8be1061204be4162e7d34d42271a9f41.png

z
@micha may have added that ( i think ) partly when working with @bdw429s on CFconfig as the order was important, or he mentioned that in parsing
oh wait, yeah the json will always be just boring old json syntax, the order will be there but no enforced