-
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 better support for serializing records to XML
- Loading branch information
JonasG
committed
Feb 7, 2024
1 parent
38cea72
commit 33482a3
Showing
27 changed files
with
393 additions
and
196 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
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
6 changes: 3 additions & 3 deletions
6
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/deserialize/MapRootSaxHandler.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
1 change: 0 additions & 1 deletion
1
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/deserialize/MapWithTypeInfo.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
8 changes: 4 additions & 4 deletions
8
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/deserialize/PathBasedSaxHandler.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
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
6 changes: 3 additions & 3 deletions
6
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/deserialize/TypedValueMapSaxHandler.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
4 changes: 2 additions & 2 deletions
4
...rdes/src/main/java/io/jonasg/xjx/serdes/deserialize/accessor/ReflectiveFieldAccessor.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
4 changes: 2 additions & 2 deletions
4
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/deserialize/accessor/SetterFieldAccessor.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
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
4 changes: 2 additions & 2 deletions
4
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/seraialize/XmlNode.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
106 changes: 65 additions & 41 deletions
106
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/seraialize/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 |
---|---|---|
@@ -1,52 +1,76 @@ | ||
package io.jonasg.xjx.serdes.seraialize; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
|
||
import io.jonasg.xjx.serdes.Path; | ||
import io.jonasg.xjx.serdes.Section; | ||
import io.jonasg.xjx.serdes.Tag; | ||
import io.jonasg.xjx.serdes.Path; | ||
import io.jonasg.xjx.serdes.reflector.InstanceField; | ||
import io.jonasg.xjx.serdes.reflector.Reflector; | ||
|
||
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()); | ||
} | ||
} | ||
public static final List<Class<?>> BASIC_TYPES = List.of( | ||
String.class, Integer.class, Boolean.class, boolean.class, Long.class, long.class, BigDecimal.class, Double.class, | ||
double.class, char.class, Character.class, LocalDate.class, LocalDateTime.class, ZonedDateTime.class, byte[].class); | ||
|
||
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 && !BASIC_TYPES.contains(data.getClass()) && !data.getClass().isEnum()) { | ||
List<InstanceField> fields = Reflector.reflect(data).fields(); | ||
node = buildNodeForFields(parentPath, node, fields); | ||
} | ||
return node; | ||
} | ||
|
||
private XmlNode buildNodeForFields(Path parentPath, XmlNode node, List<InstanceField> fields) { | ||
for (InstanceField field : fields) { | ||
if (field.hasAnnotation(Tag.class)) { | ||
node = buildNodeForField(field, parentPath, node); | ||
} | ||
else if (!BASIC_TYPES.contains(field.type())) { | ||
return buildNodeForFields(parentPath, node, field.reflect().fields()); | ||
} | ||
} | ||
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 void handleLeafNode(InstanceField field, Section section, Tag tag, XmlNode node) { | ||
if (!tag.attribute().isEmpty()) { | ||
if (field.getValue() != null) { | ||
node.addNode(section.name()) | ||
.addAttribute(tag.attribute(), field.getValue()); | ||
} | ||
} | ||
else { | ||
node.addValueNode(section.name(), field.getValue()); | ||
} | ||
} | ||
|
||
} |
2 changes: 0 additions & 2 deletions
2
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/seraialize/XmlStringBuilder.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
5 changes: 3 additions & 2 deletions
5
...serdes/src/test/java/io/jonasg/xjx/serdes/deserialize/ComplexTypeDeserializationTest.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
5 changes: 3 additions & 2 deletions
5
...serdes/src/test/java/io/jonasg/xjx/serdes/deserialize/CustomValueDeserializationTest.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
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
5 changes: 3 additions & 2 deletions
5
...serdes/src/test/java/io/jonasg/xjx/serdes/deserialize/DeserializationFieldAccessTest.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
Oops, something went wrong.