Cannot store values in Values hash when an undef is already there. #2

Closed
cowens opened this Issue Dec 14, 2012 · 4 comments

2 participants

@cowens

It appears as if you cannot modify the value of a hash key whose value is undef:

#!/usr/bin/perl

use strict;
use warnings;

use JSON::SL;
use Data::Dumper;

my $j = JSON::SL->new;
$j->set_jsonpointer(["/^"]);

for my $r ($j->feed('[{"a":5},{"a":null}]')) {
    $r->{Value}{a} = defined $r->{Value}{a})  ? "d" : "not d";
    print Dumper $r;
}

Most likely this is being caused by code like:

hv_store(hv, "key", 3, &PL_sv_undef, 0);

I believe you must create a new scalar to hold the undef value and store that instead.

My current workaround is to delete the hash entry if it contains an undef.

#!/usr/bin/perl

use strict;
use warnings;

use JSON::SL;
use Data::Dumper;

my $j = JSON::SL->new;
$j->set_jsonpointer(["/^"]);

for my $r ($j->feed('[{"a":5},{"a":null}]')) {
    delete $r->{Value}{a} unless defined $r->{Value}{a};
    $r->{Value}{a} = exists $r->{Value}{a})  ? "d" : "not d";
    print Dumper $r;
}
@cowens

It appears as if you should use newSV(0) instead of &PL_sv_undef when storing undef in arrays and hashes

@mnunberg
Owner

You are indeed correct -

I use &PL_sv_undef for placeholders for unknown values, but I really should be using a 'fully qualified' SV for actual JSON nulls. See latest commit in master. See if this works for you

@cowens

That seems to work for me.

@mnunberg
Owner

Can you close this issue if you think it's been resolved? Trying to push for a second release (a 1.0)

@mnunberg mnunberg added a commit that referenced this issue Dec 14, 2012
@mnunberg Add test for issue #2 ce76731
@mnunberg mnunberg closed this Mar 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment