|
20 | 20 | package com.flowingcode.vaadin.addons.orgchart; |
21 | 21 |
|
22 | 22 |
|
23 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
24 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
25 | 23 | import com.flowingcode.vaadin.addons.orgchart.client.OrgChartState; |
26 | 24 | import com.flowingcode.vaadin.addons.orgchart.event.ChildrenAddedEvent; |
27 | 25 | import com.flowingcode.vaadin.addons.orgchart.event.NodeUpdatedEvent; |
28 | 26 | import com.flowingcode.vaadin.addons.orgchart.event.NodesRemovedEvent; |
29 | 27 | import com.flowingcode.vaadin.addons.orgchart.event.ParentAddedEvent; |
30 | 28 | import com.flowingcode.vaadin.addons.orgchart.event.SiblingsAddedEvent; |
31 | 29 | import com.flowingcode.vaadin.jsonmigration.JsonMigration; |
| 30 | +import com.flowingcode.vaadin.jsonmigration.JsonSerializer; |
32 | 31 | import com.vaadin.flow.component.AttachEvent; |
33 | 32 | import com.vaadin.flow.component.ClientCallable; |
34 | 33 | import com.vaadin.flow.component.ComponentEvent; |
|
39 | 38 | import com.vaadin.flow.component.dependency.NpmPackage; |
40 | 39 | import com.vaadin.flow.component.html.Div; |
41 | 40 | import com.vaadin.flow.shared.Registration; |
| 41 | +import elemental.json.Json; |
42 | 42 | import elemental.json.JsonArray; |
| 43 | +import elemental.json.JsonValue; |
43 | 44 | import java.util.ArrayList; |
44 | 45 | import java.util.Collections; |
45 | 46 | import java.util.List; |
@@ -97,23 +98,28 @@ protected void onAttach(AttachEvent attachEvent) { |
97 | 98 | public void initializeChart() {} |
98 | 99 |
|
99 | 100 | public void setValue(OrgChartItem orgChartItem) { |
100 | | - String value = convertToJsonObj(orgChartItem); |
| 101 | + String value = convertToJsonObj(orgChartItem).toJson(); |
101 | 102 | getState().value = value; |
102 | 103 | } |
103 | 104 |
|
104 | 105 | protected OrgChartState getState() { |
105 | 106 | return this.state; |
106 | 107 | } |
107 | 108 |
|
108 | | - private String convertToJsonObj(Object orgChartItem) { |
109 | | - String result = null; |
110 | | - ObjectMapper mapper = new ObjectMapper(); |
111 | | - try { |
112 | | - result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(orgChartItem); |
113 | | - } catch (JsonProcessingException e) { |
114 | | - e.printStackTrace(); |
| 109 | + private JsonValue convertToJsonObj(OrgChartState state) { |
| 110 | + return JsonSerializer.toJson(state); |
| 111 | + } |
| 112 | + |
| 113 | + private JsonValue convertToJsonObj(OrgChartItem orgChartItem) { |
| 114 | + return orgChartItem.toJson(); |
| 115 | + } |
| 116 | + |
| 117 | + private JsonValue convertToJsonObj(List<OrgChartItem> items) { |
| 118 | + JsonArray array = Json.createArray(); |
| 119 | + for (OrgChartItem item : items) { |
| 120 | + array.set(array.length(), convertToJsonObj(item)); |
115 | 121 | } |
116 | | - return result; |
| 122 | + return array; |
117 | 123 | } |
118 | 124 |
|
119 | 125 | public void setChartNodeTitle(String chartNodeTitle) { |
@@ -425,10 +431,11 @@ public void addSiblings(Integer nodeId, List<OrgChartItem> siblings) { |
425 | 431 | } |
426 | 432 |
|
427 | 433 | // Update the visual representation by calling the client-side method addSiblings |
428 | | - String siblingsJson = convertToJsonObj(siblings); |
| 434 | + JsonValue siblingsJson = convertToJsonObj(siblings); |
429 | 435 | this.getElement().executeJs("this.addSiblings($0, $1)", nodeId, siblingsJson); |
430 | 436 | } |
431 | 437 |
|
| 438 | + |
432 | 439 | /** |
433 | 440 | * Handles sibling addition events from the client side. Converts the received JsonArray of |
434 | 441 | * sibling IDs to a List and fires a {@link SiblingsAddedEvent}. |
@@ -502,7 +509,7 @@ public void addChildren(Integer nodeId, List<OrgChartItem> children) { |
502 | 509 | appendItemsToParent(targetNode, children); |
503 | 510 |
|
504 | 511 | // Update the visual representation |
505 | | - String itemsJson = convertToJsonObj(children); |
| 512 | + JsonValue itemsJson = convertToJsonObj(children); |
506 | 513 | if (currentChildrenEmpty) { |
507 | 514 | this.getElement().executeJs("this.addChildren($0, $1)", nodeId, itemsJson); |
508 | 515 | } else { |
@@ -636,7 +643,7 @@ public void addParent(OrgChartItem newParentItem) { |
636 | 643 | this.orgChartItem = newParentItem; |
637 | 644 |
|
638 | 645 | // Update the visual representation by calling the client-side method addParent |
639 | | - String parentJson = convertToJsonObj(newParentItem); |
| 646 | + JsonValue parentJson = convertToJsonObj(newParentItem); |
640 | 647 | this.getElement().executeJs("this.addParent($0)", parentJson); |
641 | 648 | } |
642 | 649 |
|
@@ -708,7 +715,7 @@ public void updateNode(Integer nodeId, OrgChartItem newDataItem) { |
708 | 715 | } |
709 | 716 |
|
710 | 717 | // Call the client-side JS function to update the visual representation |
711 | | - String newDataJson = convertToJsonObj(newDataItem); |
| 718 | + JsonValue newDataJson = convertToJsonObj(newDataItem); |
712 | 719 | this.getElement().executeJs("this.updateNode($0, $1)", nodeId, newDataJson); |
713 | 720 | } else { |
714 | 721 | throw new IllegalArgumentException("Node not found: " + nodeId); |
|
0 commit comments