Python Forum
serialize/deserialize data from/to json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
serialize/deserialize data from/to json
#1
so i have some classes
class Record:
    def __init__(self, a, b, c):
        # a, b, c are strings
        self.a = a
        self.b = b
        self.c = c

class Info:
    def __init__(self, t, d):
        # t, d are strings
        self.t = t
        self.d = d

class Containter:
    def __init__(self):
        self.infos = {} # str to Info hash
        self.storage = {} # str to (str to list of Records) hash
for example, instance of a container:
{"r1": {"c1": [Record("a", "b", "c"), Record("h", "u", "e")],
        "c2": [Record("t", "b", "r"), Record("q", "o", "m")]
       },

"r2": {"c3": [Record("c", "j", "m"), Record("h", "u", "e")]
      }
}
i tried to use jsonpickle but the output isn't simple to store in file and then deserialize

i read about subclassing jsonencoder, but i could do it only for record class, not container
Reply
#2
I think it is an interesting question, so I wrote this script. It is certainly not terse code, but it works!
from functools import singledispatch
import json

class Record:
    def __init__(self, a, b, c):
        # a, b, c are strings
        self.a = a
        self.b = b
        self.c = c
 
class Info:
    def __init__(self, t, d):
        # t, d are strings
        self.t = t
        self.d = d
 
class Container:
    def __init__(self):
        self.infos = {} # str to Info hash
        self.storage = {} # str to (str to list of Records) hash

'''
example
{"r1": {"c1": [Record("a", "b", "c"), Record("h", "u", "e")],
        "c2": [Record("t", "b", "r"), Record("q", "o", "m")]
       },
 
"r2": {"c3": [Record("c", "j", "m"), Record("h", "u", "e")]
      }
}
'''

############# Encoding ##############

class CannotConvert(Exception):
    pass

@singledispatch
def my_converter(ob):
    raise CannotConvert

@my_converter.register(Container)
def _(ob):
    return {
        'my_type': 'Container',
        'infos': ob.infos,
        'storage': ob.storage,
    }

@my_converter.register(Record)
def _(ob):
    return {
        'my_type': 'Record',
        'a': ob.a,
        'b': ob.b,
        'c': ob.c,
    }

@my_converter.register(Info)
def _(ob):
    return {
        'my_type': 'Info',
        't': ob.t,
        'd': ob.d,
    }


class MyEncoder(json.JSONEncoder):
    def default(self, ob):
        try:
            return my_converter(ob)
        except CannotConvert:
            pass
        return json.JSONEncoder.default(self, ob)

############## Decoding #################

builders = {}

def builder(tp):
    def decorator(func):
        builders[tp] = func
        return func
    return decorator

def my_hook(pairs):
    dic = dict(pairs)
    try:
        typename = dic['my_type']
        tp = {
            'Container': Container,
            'Info': Info,
            'Record': Record,
        }[typename]
    except KeyError:
        return dic
    dic.pop('my_type')
    return builders[tp](dic)


@builder(Record)
def _(dic):
    return Record(dic['a'], dic['b'], dic['c'])

@builder(Info)
def _(dic):
    return Info(dic['t'], dic['d'])

@builder(Container)
def _(dic):
    ob = Container()
    for k in ('infos', 'storage'):
        setattr(ob, k, dic[k])
    return ob



kont = Container()
kont.storage['r1'] = {
    'c1': [Record('a', 'b', 'c'), Record('h', 'u', 'e')],
    'c2': [Record('t', 'b', 'r'), Record('q', 'o', 'm')],
}
kont.storage['r2'] = {
    'c3': [Record('c', 'j', 'm'), Record('h', 'u', 'e')],
}
kont.infos['i1'] = Info('foo', 'bar')
kont.infos['i2'] = Info('baz', 'qux')

s = json.dumps(kont, cls=MyEncoder)
print(s)
c = json.loads(s, object_pairs_hook=my_hook)
print(c)
print(c.infos)
print(c.storage)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 68 Yesterday, 05:16 PM
Last Post: deanhystad
  Read nested data from JSON - Getting an error marlonbown 5 1,310 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Reading Data from JSON tpolim008 2 1,032 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Convert nested sample json api data into csv in python shantanu97 3 2,726 May-21-2022, 01:30 PM
Last Post: deanhystad
  Struggling with Juggling JSON Data SamWatt 7 1,821 May-09-2022, 02:49 AM
Last Post: snippsat
  json api data parsing elvis 0 902 Apr-21-2022, 11:59 PM
Last Post: elvis
  Deserialize Complex Json to object using Marshmallow tlopezdh 2 2,086 Dec-09-2021, 06:44 PM
Last Post: tlopezdh
  Capture json data JohnnyCoffee 0 1,175 Nov-18-2021, 03:19 PM
Last Post: JohnnyCoffee
  Serializing Python data Correctly (JSON) JgKSuperstar 4 2,040 Nov-04-2021, 07:31 PM
Last Post: JgKSuperstar
  How to save json data in a dataframe shantanu97 1 2,122 Apr-15-2021, 02:44 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020