mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 22:06:42 +01:00
Maps with enum keys, such as std::map<E, T>, are stored as arrays of [key, value] pairs, because enums are not convertible to the string type of object keys - even if NLOHMANN_JSON_SERIALIZE_ENUM maps them to strings (#4378). The new JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS macro stores them as objects instead, converting each key with the enum's to_json. It applies to any map-like type with enum keys (std::map with any comparator, std::unordered_map, ...). A key that does not convert to a string throws type_error.302, and two keys converting to the same string throw the new type_error.318, rather than losing an entry. The macro changes the output of inline functions, so it is part of the ABI tag (_ekmo). Reading needs no macro: std::map and std::unordered_map with enum keys are now also read from objects, converting each key with the enum's from_json. That input was rejected before, and arrays of pairs are still read, so data written either way can be read. This supersedes #4531, which first proposed storing these maps as objects. Co-authored-by: Muhammad Amir bin Mohamad Ghazaly <amirghaz@umich.edu> Signed-off-by: Niels Lohmann <mail@nlohmann.me>
42 lines
1.4 KiB
Python
Executable File
42 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import itertools
|
|
import jinja2
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
def semver(v):
|
|
if not re.fullmatch(r'\d+\.\d+\.\d+', v):
|
|
raise ValueError
|
|
return v
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--version', required=True, type=semver, help='Library version number')
|
|
parser.add_argument('output', help='Output directory for nlohmann_json.natvis')
|
|
args = parser.parse_args()
|
|
|
|
namespaces = ['nlohmann']
|
|
abi_prefix = 'json_abi'
|
|
abi_tags = ['_diag', '_ldvcmp', '_dp', '_bics', '_psp', '_snul', '_ekmo']
|
|
version = '_v' + args.version.replace('.', '_')
|
|
inline_namespaces = []
|
|
|
|
# generate all combinations of inline namespace names
|
|
for n in range(0, len(abi_tags) + 1):
|
|
for tags in itertools.combinations(abi_tags, n):
|
|
ns = abi_prefix + ''.join(tags)
|
|
inline_namespaces += [ns, ns + version]
|
|
|
|
namespaces += [f'{namespaces[0]}::{ns}' for ns in inline_namespaces]
|
|
|
|
env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath=sys.path[0]), autoescape=True, trim_blocks=True,
|
|
lstrip_blocks=True, keep_trailing_newline=True)
|
|
template = env.get_template('nlohmann_json.natvis.j2')
|
|
natvis = template.render(namespaces=namespaces)
|
|
|
|
with open(os.path.join(args.output, 'nlohmann_json.natvis'), 'w') as f:
|
|
f.write(natvis)
|