I have the following method in a web service class. I'm a little unhappy about the big block of new JProperty(...)
calls in the for loop. Is there a way to simplify that?
public string UserCatalog(string numericSessionId, JObject incomingRequestJson)
{
JObject json = new JObject();
JObject returningJson = new JObject();
JArray userCatalogArray = new JArray();
string deviceId = incomingRequestJson.SelectToken("deviceId", true).ToString();
string version = getOptionalData(incomingRequestJson, "version", "1.0.0");
requireMinVersion(incomingRequestJson);
IEnumerable<Bookcard> catalog = readerBLL.UserCatalog(numericSessionId, deviceId);
var publicKeyRSA = readerTools.GetPublicKey(HttpUtility.HtmlDecode(numericSessionId), HttpUtility.HtmlDecode(version));
//Build the userCatalog json array
foreach (Bookcard card in catalog)
{
JObject arrayEntry = new JObject(
new JProperty("bookThumbnailUrl", card.bookThumbnailUrl),
new JProperty("bookId", card.bookId),
new JProperty("bookTitle", card.bookTitle),
new JProperty("titlePrefix", card.titlePrefix),
new JProperty("author", card.author),
new JProperty("annotation", card.annotation),
new JProperty("publisher", card.publisher),
new JProperty("numPages", card.numPages),
new JProperty("returnDate", card.returnDate),
new JProperty("downloaded", card.downloaded),
new JProperty("deviceId", card.deviceId),
new JProperty("currentPageLabel", card.currentPageLabel),
new JProperty("furthestPageLabel", card.furthestPageLabel),
new JProperty("currentReadPosition", card.currentReadPosition),
new JProperty("furthestReadPosition", card.furthestReadPosition),
new JProperty("lastReadTimestamp", card.lastReadTimestamp),
new JProperty("bookLength", card.bookLength),
new JProperty("ttsEnabled", card.ttsEnabled),
new JProperty("mackinCheckoutID", card.mackinCheckoutID),
new JProperty("runtime", card.runtime),
new JProperty("readerType", card.readerType),
new JProperty("dop", card.dop),
new JProperty("externalId", card.externalId),
new JProperty("externalSessionKey", card.externalSessionKey),
new JProperty("externalCheckoutSessionKey", card.externalBookSessionKey),
new JProperty("externalCheckoutId", card.externalCheckoutId),
new JProperty("externalAccountId", card.externalAccountId + findawayBLL.AccountSuffix)
);
if (card.printAllowed && !readerTools.isOldVersion(1, 3, 0, version))
{
arrayEntry.Add(new JProperty("printAllowed", readerTools.EncryptRSA("print_OK_" + card.bookId, publicKeyRSA)));
}
userCatalogArray.Add(arrayEntry);
}
returningJson.Add("userCatalog", userCatalogArray);
return returningJson.ToString();
}