-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add preliminary serialization functionality
- Loading branch information
1 parent
dc570fc
commit 50ff43c
Showing
22 changed files
with
933 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,7 @@ build/ | |
|
||
### Mac OS ### | ||
.DS_Store | ||
|
||
### Jreleaser | ||
out/ | ||
trace.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/Section.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.jonasg.xjx.serdes; | ||
|
||
import java.util.StringJoiner; | ||
|
||
public class Section { | ||
|
||
private final String name; | ||
private final boolean isLeaf; | ||
|
||
public Section(String name) { | ||
this.name = name; | ||
isLeaf = false; | ||
} | ||
|
||
public Section(String name, boolean isLeaf) { | ||
this.name = name; | ||
this.isLeaf = isLeaf; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public boolean isLeaf() { | ||
return isLeaf; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringJoiner(", ", Section.class.getSimpleName() + "[", "]") | ||
.add("name='" + name + "'") | ||
.add("isLeaf=" + isLeaf) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/XmlNodeStructureFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.jonasg.xjx.serdes; | ||
|
||
import io.jonasg.xjx.serdes.deserialize.Path; | ||
import io.jonasg.xjx.serdes.reflector.InstanceField; | ||
import io.jonasg.xjx.serdes.reflector.Reflector; | ||
import io.jonasg.xjx.serdes.seraialize.XmlNode; | ||
|
||
public class XmlNodeStructureFactory { | ||
|
||
public <T> XmlNode build(T data) { | ||
return getXmlNode(Path.parse("/"), data, null); | ||
} | ||
|
||
private <T> XmlNode getXmlNode(Path parentPath, T data, XmlNode node) { | ||
if (data != null) { | ||
for (InstanceField field : Reflector.reflect(data) | ||
.fields(f -> f.hasAnnotation(Tag.class))) { | ||
node = buildNodeForField(field, parentPath, node); | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
private XmlNode buildNodeForField(InstanceField field, Path parentPath, XmlNode rootNode) { | ||
var tag = field.getAnnotation(Tag.class); | ||
var path = parentPath.append(Path.parse(tag.path())); | ||
if (rootNode == null) { | ||
rootNode = new XmlNode(path.getRoot()); | ||
} | ||
var node = rootNode; | ||
for (int i = 1; i < path.size(); i++) { | ||
Section section = path.getSection(i); | ||
if (section.isLeaf()) { | ||
handleLeafNode(field, section, tag, node); | ||
} else { | ||
node = node.addNode(section.name()); | ||
} | ||
} | ||
return getXmlNode(path, field.getValue(), rootNode); | ||
} | ||
|
||
private static void handleLeafNode(InstanceField field, Section section, Tag tag, XmlNode node) { | ||
if (!tag.attribute().isEmpty()) { | ||
node.addNode(section.name()) | ||
.addAttribute(tag.attribute(), field.getValue()); | ||
} else { | ||
node.addValueNode(section.name(), field.getValue()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.