1

I want to use IntEnum in my project and i have to serialize enum value into json and then deserialize it back.

The problem is when i use python 2.7 i get this error: ValueError: No JSON object could be decoded

When i use python 3.* all is ok.

The code is (for python 2.7):

import json
from enum import IntEnum

class DigitEnum(IntEnum):
    A = 1

if __name__ == '__main__':
    print DigitEnum.A
    a = json.dumps(DigitEnum.A)
    print a # DigitEnum.A
    a = json.loads(a) # error here
    print a
    print a == DigitEnum.A

python 3.*:

import json
from enum import IntEnum

class DigitEnum(IntEnum):
    A = 1

if __name__ == '__main__':
    print(DigitEnum.A)
    a = json.dumps(DigitEnum.A)
    print(a) # 1
    a = json.loads(a)
    print(a)
    print(a == DigitEnum.A)

Is it possible to avoid creating custom JSONDecoder\JSONEncoder for my enum class or the only way is to use something like this:

a = json.dumps(DigitEnum.A.value)

The primary goal is to save compatibility between to major python versions

Nikita Ryanov
  • 1,520
  • 3
  • 17
  • 34
  • 1
    how do you want to *save compatibility between to major python versions* if you have 2 different snippets for 2 versions? afaik you can leave **Python 3** version (with parentheses around `print` arguments) and it will work in **Python 2.7** as well – Azat Ibrakov Jul 30 '18 at 10:51
  • The line causing the issue is `a = json.loads(a)`.It's caused by the fact that `a` is not a JSON object, so it can not be decoded. – ech0 Jul 30 '18 at 10:53
  • @AzatIbrakov, this is the main question. I need the way to use json for my enum class in both major python versions. This enum package, as i know, was created specially for this purpose: save compatibility. I need only enum value when i deconding enum, so, it is ok if i lose all info about enum class – Nikita Ryanov Jul 30 '18 at 10:56
  • @chromaerror, so, is it possible to force json to use enum value, not a enum name for serialization without custom json encoder\decoder? – Nikita Ryanov Jul 30 '18 at 10:58
  • 1
    As far as I am aware, no. You should be using a custom JSON encoder. You can take a look at this thread: https://stackoverflow.com/questions/24481852/serialising-an-enum-member-to-json – ech0 Jul 30 '18 at 11:00
  • @chromaerror, thank you, i saw this question, but in my case i use IntEnum and the behaviour of this class is different in serialization. When i use Enum i will get this error in both python versions as also said in related question. But if i use IntEnum i will get this error only in python 2.7. This is strange for me, but maybe i miss something crucial – Nikita Ryanov Jul 30 '18 at 11:04

1 Answers1

2

In Python 3.5+ json was modified to work properly with IntEnum so that, for example:

json.dumps(DigitEnum.A) == 1

This change was not backported to 2.7 (and won't be) so the corresponding code in 2.7 results in:

json.dumps(DigitEnum.A) == 'DigitEnum.A'

which is a str, not a DigitEnum and not an int.


Side note: always use repr() when debugging so you can see what the actual types/values are.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237