rancavil / python-convert Goto Github PK
View Code? Open in Web Editor NEWAn API for convert an python object in a xml or json document
An API for convert an python object in a xml or json document
while converting a list of string from json to object error pops up .
Please Find below the sample code (modified to have json to object) shared in the pyconvert git
import pyconvert.pyconv
class Band(object):
name_band = str
year_band = int
class Album(object):
band_album = Band
name_album = str
year_album = int
song = [str]
band = Band()
band.name_band = "Led Zeppelin"
band.year_band = 1968
album = Album()
album.band_album = band
album.name_album = "House of Holy"
album.year_album = 1973
album.song = list()
album.song.append("The Song Remains the Same")
album.song.append("The Rain Song")
album.song.append("Over the Hills and Far Away")
album.song.append("The Crunge")
album.song.append("Dancing Days")
album.song.append("D'yer Mak'er")
album.song.append("No Quarter")
album.song.append("The Ocean")
print("#####################")
json_doc = pyconvert.pyconv.convert2JSON(album)
print(json_doc)
print("#####################################")
object= pyconvert.pyconv.convertJSON2OBJ(Album,json_doc)
print(object)
The ERROR is:
Traceback (most recent call last):
File "sample.py", line 32, in
aaa= pyconvert.pyconv.convertJSON2OBJ(Album,json_doc)
File "/usr/lib/python3.4/site-packages/pyconvert/pyconv.py", line 322, in convertJSON2OBJ
print(isinstance(cls.dict[key],list))
KeyError: 'songs'
The convertJSON2OBJ
function doesn't correctly convert attribute types, and fails completely if the JSON contains a null value. The issue is in lines 312-314 of pyconv.py:
if not isinstance(value,list) and not isinstance(value,dict):
type_conv = type(value)
attrs[key] = type_conv(value)
This gets the target type from the source document, then calls it with the value it is derived from. This is particularly problematic if the value is None
as the NoneType
constructor takes no arguements.
Replacing the above with the following code works as expected:
if value is None:
continue
if not isinstance(value,list) and not isinstance(value,dict):
type_conv = hasattr(cls, key) and getattr(cls, key) or None
if type_conv is None:
continue
attrs[key] = type_conv(value)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.