Skip to content

Commit

Permalink
BL-743 resolve - Fix boolean serialization and de-serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jclausen committed Nov 5, 2024
1 parent 3738658 commit 75fd597
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/java/ortus/boxlang/modules/wddx/util/WDDXUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.dynamic.casters.ArrayCaster;
import ortus.boxlang.runtime.dynamic.casters.BooleanCaster;
import ortus.boxlang.runtime.dynamic.casters.DateTimeCaster;
import ortus.boxlang.runtime.dynamic.casters.GenericCaster;
import ortus.boxlang.runtime.dynamic.casters.IntegerCaster;
Expand Down Expand Up @@ -95,6 +96,9 @@ public static Object deserializeObject( XML obj ) {
case "array" : {
return obj.getXMLChildrenAsList().stream().map( WDDXUtil::deserializeObject ).collect( BLCollector.toArray() );
}
case "boolean" : {
return BooleanCaster.cast( obj.getXMLAttributes().get( Key.value ) );
}
default : {
return GenericCaster.cast( context, obj.getXMLText(), obj.getNode().getNodeName() );
}
Expand Down Expand Up @@ -126,6 +130,11 @@ public static String serializeObject( Object obj ) {
if ( obj instanceof Query ) {
return serializeQuery( QueryCaster.cast( obj ) );
}
// Booleans have a different pattern, in that they do not have an outer wrapper
if ( obj instanceof Boolean ) {
return "<boolean value=\"" + obj.toString() + "\"/>";
}

Key classKey = Key.of( StringUtil.lcFirst( obj.getClass().getSimpleName() ) );
String serialization = "<" + classKey.getName() + ( obj instanceof Array ? " length=\"" + ArrayCaster.cast( obj ).size() + "\"" : "" ) + ">";
if ( obj instanceof IStruct ) {
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/ortus/boxlang/modules/wddx/util/WDDXUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import ortus.boxlang.runtime.dynamic.casters.StructCaster;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.types.Array;
import ortus.boxlang.runtime.types.DateTime;
Expand All @@ -40,10 +41,11 @@ public class WDDXUtilTest {
@Test
void testSerializeStruct() {
IStruct test = Struct.of(
"foo", "bar"
"foo", "bar",
"isWDDX", true
);
String wddx = WDDXUtil.serializeObject( test );
assertEquals( "<struct><var name=\"foo\"><string>bar</string></var></struct>", wddx );
assertEquals( "<struct><var name=\"isWDDX\"><boolean value=\"true\"/></var><var name=\"foo\"><string>bar</string></var></struct>", wddx );
}

@DisplayName( "Test array serialization" )
Expand Down Expand Up @@ -105,12 +107,24 @@ void testParse() {
<var name=\"foo\">
<string>bar</string>
</var>
<var name=\"isWDDX\">
<boolean value=\"true\"/>
</var>
</struct>
</data>
</wddxPacket>
""".trim();
Object result = WDDXUtil.parse( wddx );
assertTrue( result instanceof IStruct );
IStruct deserialized = StructCaster.cast( result );
assertTrue( deserialized.containsKey( Key.of( "flea" ) ) );
assertTrue( deserialized.get( Key.of( "flea" ) ) instanceof Array );
assertTrue( deserialized.containsKey( Key.of( "flah" ) ) );
assertTrue( deserialized.get( Key.of( "flah" ) ) instanceof IStruct );
assertTrue( deserialized.containsKey( Key.of( "foo" ) ) );
assertTrue( deserialized.get( Key.of( "foo" ) ) instanceof String );
assertTrue( deserialized.containsKey( Key.of( "isWDDX" ) ) );
assertTrue( deserialized.get( Key.of( "isWDDX" ) ) instanceof Boolean );
}

}

0 comments on commit 75fd597

Please sign in to comment.