-
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
1 parent
38cea72
commit 3b22c7d
Showing
28 changed files
with
408 additions
and
211 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
14 changes: 7 additions & 7 deletions
14
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/XjxSerdes.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
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
52 changes: 0 additions & 52 deletions
52
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/seraialize/XmlNodeStructureFactory.java
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
...jonasg/xjx/serdes/seraialize/XmlNode.java → .../jonasg/xjx/serdes/serialize/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
76 changes: 76 additions & 0 deletions
76
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/serialize/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,76 @@ | ||
package io.jonasg.xjx.serdes.serialize; | ||
|
||
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.reflector.InstanceField; | ||
import io.jonasg.xjx.serdes.reflector.Reflector; | ||
|
||
public class XmlNodeStructureFactory { | ||
|
||
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()); | ||
} | ||
} | ||
|
||
} |
4 changes: 1 addition & 3 deletions
4
...x/serdes/seraialize/XmlStringBuilder.java → ...jx/serdes/serialize/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
Oops, something went wrong.