DEV Community

Simon Hafner
Simon Hafner

Posted on

2 1

Decoding CBOR with PureScript

If you want to decode cbor data from PureScript, there's cborg, which works just fine... if you use useMaps to deal with the Error: CBOR decode error: non-string keys not supported (got number) errors. Afterwards you have javascript Maps, which can't be decoded with argonaut. I use this code as workaround:

const toObject = function(cbor) {
  if (cbor instanceof Map) {
    var obj = {}
    cbor.forEach(function(value, key) {
      obj[key.toString()] = toObject(value);
    });
    return obj;
  } else {
    if (cbor instanceof Array) {
      return cbor.map(ob => toObject(ob));
    } else {
      if (cbor instanceof Object) {
        var obj = {}
        cbor.forEach(function(value, key) {
          obj[key.toString()] = toObject(value);
        });
        return obj;
      } else {
        return cbor
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

After that, you can decode the tags as string keys, e.g.

decodeJson cbor :: _ { "1" :: Int }
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay