JSON Serialization

JSON Serialization

john_dalessandro
Contributor Contributor
2,784 Views
7 Replies
Message 1 of 8

JSON Serialization

john_dalessandro
Contributor
Contributor

Are there any libraries anyone has made for serializing the base level Revit API DB objects? I've looked around a bit and found some examples of serializing specific objects for a specific use case, but I'd like a more generalized option. Default serialization fails for a few types for a number of reasons. For example, a Parameter won't serialize if it isn't a shared parameter because the .GUID getter throws an exception. 

0 Likes
2,785 Views
7 Replies
Replies (7)
Message 2 of 8

perry.swoboda
Advocate
Advocate

I ran into similar problems when I was first trying to learn about the Revit object structure.  What I did was copy Jeremy Tammik's excellent RevitLookup addin, replaced the code that would display information with specific JSON serialization code (can't let JSON do it itself, it gets stuck on recursion), and then called everything recursively.

Horribly ugly code on my part, but it gave interesting information.

Check out Jeremy's RevitLookup github here.  Maybe you can look at how the addin does it to see what you need for serialization.

https://github.com/jeremytammik/RevitLookup

0 Likes
Message 3 of 8

john_dalessandro
Contributor
Contributor

Yeah, I'm trying to avoid the overhead of making a large number of wrappers for Revit objects. I looked at that project when I was building a similar one, https://github.com/klhengineers/RevitDeveloperTools, and ran into the same thing. Maintaining all those wrappers also means maintaining it across Revit versions.

 

My current plan is to make a minimal number of JsonConverters since a few of the Revit classes work just fine, like ElementId. That way I'll only have to support those classes and I won't need an extra model layer.

0 Likes
Message 4 of 8

hlipperjohn
Community Visitor
Community Visitor
0 Likes
Message 5 of 8

perry.swoboda
Advocate
Advocate

Thanks hlipperhjohn.

The problem with using default json conversion is that many of the Revit objects will throw an exception when their certain properties are accessed.  A custom converter has to be written.

I have always had to write very specific json converters for what I use.  I have not thought of a generic solution, even 4 years after this question was asked.  🙂

0 Likes
Message 6 of 8

RPTHOMAS108
Mentor
Mentor

No the API class objects don't have public constructors and are not marked as serializable so they will not allow that.

 

You also have to make your business/usage case for this also since yes you can extract information from Revit and persist it in some form but you can't create API objects independent of the API i.e. what form would deserialization take on?

 

In the past I've serialised things based on looking at what a constructor to a class such as Line requires and storing those aspects but again those are not API class objects but copies of such. Some such aspects you can't roundtrip because they don't expose the properties you need to create them or vice versa.

 

 

0 Likes
Message 7 of 8

perry.swoboda
Advocate
Advocate

Ya, I guess that was what I was trying to say.  I load a custom object with what parameters I need from a Revit API object and serialize that.  I certainly agree that there is no way to deserialize some data and create a Revit object, but I don't think the original poster was asking about that.  Probably just wanted a quick way to get data out of a model without thinking about it.  I used to want that before I learned better.

0 Likes
Message 8 of 8

RPTHOMAS108
Mentor
Mentor

A couple of years back or perhaps less I was looking at round-tripping Revit geometry via the brep builder with the objects stored in xml in-between. It kind of worked ok but not all forms of geometry are exposed to the API and Hermite splines based on ellipses are quite interesting to look at (in terms of understanding the parameterisation of such).

 

I'm not even sure what the usage case for that would have been, perhaps storing a version of Revit geometry to see how it then progresses later and overlay the stored xml via external graphics.

 

There are definitely features where I think "I would like to store that aspect for later comparison' i.e. when considering how slab edge profile changes based on revisions and then visually tracking the amount of work put into those. So instead of all the overhead of overlaying two models for comparison we can instead overlay just one feature but every version of it. We all often know where the controversial/design intensive areas of a model are by other means it is then just about understanding how those areas developed.

0 Likes