function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MickleMickle 

Dynamically build Apex Class attribute datatype?

I searched around for a bit, but couldn't find an exact answer I'm looking for. Essentially, I'm trying to dynamically build the attribute (specifically the datatype of a List's members) of a Class.

 

In playing around with the Etsy API, I've build a couple of Apex Classes that mirror the data structure that is being returned. Ideally, I'm trying to use the System.JSON class to parse the results into an Object, which I'm going to do some manipulation on, prior to creating an sObject.

 

public class EtsyShop {
		public integer shop_id;
		public string shop_name;
		public string first_line;
		public string second_line;
		public string city;
		public string state;
...
	}

 

public class EtsyListing {
		public Integer listing_id;
		public string state;
		public Integer user_id;
		public Integer category_id;
		public string title;
		public string description;
                ....
	}

 When I get any results from the Etsy API, it comes wrapped up with some nice information, count of records, params used, etc. When I ask for Etsy Listings, I get a List of EtsyListings, when I ask for EtsyShops, I get a list of  EtsyShops. Right now I have hacked this together by having a Class for each results scenario. Ideally I would like this to only be one Class, with a dynamically built List of Type X.

 

{ "count": 1, "results": [ { "listing_id": 60008911, "state": "active", "user_id": 9646570, "category_id": 69170659, "title": "The Arbour Shirt - Large (bow tie sold separately)", "description": "The Rover Classics Collection evokes memories of cool mornings on the dock and lazy afternoons by the sea. Designed for the very small to the very big, our tailored jackets, fitted shirts, flirty dresses, and sophisticated accessories are the perfect additions to a well-groomed wardrobe.\r\n\r\nOur arbour shirt is perfect for casual weekends by the water. In our signature cotton madras print with scalloped lilac trim and back velcro closure with button detail.\r\n\r\nWash in cold water and hang to dry.\r\n\r\nThe Arbour Shirt can be ordered in sizes xs to xxl (please refer to sizing chart image).", "creation_tsz": 1328669426, "ending_tsz": 1339041600, "original_creation_tsz": 1288115249, "last_modified_tsz": 1328669426, "price": "30.00", "currency_code": "USD", "quantity": 1, "tags": [ "Pets", "Clothing", "Dog", "pug", "plaid", "blue", "shirt", "collar", "accessories", "bow_tie", "formal", "madras", "preppy", "pet_lover" ], "category_path": [ "Pets", "Clothing", "Dog" ], "materials": [ "cotton" ], "shop_section_id": 7299132, "featured_rank": null, "state_tsz": 1328669426, "hue": 43, "saturation": 7, "brightness": 60, "is_black_and_white": false, "url": "http://www.etsy.com/listing/60008911/the-arbour-shirt-large-bow-tie-sold?utm_source=apigeeapiconsole&utm_medium=api&utm_campaign=api", "views": 6342, "num_favorers": 164, "who_made": "i_did", "is_supply": "false", "when_made": false, "recipient": null, "occasion": null, "style": null } ], "params": { "listing_id": "60008911" }, "type": "Listing", "pagination": {} }

 

public class EtsyListingResult {
	public integer count;
	public List<Etsy.EtsyListing> results; //only difference
	public Map<String, String> params;
	public string type;
	public Map<String, String> pagination;
}

 

public class EtsyShopResult {
	public integer count;
	public List<Etsy.EtsyShop> results; //only difference
	public Map<String, String> params;
	public string type;
	public Map<String, String> pagination;
}

You can see the only difference between these two Classes is the datatype of the results list.

 

If it helps, the way these are being used is in the following code:

 

EtsyShopResult myEtsyShopResult = (EtsyShopResult)JSON.deserialize(responseBody, EtsyShopResult.class);

 

Any help would be appreciated. I'm just trying to make this code as resusable and scalable as possible, and I feel like there are a couple of higher level techniques that I'm not fully grasping. :)