On Mon Jan 27 19:15:07 2020, PEVANS wrote:
Show quoted text> On Thu Jan 23 11:16:13 2020, srchulo wrote:
> > I was picturing the key/value dictionary structure. A hash with the
> > method names as keys and the values as the values as they are, and
> > then at that point I think any behavior a JSON encoder would have
> > would be natural (encoding scalars, arrays, hashes, looking for
> > TO_JSON on blessed objects and stringifying them if they don't have
> > one). I think this makes sense, because Struct::Dumb is basically a
> > locked hash with methods instead of keys.
>
> That's certainly one interpretation. Another is that it is an array
> with named accessors instead of indices. And in fact that is how it is
> implemented.
>
> I'm not particularly familiar with JSON encoders/decoders, but I
> wonder how these would be decoded out of the stream back into a
> structure - I imagine that wouldn't be possible, so this is a one-way
> conversion?
I think the array with named accessors makes sense for the Struct::Dumb implementation for speed reasons, but I'm not sure that would make sense in JSON. In JSON things become (as far as I know) either strings, ints, arrays, hashes, or booleans, which is why I think a hash would be the best representation in this case.
As for decoding, the decoder would decode it as the structure it is in JSON-- i.e. an array or a hash, not into an object. Although you could make a from_json type of method for creation, but TO_JSON is the only required one to play well with perl's JSON encoder/decoders.