Deserialize ElementId: Unable to find a constructor to use for type Autodesk.Revit.DB.ElementId

Deserialize ElementId: Unable to find a constructor to use for type Autodesk.Revit.DB.ElementId

MiguelGT17
Advocate Advocate
594 Views
2 Replies
Message 1 of 3

Deserialize ElementId: Unable to find a constructor to use for type Autodesk.Revit.DB.ElementId

MiguelGT17
Advocate
Advocate

Hi folks, 
I have this class and it's throwing the following exception when deserializing a json:MiguelGT17_0-1699510533620.png

 

 

 

 

        
public class RevitItem{
       public string instanceName { get; set; }

        private List<ElementId> _location;
        public List<ElementId> location //list of ids, because there can be rooms with same name! or using same words
        {
            get { return _location; }
            set { _location = value; }
        }
public RevitItem(string instanceNAME, List<ElementId> roomLoc = null,
            ) {
            this.instanceName = instanceNAME;
            this.location = roomLoc == null ? new List<ElementId>() :roomLoc;
        }
[JsonConstructor]
        public RevitItem(string instanceNAME, List<dynamic> roomLoc = null,
            ) {
            this.instanceName = instanceNAME;
            this.location = roomLoc == null ? new List<ElementId>() : roomLoc.Select(x=>new ElementId(x.IntegerValue)).ToList();
        }

}
        

 

When I did the serialization, It seems that the List<elementId> location props turns into this object:

MiguelGT17_1-1699510600940.png

any Ideas on how to process this data and turn it into List<ElementId>?

thanks in advance

0 Likes
595 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni

Yes, sure, lots. Look at the list of ElementId constructors:

  

  • Constructors:
  • ElementId(Int32) Obsolete.
  • ElementId(Int64)
  • ElementId(BuiltInCategory)
  • ElementId(BuiltInParameter)

  

Do you see a constructor taking an integer argument?  I don't. So, implement one yourself as an extension method for the ElementId class and call the Int64 native constructor from that. If that does not work, implement an own replacement class for ElementId and figure out how to convert it after deserialisation. Or, or, or.... many paths lead to Rome.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

RPTHOMAS108
Mentor
Mentor

Storing ElementIds outside of Revit is a dubious process due to changes that occur to such during work-sharing etc. UniqueId or IFCguid is a better option but again I hear that depending on what operations you do in Revit those may change also.

 

Perhaps some form of spatial id incorporating category is the best option.