summaryrefslogtreecommitdiffstats
path: root/base/common/python/pki/encoder.py
blob: 7fee57e7106adf841b9d3dd3f58d8eb4843d6ee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import json
import pki.system

TYPES = {}
NOTYPES = {}

class CustomTypeEncoder(json.JSONEncoder):
    """A custom JSONEncoder class that knows how to encode core custom
    objects.

    Custom objects are encoded as JSON object literals (ie, dicts) with
    one key, 'TypeName' where 'TypeName' is the actual name of the
    type to which the object belongs.  That single key maps to another
    object literal which is just the __dict__ of the object encoded."""

    def default(self, obj):
        for k, v in TYPES.items():
            if isinstance(obj, v):
                return { k: obj.__dict__ }
        for k, v in NOTYPES.items():
            if isinstance(obj, v):
                return obj.__dict__
        return json.JSONEncoder.default(self, obj)


def CustomTypeDecoder(dct):
    if len(dct) == 1:
        type_name, value = dct.items()[0]
        if type_name in TYPES:
            return TYPES[type_name].from_dict(value)
    return dct