timo / json_fast

a naive imperative json parser in perl6, to evaluate performance against JSON::Tiny
Artistic License 2.0
26 stars 20 forks source link

Using @ sigils with from-json on arrays creates a nested array #53

Closed Kaiepi closed 1 year ago

Kaiepi commented 5 years ago

Pretty self-explanatory:

bastille% perl6 -MJSON::Fast -e 'say from-json "[]"'
[]
bastille% perl6 -MJSON::Fast -e 'say my @ = from-json "[]"' 
[[]]
timo commented 5 years ago

unfortunately that's part of JSON::Fast being a drop-in replacement for JSON::Tiny. I want to have something in the future that'd get rid of the scalar container there, but I'm not yet sure what it should look like

Antisunny commented 5 years ago

Environment: Perl 6.d 2019.03; OS: MacOS Mojave 14.5

Has this issue been solved? @timo

Issues:

my %data = from-json '[]';

should thow a Error of Type Mismatch; actually got a {} hash type object; This's unacceptable.

my @data = from-json '[]';

should not wrap another Array outside of the real data; actually got [[]]

but

my $data = from-json '[]';

got the right data: []

Hash parsed correctly

The hash is parsed correctly.

my %data = from-json '{}';
my $data = from-json '{}';

say $data.WHAT, %data.WHAT;  # (Hash)(Hash)
Kaiepi commented 5 years ago

FWIW you can deal with this by using := instead of =:

bastille% perl6 -MJSON::Fast -e 'say my @ := from-json "[]"'
[]
timo commented 1 year ago

the solution for this is to use the immutable option for from-json, which results in List an Map instead of Array and Hash, and also strips containerization from outside, which allows you to just assign to % and @ sigiled variables. just changing the original way this works was not an option for json::fast's intended use as a drop-in replacement of JSON::Tiny, unfortunately