minizinc-python icon indicating copy to clipboard operation
minizinc-python copied to clipboard

Usage of IntEnum in array model input

Open Dekker1 opened this issue 5 years ago • 1 comments

The standard JSONEncoder that is used to create the JSON input for a MiniZinc model ensures that an IntEnum is always represented using its integer representation: https://github.com/python/cpython/blob/3.9/Lib/json/encoder.py#L309-L313

Normally the JSON representation can be customised in the default member function of MZNJSONEncoder, but because IntEnum inherits from int, the representation is part of the base types of JSONEncoder that is directly implemented in the encoder library (and a C backend)

This is incorrect as an input for a MiniZinc enum, where a JSON object {"e": "some_name"} is expected and an integer will not be accepted.

workaround A current workaround is to use an Enum in Python, or to use the actual matching integers in the MiniZinc model.

possible solutions The following solutions could be considered:

  • A more customisable JSONEncoder that gives us more freedom to change the JSON representation of IntEnum
  • Find a way to consistently change the _intstr function (both in the Python and C implementation): https://github.com/python/cpython/blob/3.9/Lib/json/encoder.py#L271
  • Change the data when it is set to be in the required format.

The last solution seems more likely, as I'm currently unaware of a better JSON Encoder alternative and several attempts are the second method have failed.

Dekker1 avatar Oct 22 '20 00:10 Dekker1

Thank you so much for the help and the attention!

ofekkir avatar Oct 22 '20 12:10 ofekkir