nlohmann / nlohmann/json

Support for integer keys in the SAX interface when parsing CBOR

Open
#2,766 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

aspect: binary formats
Dominant language
C++
Stars
50.6k
Forks
7.5k
Avg merge
4d 17h
Merged PRs (30d)
58

Description

Currently parsing CBOR with this library can only handle string keys. At the project I am working on, we want to use integer keys to reduce the size of our responses by using them as an alias for the long string keys. As such it would be very helpful to have some way to transform those to integer keys. While this would probably not fit into the STL-like interface, there is place to add it to the SAX interface.

My proposal would be to add overloads for bool key(string_t& val);, which take an integer or similar types. The default version of those functions should still emit a parse error, but a library user would be able to override them and decide how to handle them differently. That way I can transform the CBOR into something, that can be handled via the STL interface (or one could even use that to build some native structs).

One issue I see with that, is how to handle the default implementation, which should call bool parse_error(std::size_t position, const std::string& last_token, const detail::exception& ex); imo. This has more parameters, which may have gotten lost already? This may be solveable by accessing some internal state, but I am not familiar enough with the library to be able to tell, if this is actually doable.

Alternatives

One could probably also implement the parsing in the parse_error callback, but that would be difficult and error prone. Maybe there is also already a way to do this already, but I don't know how.

PS: How do I tag this as enhancement?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the SAX interface's key(string_t& val) overload and the CBOR parsing path. Review how parse_error(std::size_t position, const std::string& last_token, const detail::exception& ex) is reached, then determine how integer-key overloads should report unsupported keys by default. Done means the API can expose integer CBOR keys to overriding SAX handlers without breaking existing string-key behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.