Skip to content

0x00 showing up as \u0000 instead of null in JSON #6

@pherris

Description

@pherris

Hoping you might be able to help me figure out why my null value is showing up as '\u0000' instead of null.

When using for a null-terminated string that has no value the resulting JSON has the following value: '\u0000'. I have this method to help me identify where the next null is (null is a delimiter for a file I am trying to parse).

Buffer.prototype.nextNull = function (from) {
var length = this.length, 
    from = (!from ? 0 : from);

for (var i = from; i < length; ++i) {
    if (this[i] === 0x00) {
        console.log(i, this.toString('hex', i, i+1));
        return i;
    }
}
return -1;
};

The console.log line prints out the below (meaning I know I have an exact match to 0x00 at index 12).

12 '00'

When I write a quick test with your code I see it working properly (but not against my file):

var spec = {
        bigEndian: true,
        fields: [
          { name: "shouldBeNull",  start: 0,  type: 'utf8', length: 0 } 
        ]
};
var buf = new Buffer(1);
//either of these give the same result
//buf[0] = null; 
buf.write("00", 0, 1, 'hex');
console.log("--" + buf.toString('hex', 0, 1));
console.log(decoderRing.decode(buf, spec));

The (acceptable) result from the code not using my file:

{ shouldBeNull: '' }

The output from my file:

{ shouldBeNull: '\u0000' }

Any thoughts? I looked for other ways to open the file and couldnt see anything. Suggestions welcome ;) - thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions