JSON Encodes / Decodes GTK Enumerations

I need to save various properties of custom GTK elements to a file for future use and decided to use JSON due to the simple format and nesting of dicts.

Many properties are GTK enumerations, such as gtk.PAGE_ORIENTATION_PORTRAIT, gtk.ANCHOR_CENTERand pango.ALIGN_LEFT. They have a unique name that can be obtained using obj.value_nameto get a valid JSON type.

Currently, I have 2 methods for each of my elements: to_str()to get value_name and from_str(), which again displays str in enum. I would like to automate this, so I remember calling them and tidying up the code a bit. JSONEncoder and JSONDecodr do exactly that, or so I thought ...

This is an example pointed out in Python docs and it works as expected.

import json

class ComplexEncoder(json.JSONEncoder):
    def default(self, obj):
        print "default method called for:", obj
        if isinstance(obj, complex):
            return [obj.real, obj.imag]
        return json.JSONEncoder.default(self, obj)

print json.dumps(2 + 1j, cls=ComplexEncoder)

GTK:

import json
import gtk

ENUMS = [gtk.PAGE_ORIENTATION_PORTRAIT, gtk.PAGE_ORIENTATION_LANDSCAPE]

class GtkEncoder(json.JSONEncoder):
    def default(self, obj):
        print "default method called for:", obj
        if obj in ENUMS:
            return obj.value_name
        return json.JSONEncoder.default(self, obj)

print json.dumps(gtk.PAGE_ORIENTATION_LANDSCAPE, cls=GtkEncoder)

default. , GTK. default , <enum GTK_PAGE_ORIENTATION_LANDSCAPE of type GtkPageOrientation>, JSON.

, / ? , - , dict dicts.

+3
2

, gtk.PAGE_ORIENTATION_LANDSCAPE class 'gtk._gtk.PageOrientation', type 'gobject.GEnum', , , type 'int'.

, GTK- ints, json- , int , , default .

, json , : -/ ( , - ). , isinstance(value, (int, long)).

, , json encoder , json . encoder.py json ( /usr/lib/python2.7/json/encoder.py) .

_iterencode_list() _iterencode_dict() ( _make_iterencode()), int long; , str(value). encodeInt(value) ( !) encodeInt() encoder.py:

def encodeInt(value):
  try:
    return value.value_name
  except:
    return str(value)

:

import encoder

, C , . ( , , ( ) C, Python, - , .) :

encoder.c_make_encoder = None

:

print encoder.JSONEncoder().encode({
  gtk.PAGE_ORIENTATION_PORTRAIT: [
    gtk.PAGE_ORIENTATION_LANDSCAPE
  ],
  gtk.PAGE_ORIENTATION_LANDSCAPE: gtk.PAGE_ORIENTATION_PORTRAIT })

:

{"GTK_PAGE_ORIENTATION_PORTRAIT": [GTK_PAGE_ORIENTATION_LANDSCAPE], "GTK_PAGE_ORIENTATION_LANDSCAPE": GTK_PAGE_ORIENTATION_PORTRAIT}

, Json dict . . int - - . .

http://pastebin.com/2HAtN9E8, .

+2

, gtk, .

IntEnum Enum, str Enum, , , , .

import json
from enum import IntEnum

class TrekCaptains(IntEnum):
    Kirk  = 0
    Picard = 1

    def __str__(self):
        return '{0}'.format(self.value)


s = {TrekCaptains.Kirk:'Original Series',
     TrekCaptains.Picard:'Next Generation'}
j = json.dumps(s)
print j

#result
#{"0": "Original Series", "1": "Next Generation"}
0

Source: https://habr.com/ru/post/1676680/


All Articles