honeybee icon indicating copy to clipboard operation
honeybee copied to clipboard

JSON serialization is missing for a number of Honeybee objects

Open abbaad opened this issue 6 years ago • 11 comments

Is anyone currently working on the JSON serialization-deserialization to get honeybee working better with honeybee-server-daylight?

If not, I'd like to try working on this enhancement (at least in a hackish way to test honeybee-server-daylight for my own needs first).

abbaad avatar Jan 23 '19 17:01 abbaad

It has already been implemented in all Honeybee objects. After creating an analysis recipe try .to_json() method which will give you the JSON data. Write the JSON object to a file and follow the sample code in honeybee-server-daylight to submit it for simulation.

mostaphaRoudsari avatar Jan 23 '19 19:01 mostaphaRoudsari

There are some objects which don't currently get serialized properly (e.g., RadianceNumberType) and some which can't be serialized without some architectural changes (e.g., StaticScenes which refer to .rad files that aren't machine-relocatable)

abbaad avatar Jan 23 '19 19:01 abbaad

There are some objects which don't currently get serialized properly (e.g., RadianceNumberType)

I'm not sure why would you need to serialize RadianceNumberType. Just use it's value (number) and it will be fine.

some which can't be serialized without some architectural changes (e.g., StaticScenes which refer to .rad files that aren't machine-relocatable)

This is True. All was an overestimation. Also XML files are not currently supported. If you want to go ahead and add support for missing objects that would be a great but I suggest to hold on for now as the overall workflow is currently under being redesigned and some of these objects will not look the same including the StaticScene class.

mostaphaRoudsari avatar Jan 23 '19 19:01 mostaphaRoudsari

I'm not sure why would you need to serialize RadianceNumberType. Just use it's value (number) and it will be fine.

RadianceNumberType is currently the type of some member variables for objects which are supposedly serialized by to_json(), but since it's not natively serializable, you can't get a JSON string out of it using python's json module.

abbaad avatar Jan 23 '19 19:01 abbaad

If I remember correctly we only use those types for Radiance parameters and we change radiance parameters to string in JSON objects. Do you have an example where this type creates an issue?

mostaphaRoudsari avatar Jan 23 '19 22:01 mostaphaRoudsari

Do you have an example where this type creates an issue?

Maybe I'm using the interface wrong. But here's an example of what I mean:

import json

...

_analysisRecipe.hb_objects = _HBObjects
_analysisRecipe.scene = radScene_

serialized = _analysisRecipe.to_json()

print type(serialized['surfaces'][0]['surface_material']['specularity'])

# Error in following line, because json.dumps cant't handle RadianceNumberType
string_to_POST = json.dumps(serialized)

Which gives the following output:

Found 1 opaque surfaces.
Found 0 fenestration surfaces.
Found 0 window-groups.
<class 'honeybee.radiance.datatype.RadianceNumberType'>
Runtime error (TypeErrorException): 0.0 is not JSON serializable

abbaad avatar Jan 24 '19 18:01 abbaad

This is a bug in how to_json is written for the materials. Interesting that I have never faced this issue. 😕

mostaphaRoudsari avatar Jan 24 '19 19:01 mostaphaRoudsari

Semi-related question: is it infeasible to import 3rd party libraries because of the way Grasshopper usually works? Specifically, I'm asking about jsonpickle.

abbaad avatar Jan 25 '19 17:01 abbaad

Do you mean for honeybee core library or in general?

For Honeybee we have to keep it compatible with IronPython but if you want to run it from command line or write your own script and run it outside Grasshopper/IronPython there will be no limitations.

mostaphaRoudsari avatar Jan 25 '19 18:01 mostaphaRoudsari

Sorry, I had meant for honeybee core. I wasn't sure if external dependencies are permitted by your code guidelines.

abbaad avatar Mar 01 '19 17:03 abbaad

HoneybeeCore is also used in Grasshopper and therefore must be able to run in IronPython and not install dependencies via pip. An example of an existing dependency (sort of) is in the ladybug repo where the Euclid package has been copied into the repository.

AntoineDao avatar Mar 01 '19 17:03 AntoineDao