Skip Menu |

This queue is for tickets about the Struct-Dumb CPAN distribution.

Report information
The Basics
Id: 131546
Status: open
Priority: 0/
Queue: Struct-Dumb

People
Owner: Nobody in particular
Requestors: violapiratejunky [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Add TO_JSON
It would be great if Struct::Dumb provided a TO_JSON method so it could be encoded to JSON by modules like JSON, JSON::XS, Cpanel::JSON::XS, Mojo::JSON, etc.
On Thu Jan 23 00:14:54 2020, srchulo wrote: Show quoted text
> It would be great if Struct::Dumb provided a TO_JSON method so it > could be encoded to JSON by modules like JSON, JSON::XS, > Cpanel::JSON::XS, Mojo::JSON, etc.
Yes it's a fun thought, but there's no one "natural" way to do it. Would it be done entirely as a named key/value dictionary structure? Or a flat list of values? Or something else entirely? -- Paul Evans
On Thu Jan 23 10:20:52 2020, PEVANS wrote: Show quoted text
> On Thu Jan 23 00:14:54 2020, srchulo wrote:
> > It would be great if Struct::Dumb provided a TO_JSON method so it > > could be encoded to JSON by modules like JSON, JSON::XS, > > Cpanel::JSON::XS, Mojo::JSON, etc.
> > Yes it's a fun thought, but there's no one "natural" way to do it. > Would it be done entirely as a named key/value dictionary structure? > Or a flat list of values? Or something else entirely?
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.
On Thu Jan 23 11:16:13 2020, srchulo wrote: Show quoted text
> 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? -- Paul Evans
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.