Constraint labels can break CQM (de-)serialization
Description
Currently CQM.to_file() encodes constraint labels with json.dumps, allowing characters that are illegal in file names, like /. That becomes a problem only when trying to extract the CQM zip file.
However, CQM.from_file() assumes forward slashes are not allowed in file names and that / is used only as (POSIX) path separator:
https://github.com/dwavesystems/dimod/blob/85f5bafd514f32585666e5de4c75db9b53444626/dimod/constrained/constrained.py#L1071-L1073
Consequently, decoding CQMs with slashes in constraint labels will fail.
Steps To Reproduce
>>> import dimod
>>> cqm = dimod.CQM()
>>> cqm.add_constraint(dimod.Binary() == 1, label='a/b')
'a/b'
>>> dimod.CQM.from_file(cqm.to_file())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<omitted>/env311/lib/python3.11/site-packages/dimod/constrained/constrained.py", line 1078, in from_file
label = deserialize_variable(json.loads(constraint))
^^^^^^^^^^^^^^^^^^^^^^
File "<omitted>/lib/python3.11/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<omitted>/lib/python3.11/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<omitted>/lib/python3.11/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 1 (char 0)
Expected Behavior
Serializing CQMs with forward slashes (and some other unicode characters, like NULL) should be forbidden. Or at least such labels should be renamed, either on constraint creation, or cqm serialization.
Environment
- any OS, any python
- reproduces with latest
dimod==0.12.12