/modules/apps/headless/headless-admin-content/headless-admin-content-client/src/main/java/com/liferay/headless/admin/content/client/serdes/v1_0/StructuredContentSerDes.java

https://github.com/danielreuther/liferay-portal · Java · 1157 lines · 902 code · 238 blank · 17 comment · 340 complexity · 6b09933e0122d52cd4a9506655fc05d3 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.headless.admin.content.client.serdes.v1_0;
  15. import com.liferay.headless.admin.content.client.dto.v1_0.ContentField;
  16. import com.liferay.headless.admin.content.client.dto.v1_0.CustomField;
  17. import com.liferay.headless.admin.content.client.dto.v1_0.RelatedContent;
  18. import com.liferay.headless.admin.content.client.dto.v1_0.RenderedContent;
  19. import com.liferay.headless.admin.content.client.dto.v1_0.StructuredContent;
  20. import com.liferay.headless.admin.content.client.dto.v1_0.TaxonomyCategoryBrief;
  21. import com.liferay.headless.admin.content.client.json.BaseJSONParser;
  22. import java.text.DateFormat;
  23. import java.text.SimpleDateFormat;
  24. import java.util.Iterator;
  25. import java.util.Map;
  26. import java.util.Objects;
  27. import java.util.Set;
  28. import java.util.TreeMap;
  29. import java.util.stream.Stream;
  30. import javax.annotation.Generated;
  31. /**
  32. * @author Javier Gamarra
  33. * @generated
  34. */
  35. @Generated("")
  36. public class StructuredContentSerDes {
  37. public static StructuredContent toDTO(String json) {
  38. StructuredContentJSONParser structuredContentJSONParser =
  39. new StructuredContentJSONParser();
  40. return structuredContentJSONParser.parseToDTO(json);
  41. }
  42. public static StructuredContent[] toDTOs(String json) {
  43. StructuredContentJSONParser structuredContentJSONParser =
  44. new StructuredContentJSONParser();
  45. return structuredContentJSONParser.parseToDTOs(json);
  46. }
  47. public static String toJSON(StructuredContent structuredContent) {
  48. if (structuredContent == null) {
  49. return "null";
  50. }
  51. StringBuilder sb = new StringBuilder();
  52. sb.append("{");
  53. DateFormat liferayToJSONDateFormat = new SimpleDateFormat(
  54. "yyyy-MM-dd'T'HH:mm:ssXX");
  55. if (structuredContent.getActions() != null) {
  56. if (sb.length() > 1) {
  57. sb.append(", ");
  58. }
  59. sb.append("\"actions\": ");
  60. sb.append(_toJSON(structuredContent.getActions()));
  61. }
  62. if (structuredContent.getAggregateRating() != null) {
  63. if (sb.length() > 1) {
  64. sb.append(", ");
  65. }
  66. sb.append("\"aggregateRating\": ");
  67. sb.append(structuredContent.getAggregateRating());
  68. }
  69. if (structuredContent.getAssetLibraryKey() != null) {
  70. if (sb.length() > 1) {
  71. sb.append(", ");
  72. }
  73. sb.append("\"assetLibraryKey\": ");
  74. sb.append("\"");
  75. sb.append(_escape(structuredContent.getAssetLibraryKey()));
  76. sb.append("\"");
  77. }
  78. if (structuredContent.getAvailableLanguages() != null) {
  79. if (sb.length() > 1) {
  80. sb.append(", ");
  81. }
  82. sb.append("\"availableLanguages\": ");
  83. sb.append("[");
  84. for (int i = 0;
  85. i < structuredContent.getAvailableLanguages().length; i++) {
  86. sb.append("\"");
  87. sb.append(
  88. _escape(structuredContent.getAvailableLanguages()[i]));
  89. sb.append("\"");
  90. if ((i + 1) <
  91. structuredContent.getAvailableLanguages().length) {
  92. sb.append(", ");
  93. }
  94. }
  95. sb.append("]");
  96. }
  97. if (structuredContent.getContentFields() != null) {
  98. if (sb.length() > 1) {
  99. sb.append(", ");
  100. }
  101. sb.append("\"contentFields\": ");
  102. sb.append("[");
  103. for (int i = 0; i < structuredContent.getContentFields().length;
  104. i++) {
  105. sb.append(structuredContent.getContentFields()[i]);
  106. if ((i + 1) < structuredContent.getContentFields().length) {
  107. sb.append(", ");
  108. }
  109. }
  110. sb.append("]");
  111. }
  112. if (structuredContent.getContentStructureId() != null) {
  113. if (sb.length() > 1) {
  114. sb.append(", ");
  115. }
  116. sb.append("\"contentStructureId\": ");
  117. sb.append(structuredContent.getContentStructureId());
  118. }
  119. if (structuredContent.getCreator() != null) {
  120. if (sb.length() > 1) {
  121. sb.append(", ");
  122. }
  123. sb.append("\"creator\": ");
  124. sb.append(structuredContent.getCreator());
  125. }
  126. if (structuredContent.getCustomFields() != null) {
  127. if (sb.length() > 1) {
  128. sb.append(", ");
  129. }
  130. sb.append("\"customFields\": ");
  131. sb.append("[");
  132. for (int i = 0; i < structuredContent.getCustomFields().length;
  133. i++) {
  134. sb.append(structuredContent.getCustomFields()[i]);
  135. if ((i + 1) < structuredContent.getCustomFields().length) {
  136. sb.append(", ");
  137. }
  138. }
  139. sb.append("]");
  140. }
  141. if (structuredContent.getDateCreated() != null) {
  142. if (sb.length() > 1) {
  143. sb.append(", ");
  144. }
  145. sb.append("\"dateCreated\": ");
  146. sb.append("\"");
  147. sb.append(
  148. liferayToJSONDateFormat.format(
  149. structuredContent.getDateCreated()));
  150. sb.append("\"");
  151. }
  152. if (structuredContent.getDateModified() != null) {
  153. if (sb.length() > 1) {
  154. sb.append(", ");
  155. }
  156. sb.append("\"dateModified\": ");
  157. sb.append("\"");
  158. sb.append(
  159. liferayToJSONDateFormat.format(
  160. structuredContent.getDateModified()));
  161. sb.append("\"");
  162. }
  163. if (structuredContent.getDatePublished() != null) {
  164. if (sb.length() > 1) {
  165. sb.append(", ");
  166. }
  167. sb.append("\"datePublished\": ");
  168. sb.append("\"");
  169. sb.append(
  170. liferayToJSONDateFormat.format(
  171. structuredContent.getDatePublished()));
  172. sb.append("\"");
  173. }
  174. if (structuredContent.getDescription() != null) {
  175. if (sb.length() > 1) {
  176. sb.append(", ");
  177. }
  178. sb.append("\"description\": ");
  179. sb.append("\"");
  180. sb.append(_escape(structuredContent.getDescription()));
  181. sb.append("\"");
  182. }
  183. if (structuredContent.getDescription_i18n() != null) {
  184. if (sb.length() > 1) {
  185. sb.append(", ");
  186. }
  187. sb.append("\"description_i18n\": ");
  188. sb.append(_toJSON(structuredContent.getDescription_i18n()));
  189. }
  190. if (structuredContent.getExternalReferenceCode() != null) {
  191. if (sb.length() > 1) {
  192. sb.append(", ");
  193. }
  194. sb.append("\"externalReferenceCode\": ");
  195. sb.append("\"");
  196. sb.append(_escape(structuredContent.getExternalReferenceCode()));
  197. sb.append("\"");
  198. }
  199. if (structuredContent.getFriendlyUrlPath() != null) {
  200. if (sb.length() > 1) {
  201. sb.append(", ");
  202. }
  203. sb.append("\"friendlyUrlPath\": ");
  204. sb.append("\"");
  205. sb.append(_escape(structuredContent.getFriendlyUrlPath()));
  206. sb.append("\"");
  207. }
  208. if (structuredContent.getFriendlyUrlPath_i18n() != null) {
  209. if (sb.length() > 1) {
  210. sb.append(", ");
  211. }
  212. sb.append("\"friendlyUrlPath_i18n\": ");
  213. sb.append(_toJSON(structuredContent.getFriendlyUrlPath_i18n()));
  214. }
  215. if (structuredContent.getId() != null) {
  216. if (sb.length() > 1) {
  217. sb.append(", ");
  218. }
  219. sb.append("\"id\": ");
  220. sb.append(structuredContent.getId());
  221. }
  222. if (structuredContent.getKey() != null) {
  223. if (sb.length() > 1) {
  224. sb.append(", ");
  225. }
  226. sb.append("\"key\": ");
  227. sb.append("\"");
  228. sb.append(_escape(structuredContent.getKey()));
  229. sb.append("\"");
  230. }
  231. if (structuredContent.getKeywords() != null) {
  232. if (sb.length() > 1) {
  233. sb.append(", ");
  234. }
  235. sb.append("\"keywords\": ");
  236. sb.append("[");
  237. for (int i = 0; i < structuredContent.getKeywords().length; i++) {
  238. sb.append("\"");
  239. sb.append(_escape(structuredContent.getKeywords()[i]));
  240. sb.append("\"");
  241. if ((i + 1) < structuredContent.getKeywords().length) {
  242. sb.append(", ");
  243. }
  244. }
  245. sb.append("]");
  246. }
  247. if (structuredContent.getNumberOfComments() != null) {
  248. if (sb.length() > 1) {
  249. sb.append(", ");
  250. }
  251. sb.append("\"numberOfComments\": ");
  252. sb.append(structuredContent.getNumberOfComments());
  253. }
  254. if (structuredContent.getPriority() != null) {
  255. if (sb.length() > 1) {
  256. sb.append(", ");
  257. }
  258. sb.append("\"priority\": ");
  259. sb.append(structuredContent.getPriority());
  260. }
  261. if (structuredContent.getRelatedContents() != null) {
  262. if (sb.length() > 1) {
  263. sb.append(", ");
  264. }
  265. sb.append("\"relatedContents\": ");
  266. sb.append("[");
  267. for (int i = 0; i < structuredContent.getRelatedContents().length;
  268. i++) {
  269. sb.append(structuredContent.getRelatedContents()[i]);
  270. if ((i + 1) < structuredContent.getRelatedContents().length) {
  271. sb.append(", ");
  272. }
  273. }
  274. sb.append("]");
  275. }
  276. if (structuredContent.getRenderedContents() != null) {
  277. if (sb.length() > 1) {
  278. sb.append(", ");
  279. }
  280. sb.append("\"renderedContents\": ");
  281. sb.append("[");
  282. for (int i = 0; i < structuredContent.getRenderedContents().length;
  283. i++) {
  284. sb.append(structuredContent.getRenderedContents()[i]);
  285. if ((i + 1) < structuredContent.getRenderedContents().length) {
  286. sb.append(", ");
  287. }
  288. }
  289. sb.append("]");
  290. }
  291. if (structuredContent.getSiteId() != null) {
  292. if (sb.length() > 1) {
  293. sb.append(", ");
  294. }
  295. sb.append("\"siteId\": ");
  296. sb.append(structuredContent.getSiteId());
  297. }
  298. if (structuredContent.getSubscribed() != null) {
  299. if (sb.length() > 1) {
  300. sb.append(", ");
  301. }
  302. sb.append("\"subscribed\": ");
  303. sb.append(structuredContent.getSubscribed());
  304. }
  305. if (structuredContent.getTaxonomyCategoryBriefs() != null) {
  306. if (sb.length() > 1) {
  307. sb.append(", ");
  308. }
  309. sb.append("\"taxonomyCategoryBriefs\": ");
  310. sb.append("[");
  311. for (int i = 0;
  312. i < structuredContent.getTaxonomyCategoryBriefs().length;
  313. i++) {
  314. sb.append(structuredContent.getTaxonomyCategoryBriefs()[i]);
  315. if ((i + 1) <
  316. structuredContent.getTaxonomyCategoryBriefs().length) {
  317. sb.append(", ");
  318. }
  319. }
  320. sb.append("]");
  321. }
  322. if (structuredContent.getTaxonomyCategoryIds() != null) {
  323. if (sb.length() > 1) {
  324. sb.append(", ");
  325. }
  326. sb.append("\"taxonomyCategoryIds\": ");
  327. sb.append("[");
  328. for (int i = 0;
  329. i < structuredContent.getTaxonomyCategoryIds().length; i++) {
  330. sb.append(structuredContent.getTaxonomyCategoryIds()[i]);
  331. if ((i + 1) <
  332. structuredContent.getTaxonomyCategoryIds().length) {
  333. sb.append(", ");
  334. }
  335. }
  336. sb.append("]");
  337. }
  338. if (structuredContent.getTitle() != null) {
  339. if (sb.length() > 1) {
  340. sb.append(", ");
  341. }
  342. sb.append("\"title\": ");
  343. sb.append("\"");
  344. sb.append(_escape(structuredContent.getTitle()));
  345. sb.append("\"");
  346. }
  347. if (structuredContent.getTitle_i18n() != null) {
  348. if (sb.length() > 1) {
  349. sb.append(", ");
  350. }
  351. sb.append("\"title_i18n\": ");
  352. sb.append(_toJSON(structuredContent.getTitle_i18n()));
  353. }
  354. if (structuredContent.getUuid() != null) {
  355. if (sb.length() > 1) {
  356. sb.append(", ");
  357. }
  358. sb.append("\"uuid\": ");
  359. sb.append("\"");
  360. sb.append(_escape(structuredContent.getUuid()));
  361. sb.append("\"");
  362. }
  363. if (structuredContent.getViewableBy() != null) {
  364. if (sb.length() > 1) {
  365. sb.append(", ");
  366. }
  367. sb.append("\"viewableBy\": ");
  368. sb.append("\"");
  369. sb.append(structuredContent.getViewableBy());
  370. sb.append("\"");
  371. }
  372. sb.append("}");
  373. return sb.toString();
  374. }
  375. public static Map<String, Object> toMap(String json) {
  376. StructuredContentJSONParser structuredContentJSONParser =
  377. new StructuredContentJSONParser();
  378. return structuredContentJSONParser.parseToMap(json);
  379. }
  380. public static Map<String, String> toMap(
  381. StructuredContent structuredContent) {
  382. if (structuredContent == null) {
  383. return null;
  384. }
  385. Map<String, String> map = new TreeMap<>();
  386. DateFormat liferayToJSONDateFormat = new SimpleDateFormat(
  387. "yyyy-MM-dd'T'HH:mm:ssXX");
  388. if (structuredContent.getActions() == null) {
  389. map.put("actions", null);
  390. }
  391. else {
  392. map.put("actions", String.valueOf(structuredContent.getActions()));
  393. }
  394. if (structuredContent.getAggregateRating() == null) {
  395. map.put("aggregateRating", null);
  396. }
  397. else {
  398. map.put(
  399. "aggregateRating",
  400. String.valueOf(structuredContent.getAggregateRating()));
  401. }
  402. if (structuredContent.getAssetLibraryKey() == null) {
  403. map.put("assetLibraryKey", null);
  404. }
  405. else {
  406. map.put(
  407. "assetLibraryKey",
  408. String.valueOf(structuredContent.getAssetLibraryKey()));
  409. }
  410. if (structuredContent.getAvailableLanguages() == null) {
  411. map.put("availableLanguages", null);
  412. }
  413. else {
  414. map.put(
  415. "availableLanguages",
  416. String.valueOf(structuredContent.getAvailableLanguages()));
  417. }
  418. if (structuredContent.getContentFields() == null) {
  419. map.put("contentFields", null);
  420. }
  421. else {
  422. map.put(
  423. "contentFields",
  424. String.valueOf(structuredContent.getContentFields()));
  425. }
  426. if (structuredContent.getContentStructureId() == null) {
  427. map.put("contentStructureId", null);
  428. }
  429. else {
  430. map.put(
  431. "contentStructureId",
  432. String.valueOf(structuredContent.getContentStructureId()));
  433. }
  434. if (structuredContent.getCreator() == null) {
  435. map.put("creator", null);
  436. }
  437. else {
  438. map.put("creator", String.valueOf(structuredContent.getCreator()));
  439. }
  440. if (structuredContent.getCustomFields() == null) {
  441. map.put("customFields", null);
  442. }
  443. else {
  444. map.put(
  445. "customFields",
  446. String.valueOf(structuredContent.getCustomFields()));
  447. }
  448. if (structuredContent.getDateCreated() == null) {
  449. map.put("dateCreated", null);
  450. }
  451. else {
  452. map.put(
  453. "dateCreated",
  454. liferayToJSONDateFormat.format(
  455. structuredContent.getDateCreated()));
  456. }
  457. if (structuredContent.getDateModified() == null) {
  458. map.put("dateModified", null);
  459. }
  460. else {
  461. map.put(
  462. "dateModified",
  463. liferayToJSONDateFormat.format(
  464. structuredContent.getDateModified()));
  465. }
  466. if (structuredContent.getDatePublished() == null) {
  467. map.put("datePublished", null);
  468. }
  469. else {
  470. map.put(
  471. "datePublished",
  472. liferayToJSONDateFormat.format(
  473. structuredContent.getDatePublished()));
  474. }
  475. if (structuredContent.getDescription() == null) {
  476. map.put("description", null);
  477. }
  478. else {
  479. map.put(
  480. "description",
  481. String.valueOf(structuredContent.getDescription()));
  482. }
  483. if (structuredContent.getDescription_i18n() == null) {
  484. map.put("description_i18n", null);
  485. }
  486. else {
  487. map.put(
  488. "description_i18n",
  489. String.valueOf(structuredContent.getDescription_i18n()));
  490. }
  491. if (structuredContent.getExternalReferenceCode() == null) {
  492. map.put("externalReferenceCode", null);
  493. }
  494. else {
  495. map.put(
  496. "externalReferenceCode",
  497. String.valueOf(structuredContent.getExternalReferenceCode()));
  498. }
  499. if (structuredContent.getFriendlyUrlPath() == null) {
  500. map.put("friendlyUrlPath", null);
  501. }
  502. else {
  503. map.put(
  504. "friendlyUrlPath",
  505. String.valueOf(structuredContent.getFriendlyUrlPath()));
  506. }
  507. if (structuredContent.getFriendlyUrlPath_i18n() == null) {
  508. map.put("friendlyUrlPath_i18n", null);
  509. }
  510. else {
  511. map.put(
  512. "friendlyUrlPath_i18n",
  513. String.valueOf(structuredContent.getFriendlyUrlPath_i18n()));
  514. }
  515. if (structuredContent.getId() == null) {
  516. map.put("id", null);
  517. }
  518. else {
  519. map.put("id", String.valueOf(structuredContent.getId()));
  520. }
  521. if (structuredContent.getKey() == null) {
  522. map.put("key", null);
  523. }
  524. else {
  525. map.put("key", String.valueOf(structuredContent.getKey()));
  526. }
  527. if (structuredContent.getKeywords() == null) {
  528. map.put("keywords", null);
  529. }
  530. else {
  531. map.put(
  532. "keywords", String.valueOf(structuredContent.getKeywords()));
  533. }
  534. if (structuredContent.getNumberOfComments() == null) {
  535. map.put("numberOfComments", null);
  536. }
  537. else {
  538. map.put(
  539. "numberOfComments",
  540. String.valueOf(structuredContent.getNumberOfComments()));
  541. }
  542. if (structuredContent.getPriority() == null) {
  543. map.put("priority", null);
  544. }
  545. else {
  546. map.put(
  547. "priority", String.valueOf(structuredContent.getPriority()));
  548. }
  549. if (structuredContent.getRelatedContents() == null) {
  550. map.put("relatedContents", null);
  551. }
  552. else {
  553. map.put(
  554. "relatedContents",
  555. String.valueOf(structuredContent.getRelatedContents()));
  556. }
  557. if (structuredContent.getRenderedContents() == null) {
  558. map.put("renderedContents", null);
  559. }
  560. else {
  561. map.put(
  562. "renderedContents",
  563. String.valueOf(structuredContent.getRenderedContents()));
  564. }
  565. if (structuredContent.getSiteId() == null) {
  566. map.put("siteId", null);
  567. }
  568. else {
  569. map.put("siteId", String.valueOf(structuredContent.getSiteId()));
  570. }
  571. if (structuredContent.getSubscribed() == null) {
  572. map.put("subscribed", null);
  573. }
  574. else {
  575. map.put(
  576. "subscribed",
  577. String.valueOf(structuredContent.getSubscribed()));
  578. }
  579. if (structuredContent.getTaxonomyCategoryBriefs() == null) {
  580. map.put("taxonomyCategoryBriefs", null);
  581. }
  582. else {
  583. map.put(
  584. "taxonomyCategoryBriefs",
  585. String.valueOf(structuredContent.getTaxonomyCategoryBriefs()));
  586. }
  587. if (structuredContent.getTaxonomyCategoryIds() == null) {
  588. map.put("taxonomyCategoryIds", null);
  589. }
  590. else {
  591. map.put(
  592. "taxonomyCategoryIds",
  593. String.valueOf(structuredContent.getTaxonomyCategoryIds()));
  594. }
  595. if (structuredContent.getTitle() == null) {
  596. map.put("title", null);
  597. }
  598. else {
  599. map.put("title", String.valueOf(structuredContent.getTitle()));
  600. }
  601. if (structuredContent.getTitle_i18n() == null) {
  602. map.put("title_i18n", null);
  603. }
  604. else {
  605. map.put(
  606. "title_i18n",
  607. String.valueOf(structuredContent.getTitle_i18n()));
  608. }
  609. if (structuredContent.getUuid() == null) {
  610. map.put("uuid", null);
  611. }
  612. else {
  613. map.put("uuid", String.valueOf(structuredContent.getUuid()));
  614. }
  615. if (structuredContent.getViewableBy() == null) {
  616. map.put("viewableBy", null);
  617. }
  618. else {
  619. map.put(
  620. "viewableBy",
  621. String.valueOf(structuredContent.getViewableBy()));
  622. }
  623. return map;
  624. }
  625. public static class StructuredContentJSONParser
  626. extends BaseJSONParser<StructuredContent> {
  627. @Override
  628. protected StructuredContent createDTO() {
  629. return new StructuredContent();
  630. }
  631. @Override
  632. protected StructuredContent[] createDTOArray(int size) {
  633. return new StructuredContent[size];
  634. }
  635. @Override
  636. protected void setField(
  637. StructuredContent structuredContent, String jsonParserFieldName,
  638. Object jsonParserFieldValue) {
  639. if (Objects.equals(jsonParserFieldName, "actions")) {
  640. if (jsonParserFieldValue != null) {
  641. structuredContent.setActions(
  642. (Map)StructuredContentSerDes.toMap(
  643. (String)jsonParserFieldValue));
  644. }
  645. }
  646. else if (Objects.equals(jsonParserFieldName, "aggregateRating")) {
  647. if (jsonParserFieldValue != null) {
  648. structuredContent.setAggregateRating(
  649. AggregateRatingSerDes.toDTO(
  650. (String)jsonParserFieldValue));
  651. }
  652. }
  653. else if (Objects.equals(jsonParserFieldName, "assetLibraryKey")) {
  654. if (jsonParserFieldValue != null) {
  655. structuredContent.setAssetLibraryKey(
  656. (String)jsonParserFieldValue);
  657. }
  658. }
  659. else if (Objects.equals(
  660. jsonParserFieldName, "availableLanguages")) {
  661. if (jsonParserFieldValue != null) {
  662. structuredContent.setAvailableLanguages(
  663. toStrings((Object[])jsonParserFieldValue));
  664. }
  665. }
  666. else if (Objects.equals(jsonParserFieldName, "contentFields")) {
  667. if (jsonParserFieldValue != null) {
  668. structuredContent.setContentFields(
  669. Stream.of(
  670. toStrings((Object[])jsonParserFieldValue)
  671. ).map(
  672. object -> ContentFieldSerDes.toDTO((String)object)
  673. ).toArray(
  674. size -> new ContentField[size]
  675. ));
  676. }
  677. }
  678. else if (Objects.equals(
  679. jsonParserFieldName, "contentStructureId")) {
  680. if (jsonParserFieldValue != null) {
  681. structuredContent.setContentStructureId(
  682. Long.valueOf((String)jsonParserFieldValue));
  683. }
  684. }
  685. else if (Objects.equals(jsonParserFieldName, "creator")) {
  686. if (jsonParserFieldValue != null) {
  687. structuredContent.setCreator(
  688. CreatorSerDes.toDTO((String)jsonParserFieldValue));
  689. }
  690. }
  691. else if (Objects.equals(jsonParserFieldName, "customFields")) {
  692. if (jsonParserFieldValue != null) {
  693. structuredContent.setCustomFields(
  694. Stream.of(
  695. toStrings((Object[])jsonParserFieldValue)
  696. ).map(
  697. object -> CustomFieldSerDes.toDTO((String)object)
  698. ).toArray(
  699. size -> new CustomField[size]
  700. ));
  701. }
  702. }
  703. else if (Objects.equals(jsonParserFieldName, "dateCreated")) {
  704. if (jsonParserFieldValue != null) {
  705. structuredContent.setDateCreated(
  706. toDate((String)jsonParserFieldValue));
  707. }
  708. }
  709. else if (Objects.equals(jsonParserFieldName, "dateModified")) {
  710. if (jsonParserFieldValue != null) {
  711. structuredContent.setDateModified(
  712. toDate((String)jsonParserFieldValue));
  713. }
  714. }
  715. else if (Objects.equals(jsonParserFieldName, "datePublished")) {
  716. if (jsonParserFieldValue != null) {
  717. structuredContent.setDatePublished(
  718. toDate((String)jsonParserFieldValue));
  719. }
  720. }
  721. else if (Objects.equals(jsonParserFieldName, "description")) {
  722. if (jsonParserFieldValue != null) {
  723. structuredContent.setDescription(
  724. (String)jsonParserFieldValue);
  725. }
  726. }
  727. else if (Objects.equals(jsonParserFieldName, "description_i18n")) {
  728. if (jsonParserFieldValue != null) {
  729. structuredContent.setDescription_i18n(
  730. (Map)StructuredContentSerDes.toMap(
  731. (String)jsonParserFieldValue));
  732. }
  733. }
  734. else if (Objects.equals(
  735. jsonParserFieldName, "externalReferenceCode")) {
  736. if (jsonParserFieldValue != null) {
  737. structuredContent.setExternalReferenceCode(
  738. (String)jsonParserFieldValue);
  739. }
  740. }
  741. else if (Objects.equals(jsonParserFieldName, "friendlyUrlPath")) {
  742. if (jsonParserFieldValue != null) {
  743. structuredContent.setFriendlyUrlPath(
  744. (String)jsonParserFieldValue);
  745. }
  746. }
  747. else if (Objects.equals(
  748. jsonParserFieldName, "friendlyUrlPath_i18n")) {
  749. if (jsonParserFieldValue != null) {
  750. structuredContent.setFriendlyUrlPath_i18n(
  751. (Map)StructuredContentSerDes.toMap(
  752. (String)jsonParserFieldValue));
  753. }
  754. }
  755. else if (Objects.equals(jsonParserFieldName, "id")) {
  756. if (jsonParserFieldValue != null) {
  757. structuredContent.setId(
  758. Long.valueOf((String)jsonParserFieldValue));
  759. }
  760. }
  761. else if (Objects.equals(jsonParserFieldName, "key")) {
  762. if (jsonParserFieldValue != null) {
  763. structuredContent.setKey((String)jsonParserFieldValue);
  764. }
  765. }
  766. else if (Objects.equals(jsonParserFieldName, "keywords")) {
  767. if (jsonParserFieldValue != null) {
  768. structuredContent.setKeywords(
  769. toStrings((Object[])jsonParserFieldValue));
  770. }
  771. }
  772. else if (Objects.equals(jsonParserFieldName, "numberOfComments")) {
  773. if (jsonParserFieldValue != null) {
  774. structuredContent.setNumberOfComments(
  775. Integer.valueOf((String)jsonParserFieldValue));
  776. }
  777. }
  778. else if (Objects.equals(jsonParserFieldName, "priority")) {
  779. if (jsonParserFieldValue != null) {
  780. structuredContent.setPriority(
  781. Double.valueOf((String)jsonParserFieldValue));
  782. }
  783. }
  784. else if (Objects.equals(jsonParserFieldName, "relatedContents")) {
  785. if (jsonParserFieldValue != null) {
  786. structuredContent.setRelatedContents(
  787. Stream.of(
  788. toStrings((Object[])jsonParserFieldValue)
  789. ).map(
  790. object -> RelatedContentSerDes.toDTO((String)object)
  791. ).toArray(
  792. size -> new RelatedContent[size]
  793. ));
  794. }
  795. }
  796. else if (Objects.equals(jsonParserFieldName, "renderedContents")) {
  797. if (jsonParserFieldValue != null) {
  798. structuredContent.setRenderedContents(
  799. Stream.of(
  800. toStrings((Object[])jsonParserFieldValue)
  801. ).map(
  802. object -> RenderedContentSerDes.toDTO(
  803. (String)object)
  804. ).toArray(
  805. size -> new RenderedContent[size]
  806. ));
  807. }
  808. }
  809. else if (Objects.equals(jsonParserFieldName, "siteId")) {
  810. if (jsonParserFieldValue != null) {
  811. structuredContent.setSiteId(
  812. Long.valueOf((String)jsonParserFieldValue));
  813. }
  814. }
  815. else if (Objects.equals(jsonParserFieldName, "subscribed")) {
  816. if (jsonParserFieldValue != null) {
  817. structuredContent.setSubscribed(
  818. (Boolean)jsonParserFieldValue);
  819. }
  820. }
  821. else if (Objects.equals(
  822. jsonParserFieldName, "taxonomyCategoryBriefs")) {
  823. if (jsonParserFieldValue != null) {
  824. structuredContent.setTaxonomyCategoryBriefs(
  825. Stream.of(
  826. toStrings((Object[])jsonParserFieldValue)
  827. ).map(
  828. object -> TaxonomyCategoryBriefSerDes.toDTO(
  829. (String)object)
  830. ).toArray(
  831. size -> new TaxonomyCategoryBrief[size]
  832. ));
  833. }
  834. }
  835. else if (Objects.equals(
  836. jsonParserFieldName, "taxonomyCategoryIds")) {
  837. if (jsonParserFieldValue != null) {
  838. structuredContent.setTaxonomyCategoryIds(
  839. toLongs((Object[])jsonParserFieldValue));
  840. }
  841. }
  842. else if (Objects.equals(jsonParserFieldName, "title")) {
  843. if (jsonParserFieldValue != null) {
  844. structuredContent.setTitle((String)jsonParserFieldValue);
  845. }
  846. }
  847. else if (Objects.equals(jsonParserFieldName, "title_i18n")) {
  848. if (jsonParserFieldValue != null) {
  849. structuredContent.setTitle_i18n(
  850. (Map)StructuredContentSerDes.toMap(
  851. (String)jsonParserFieldValue));
  852. }
  853. }
  854. else if (Objects.equals(jsonParserFieldName, "uuid")) {
  855. if (jsonParserFieldValue != null) {
  856. structuredContent.setUuid((String)jsonParserFieldValue);
  857. }
  858. }
  859. else if (Objects.equals(jsonParserFieldName, "viewableBy")) {
  860. if (jsonParserFieldValue != null) {
  861. structuredContent.setViewableBy(
  862. StructuredContent.ViewableBy.create(
  863. (String)jsonParserFieldValue));
  864. }
  865. }
  866. }
  867. }
  868. private static String _escape(Object object) {
  869. String string = String.valueOf(object);
  870. for (String[] strings : BaseJSONParser.JSON_ESCAPE_STRINGS) {
  871. string = string.replace(strings[0], strings[1]);
  872. }
  873. return string;
  874. }
  875. private static String _toJSON(Map<String, ?> map) {
  876. StringBuilder sb = new StringBuilder("{");
  877. @SuppressWarnings("unchecked")
  878. Set set = map.entrySet();
  879. @SuppressWarnings("unchecked")
  880. Iterator<Map.Entry<String, ?>> iterator = set.iterator();
  881. while (iterator.hasNext()) {
  882. Map.Entry<String, ?> entry = iterator.next();
  883. sb.append("\"");
  884. sb.append(entry.getKey());
  885. sb.append("\": ");
  886. Object value = entry.getValue();
  887. Class<?> valueClass = value.getClass();
  888. if (value instanceof Map) {
  889. sb.append(_toJSON((Map)value));
  890. }
  891. else if (valueClass.isArray()) {
  892. Object[] values = (Object[])value;
  893. sb.append("[");
  894. for (int i = 0; i < values.length; i++) {
  895. sb.append("\"");
  896. sb.append(_escape(values[i]));
  897. sb.append("\"");
  898. if ((i + 1) < values.length) {
  899. sb.append(", ");
  900. }
  901. }
  902. sb.append("]");
  903. }
  904. else if (value instanceof String) {
  905. sb.append("\"");
  906. sb.append(_escape(entry.getValue()));
  907. sb.append("\"");
  908. }
  909. else {
  910. sb.append(String.valueOf(entry.getValue()));
  911. }
  912. if (iterator.hasNext()) {
  913. sb.append(", ");
  914. }
  915. }
  916. sb.append("}");
  917. return sb.toString();
  918. }
  919. }