Giter Site home page Giter Site logo

python-convert's People

Contributors

rancavil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

python-convert's Issues

Error while converting JSON2Obj for list of string

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'

Failure when converting JSON to object

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.