PD_Gear_ViewProviderPythonFeature is not JSON serializable

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
arcol
Posts: 223
Joined: Sun Nov 10, 2013 9:02 am

PD_Gear_ViewProviderPythonFeature is not JSON serializable

Post by arcol »

Hi,

I have just updated freecad package to the latest daily ppa (0.14.3210 (Git)).
When I hit on the save button, I get this error sometime:

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
<type 'exceptions.TypeError'>: <ViewProviderPythonFeature object> is not JSON serializable

Did anyone else run into the same error? I'll let this topic open until I figure out what exactly triggers this exception.
Once this error shows up, it is reproducible to every save button hit.
arcol
Posts: 223
Joined: Sun Nov 10, 2013 9:02 am

Re: ViewProviderPythonFeature is not JSON serializable

Post by arcol »

Aham, found it.

If you open the Part design workbench, select the Involute gear item,
and you create an involute gear sketch, then you hit on the save button, and voila, the same error.

Anyone can confirm?

I attached such faulty .fcstd file.
json_non_serializable.fcstd
JSON non serializable
(23.34 KiB) Downloaded 58 times
I have put my Sherlock hat onto my head:

Code: Select all

File "/usr/lib/python2.7/json/encoder.py", line 185, in default
    raise TypeError(repr(o) + " is not JSON serializable" + str([name for name,thing in inspect.getmembers([])]) )
<type 'exceptions.TypeError'>: <ViewProviderPythonFeature object> is not JSON serializable['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
User avatar
NormandC
Veteran
Posts: 18589
Joined: Sat Feb 06, 2010 9:52 pm
Location: Québec, Canada

Re: ViewProviderPythonFeature is not JSON serializable

Post by NormandC »

Hello,

I get the same thing on the daily PPA package and on FreeCAD compiled from tonight's master.

The error does not prevent the file from being saved, and the error is repeated when the file is opened again. That gear generator is cool!

PPA version
OS: Ubuntu 12.04.4 LTS
Platform: 64-bit
Version: 0.14.3213 (Git)
Branch: master
Hash: 995b1466590bf10cb9e6d19588700cec1cf46154
Python version: 2.7.3
Qt version: 4.8.1
Coin version: 3.1.3
SoQt version: 1.5.0
OCC version: 6.7.0



Compiled from branch
OS: Ubuntu 12.04.4 LTS
Platform: 64-bit
Version: 0.14.3218 (Git)
Branch: master
Hash: a84abfda39d2c8c5301f9f73b917c2dcd22c142c
Python version: 2.7.3
Qt version: 4.8.1
Coin version: 3.1.3
SoQt version: 1.5.0
OCC version: 6.7.0
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: ViewProviderPythonFeature is not JSON serializable

Post by jmaustpc »

When saving a default gear, padded then pocketed, with this file
geartest.fcstd
(277.88 KiB) Downloaded 55 times
I get he same error


Traceback (most recent call last):
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
<type 'exceptions.TypeError'>: <ViewProviderPythonFeature object> is not JSON serializable


OS: Ubuntu 12.04.4 LTS
Platform: 64-bit
Version: 0.14.3214 (Git)
Branch: master
Hash: daf8b1d49eec1368819430c8e3020c19352e6c0e
Python version: 2.7.3
Qt version: 4.8.2
Coin version: 3.1.3
SoQt version: 1.5.0
OCC version: 6.7.0


I do not get the error with a simple pad and pocket from sketches.for example like this
padcircpocketrectest.fcstd
(13.13 KiB) Downloaded 68 times
Jim
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: ViewProviderPythonFeature is not JSON serializable

Post by jmaustpc »

I also get the same error as last posted if I just save only the default gear profile.

e.g
testjustgearprofile.fcstd
(24.4 KiB) Downloaded 64 times
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: ViewProviderPythonFeature is not JSON serializable

Post by jmaustpc »

OS: Ubuntu 12.04.4 LTS
Platform: 64-bit
Version: 0.14.3218 (Git)
Branch: master
Hash: a84abfda39d2c8c5301f9f73b917c2dcd22c142c
Python version: 2.7.3
Qt version: 4.8.2
Coin version: 3.1.3
SoQt version: 1.5.0
OCC version: 6.7.0

With these latest updates the problem is still there,

I also noticed
1)if you don't have PartDesign workbench open first, you don't get the gear icon in the tree view.
2) if you open the saved gear profile fcstd file created with this latest FC version or the previous posted above, without opening the PartDesign wb, it does open the file but you get the following error.
Traceback (most recent call last):
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
<type 'exceptions.ValueError'>: No JSON object could be decoded


however if you do open the PartDesign workbench first, (you still get the error) and then open the file the busy thinking spinning mouse icon stays active, although a cpu core is NOT maxed out.

3) after creation of a default gear profile...nothing is visible in the model window, giving the initial appearance that nothing happened, the gear is there and is visible if you click on the viewfit button, should viewfit, or something similar, be automatically called?


Jim
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: ViewProviderPythonFeature is not JSON serializable

Post by shoogen »

maybe the JSON serialisation was never tested with ViewProvider proxies that don't implement

Code: Select all

    def __getstate__(self):
        return None

    def __setstate__(self, state):
        return None
src/App/PropertyPythonObject.cpp

Code: Select all

91	        if (this->object.hasAttr("__getstate__")) {
92	            Py::Tuple args(0);
93	            Py::Callable state(this->object.getAttr("__getstate__"));
94	            dump = state.apply(args);
95	        }
96	        else if (this->object.hasAttr("__dict__")) {
97	            dump = this->object.getAttr("__dict__");
98	        }
99	        else {
100	            dump = this->object;
http://www.freecadweb.org/tracker/view.php?id=1452
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: ViewProviderPythonFeature is not JSON serializable

Post by wmayer »

Fixed in git commit 849e39f. But this only works for newly created projects.
arcol
Posts: 223
Joined: Sun Nov 10, 2013 9:02 am

Re: ViewProviderPythonFeature is not JSON serializable

Post by arcol »

wmayer wrote:Fixed in git commit 849e39f. But this only works for newly created projects.
Thank you wmayer. So basically for JSON serialize to work, you need to implement __get_state__ and __set_state__ ?

A completely unrelated problem:
The gear generator icon is grey on grey (the ubuntu menu's background is grey), so completely unnoticable, until you hover the mouse on the menuitem.

Who designed the icon? Can he/she add a white around the gear icon, so it would be noticable?

Best,
arcol
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: ViewProviderPythonFeature is not JSON serializable

Post by wmayer »

maybe the JSON serialisation was never tested with ViewProvider proxies that don't implement
The JSON serialization doesn't work for arbitrary types. It's not possible to serialize objects that have a reference to a so called "Python extension object" -- these are objects written in C/C++ and implement a certain interface so that they behave like real Python objects.
So basically for JSON serialize to work, you need to implement __get_state__ and __set_state__ ?
Yes, and it's sufficient to implement it the way I did.
Who designed the icon? Can he/she add a white around the gear icon, so it would be noticable?
Don't know but we are aware of this issue. Jim recently offered some alternatives but AFAIR there was still no decision which one to use. @Jim, is there a preferred icon we should use?
Post Reply