Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

python - How to deserialise enumeration with string representation?

I would like to create a class, which has an enumeration as an attribute. This enumeration should have a string representation that shows up as human-readable value when dumping the instance of the class that uses the enum attribute as a JSON string. In the minimal working example below, I created three enumerations in three different ways.

After the deserialization, each attribute shows us that it comes from an enumeration except the enumeration with a string representation. It is just a string.

If it is not possible to realize such a structure, I would like to know why.

Requirements

If you would like to test it, you have to install jsons and attrs with

pip install attrs jsons

Minimal working example

Here you see a minimal working example.

import jsons
import attr

from enum import Enum

# ----------------------------------------------------
# create enumeration with the help of a dictionary

fruits = {
    "PINEAPPLE": "PINEAPPLE",
    "APPLE": "APPLE",
    "ORANGE": "ORANGE",
    "BANANA": "BANANA",
}

Fruit = Enum("FRUITS", fruits)

# ----------------------------------------------------
# create a classical enumeration


class Nut(Enum):

    PEANUT = 1
    HAZELNUT = 2
    CASHEW = 3
    WALNUT = 4


# ----------------------------------------------------
# create enumeration with a string representation


class Vegetable(str, Enum):

    BROCCOLI = "BROCCOLI"
    CUCUMBER = "CUCUMBER"
    POTATO = "POTATO"
    ONION = "ONION"


# ----------------------------------------------------
# create a class which uses the enumerations


@attr.s(auto_attribs=True, kw_only=True)
class Order(jsons.JsonSerializable):
    fruit: Fruit
    nut: Nut
    vegetable: Vegetable


# ----------------------------------------------------
# initialize an order object, serialize and deserialize it

order = Order(fruit=Fruit.APPLE, nut=Nut.PEANUT, vegetable=Vegetable.CUCUMBER)

json_string: str = Order.dumps(order)

order_deserialised: Order = Order.loads(json_string)

Structure of the order and order_deserialised variable:

order:              Order(fruit=<FRUITS.APPLE: 'APPLE'>, nut=<Nut.PEANUT: 1>, vegetable=<Vegetable.CUCUMBER: 'CUCUMBER'>)

order_deserialised: Order(fruit=<FRUITS.APPLE: 'APPLE'>, nut=<Nut.PEANUT: 1>, vegetable='CUCUMBER')

As you can see, the order_deserialised shows the vegetable as a string and not an enumeration.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...