def thrift2json(tft):
 if isinstance(tft, type(None)):
  return None
 if isinstance(tft, (float, int, long, complex, basestring)):
  return tft
 if isinstance(tft, dict):
  d = {}
  for (key, val) in tft.iteritems():
   d[key] = thrift2json(val)
  return d
 if isinstance(tft, list):
  return [thrift2json(x) for x in tft]
 if isinstance(tft, set):
  return dict(((x, True) for x in tft))
 json = {}
 d = {}
 if hasattr(tft, '__dict__'):
  d = tft.__dict__
 elif hasattr(tft, '__slots__'):
  d = tft.__slots__
 else:
  return {}
 for k in d:
  v = getattr(tft, k)
  json[k] = thrift2json(v)
 return json

def thrift2json ( tft ) : if isinstance ( tft , type ( None ) ) : return None if isinstance ( tft , ( float , int , long , complex , basestring ) ) : return tft if isinstance ( tft , dict ) : d = { } for ( key , val ) in tft . iteritems ( ) : d [ key ] = thrift2json ( val ) return d if isinstance ( tft , list ) : return [ thrift2json ( x ) for x in tft ] if isinstance ( tft , set ) : return dict ( ( ( x , True ) for x in tft ) ) json = { } d = { } if hasattr ( tft , '__dict__' ) : d = tft . __dict__ elif hasattr ( tft , '__slots__' ) : d = tft . __slots__ else : return { } for k in d : v = getattr ( tft , k ) json [ k ] = thrift2json ( v ) return json
convert a thrift structure to a json compatible dictionary by recursing over the dictionary .