levelup: json valueEncoding doesn't work as expected

I use json valueEncoding for v1 and it works without any problem.

After upgrade to v2, It does not work anymore: after putting an object to leveldb, we will get back a [object Object] value of string type.

The following code can be able to reproduce the problem:

import * as encoding   from 'encoding-down'
import * as leveldown  from 'leveldown'
import * as levelup    from 'levelup'

async function main() {
  const encoded = encoding(leveldown('/tmp/test'), {
    valueEncoding: 'json',
  })
  const levelDb = levelup(encoded)

  await levelDb.put('test', {a: 1})
  const value = await levelDb.get('test')

  console.log('value type:', typeof value)  // value type: string
  console.log('value:', value) // value: [object Object]
}

main()
"dependencies": {
    "encoding-down": "^2.2.1",
    "leveldown": "^1.8.0",
    "levelup": "^2.0.0-rc3",
}

After a little digging, I found the following code in AbstractLevelDOWN, which is just String(value) for the value.

AbstractLevelDOWN.prototype._serializeValue = function (value) {
  if (value == null) return ''
  return this._isBuffer(value) || process.browser ? value : String(value)
}

I have no idea how to fix it because there’s too many modules/layers.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 22 (22 by maintainers)

Most upvoted comments

Will fix this today or tomorrow. After that I think we should release 2.0.0.

right. damn lockfiles 😃

@vweevers Yes, you are right. After installing the deferred-leveldown@2.0.2, the test passed!

# levelup json bug
ok 19 value type should be object
ok 20 should get back the original object

Sorry for misreporting because I was thought the leveldown had already linked the latest version of it.