Skip to content

Commit 8474104

Browse files
committed
refactor: remove jackson dependency
Close #100
1 parent b9fb24b commit 8474104

5 files changed

Lines changed: 61 additions & 25 deletions

File tree

‎pom.xml‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,6 @@
167167
<artifactId>font-awesome</artifactId>
168168
<version>4.7.0</version>
169169
<scope>test</scope>
170-
</dependency>
171-
<dependency>
172-
<groupId>com.fasterxml.jackson.core</groupId>
173-
<artifactId>jackson-databind</artifactId>
174-
<version>2.13.4.2</version>
175170
</dependency>
176171
<dependency>
177172
<groupId>com.flowingcode.vaadin.addons.demo</groupId>

‎src/main/java/com/flowingcode/vaadin/addons/orgchart/OrgChart.java‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
package com.flowingcode.vaadin.addons.orgchart;
2121

2222

23-
import com.fasterxml.jackson.core.JsonProcessingException;
24-
import com.fasterxml.jackson.databind.ObjectMapper;
2523
import com.flowingcode.vaadin.addons.orgchart.client.OrgChartState;
2624
import com.flowingcode.vaadin.addons.orgchart.event.ChildrenAddedEvent;
2725
import com.flowingcode.vaadin.addons.orgchart.event.NodeUpdatedEvent;
2826
import com.flowingcode.vaadin.addons.orgchart.event.NodesRemovedEvent;
2927
import com.flowingcode.vaadin.addons.orgchart.event.ParentAddedEvent;
3028
import com.flowingcode.vaadin.addons.orgchart.event.SiblingsAddedEvent;
3129
import com.flowingcode.vaadin.jsonmigration.JsonMigration;
30+
import com.flowingcode.vaadin.jsonmigration.JsonSerializer;
3231
import com.vaadin.flow.component.AttachEvent;
3332
import com.vaadin.flow.component.ClientCallable;
3433
import com.vaadin.flow.component.ComponentEvent;
@@ -39,7 +38,9 @@
3938
import com.vaadin.flow.component.dependency.NpmPackage;
4039
import com.vaadin.flow.component.html.Div;
4140
import com.vaadin.flow.shared.Registration;
41+
import elemental.json.Json;
4242
import elemental.json.JsonArray;
43+
import elemental.json.JsonValue;
4344
import java.util.ArrayList;
4445
import java.util.Collections;
4546
import java.util.List;
@@ -97,23 +98,28 @@ protected void onAttach(AttachEvent attachEvent) {
9798
public void initializeChart() {}
9899

99100
public void setValue(OrgChartItem orgChartItem) {
100-
String value = convertToJsonObj(orgChartItem);
101+
String value = convertToJsonObj(orgChartItem).toJson();
101102
getState().value = value;
102103
}
103104

104105
protected OrgChartState getState() {
105106
return this.state;
106107
}
107108

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));
115121
}
116-
return result;
122+
return array;
117123
}
118124

119125
public void setChartNodeTitle(String chartNodeTitle) {
@@ -425,10 +431,11 @@ public void addSiblings(Integer nodeId, List<OrgChartItem> siblings) {
425431
}
426432

427433
// Update the visual representation by calling the client-side method addSiblings
428-
String siblingsJson = convertToJsonObj(siblings);
434+
JsonValue siblingsJson = convertToJsonObj(siblings);
429435
this.getElement().executeJs("this.addSiblings($0, $1)", nodeId, siblingsJson);
430436
}
431437

