After receiving JSON data that can be in the form of a TextField object or a DateField object, both of which inherit from the Field superclass, I am faced with the task of converting this JSON into a Field object. To further complicate matters, I need to cast this Field object into the appropriate type based on an ID contained within the JSON. While I can manually create a new TextField object and populate it with the JSON values, I am looking for a way to directly cast the JSON into the corresponding class since the attributes align. For example, attempting something like
newField = <TextareaField>field;
does not yield the desired outcome when I check if newField instanceof TextField
. Any suggestions on how to successfully cast an object to another type?