438+
432439
/**
433440
* Handles sibling addition events from the client side. Converts the received JsonArray of
434441
* sibling IDs to a List and fires a {@link SiblingsAddedEvent}.
@@ -502,7 +509,7 @@ public void addChildren(Integer nodeId, List<OrgChartItem> children) {
502509
appendItemsToParent(targetNode, children);
503510

504511
// Update the visual representation
505-
String itemsJson = convertToJsonObj(children);
512+
JsonValue itemsJson = convertToJsonObj(children);
506513
if (currentChildrenEmpty) {
507514
this.getElement().executeJs("this.addChildren($0, $1)", nodeId, itemsJson);
508515
} else {
@@ -636,7 +643,7 @@ public void addParent(OrgChartItem newParentItem) {
636643
this.orgChartItem = newParentItem;
637644

638645
// Update the visual representation by calling the client-side method addParent
639-
String parentJson = convertToJsonObj(newParentItem);
646+
JsonValue parentJson = convertToJsonObj(newParentItem);
640647
this.getElement().executeJs("this.addParent($0)", parentJson);
641648
}
642649

@@ -708,7 +715,7 @@ public void updateNode(Integer nodeId, OrgChartItem newDataItem) {
708715
}
709716

710717
// Call the client-side JS function to update the visual representation
711-
String newDataJson = convertToJsonObj(newDataItem);
718+
JsonValue newDataJson = convertToJsonObj(newDataItem);
712719
this.getElement().executeJs("this.updateNode($0, $1)", nodeId, newDataJson);
713720
} else {
714721
throw new IllegalArgumentException("Node not found: " + nodeId);

‎src/main/java/com/flowingcode/vaadin/addons/orgchart/OrgChartItem.java‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
package com.flowingcode.vaadin.addons.orgchart;
2121

2222

23+
import elemental.json.Json;
24+
import elemental.json.JsonArray;
25+
import elemental.json.JsonObject;
26+
import elemental.json.JsonValue;
2327
import java.io.Serializable;
2428
import java.util.ArrayList;
2529
import java.util.Collections;
@@ -28,8 +32,6 @@
2832
import java.util.Map;
2933
import java.util.Optional;
3034

31-
import com.fasterxml.jackson.annotation.JsonProperty;
32-
3335
/** @author pbartolo */
3436
@SuppressWarnings("serial")
3537
public class OrgChartItem implements Serializable {
@@ -152,7 +154,6 @@ public boolean isHybrid() {
152154
* @param hybrid {@code true} to mark this node as hybrid; {@code false}
153155
* otherwise
154156
*/
155-
@JsonProperty("isHybrid")
156157
public void setHybrid(boolean hybrid) {
157158
this.hybrid = hybrid;
158159
}
@@ -192,4 +193,36 @@ private void printChildren(OrgChartItem item, StringBuilder sb, int count) {
192193
printChildren(item.getChildren().get(i), sb, count);
193194
}
194195
}
196+
197+
JsonValue toJson() {
198+
JsonObject json = Json.createObject();
199+
json.put("name", toJson(name));
200+
json.put("title", toJson(title));
201+
json.put("className", toJson(className));
202+
json.put("id", id != null ? Json.create(id) : Json.createNull());
203+
json.put("isHybrid", Json.create(hybrid));
204+
205+
JsonObject dataJson = Json.createObject();
206+
if (data != null) {
207+
for (Map.Entry<String, String> entry : data.entrySet()) {
208+
dataJson.put(entry.getKey(), toJson(entry.getValue()));
209+
}
210+
}
211+
json.put("data", dataJson);
212+
213+
JsonArray childrenJson = Json.createArray();
214+
if (children != null) {
215+
for (OrgChartItem child : children) {
216+
childrenJson.set(childrenJson.length(), child.toJson());
217+
}
218+
}
219+
json.put("children", childrenJson);
220+
221+
return json;
222+
}
223+
224+
private static JsonValue toJson(String value) {
225+
return value != null ? Json.create(value) : Json.createNull();
226+
}
227+
195228
}

‎src/main/java/com/flowingcode/vaadin/addons/orgchart/client/OrgChartState.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import com.flowingcode.vaadin.addons.orgchart.client.constants.ChartConstants;
2424
import com.flowingcode.vaadin.addons.orgchart.client.enums.ChartDirectionEnum;
2525
import java.io.Serializable;
26+
import lombok.Getter;
2627

2728
/** @author pbartolo */
2829
@SuppressWarnings("serial")
30+
@Getter
2931
public class OrgChartState implements Serializable {
3032

3133
public String value;

‎src/main/resources/META-INF/frontend/fc-orgchart.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ import JSONDigger from "./json-digger.js";
3333
*/
3434
class FCOrgChart extends PolymerElement {
3535

36-
initializeOrgChart(statestring,data,identifier) {
36+
initializeOrgChart(state,data,identifier) {
3737
var $ = window.jQuery || jQuery;
38-
var state = $.parseJSON(statestring);
3938

4039
let exportChart = state.chartExportButton;
4140
let exportExt = state.chartExportFileExtension;

0 commit comments

Comments
 (0)