diff --git a/src/main/java/in/dragonbra/javasteam/types/KeyValue.java b/src/main/java/in/dragonbra/javasteam/types/KeyValue.java index 793da8b1..92c01233 100644 --- a/src/main/java/in/dragonbra/javasteam/types/KeyValue.java +++ b/src/main/java/in/dragonbra/javasteam/types/KeyValue.java @@ -520,7 +520,7 @@ private void recursiveSaveBinaryToStreamCore(OutputStream os) throws IOException // Only supported types ATM: // 1. KeyValue with children (no value itself) // 2. String KeyValue - if (!children.isEmpty()) { + if (value == null) { os.write(Type.NONE.code()); os.write(name.getBytes(Charset.forName("UTF-8"))); os.write(0); @@ -532,11 +532,7 @@ private void recursiveSaveBinaryToStreamCore(OutputStream os) throws IOException os.write(Type.STRING.code()); os.write(name.getBytes(Charset.forName("UTF-8"))); os.write(0); - if (value == null) { - os.write("".getBytes(Charset.forName("UTF-8"))); - } else { - os.write(value.getBytes(Charset.forName("UTF-8"))); - } + os.write(value.getBytes(Charset.forName("UTF-8"))); os.write(0); } } diff --git a/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java b/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java index a4b27ec0..4de10e73 100644 --- a/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java +++ b/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java @@ -363,7 +363,7 @@ public void keyValuesSavesTextToFile() throws IOException { String text; File temporaryFile = folder.newFile(); - kv.saveToFile(temporaryFile, false ); + kv.saveToFile(temporaryFile, false); text = new String(Files.readAllBytes(Paths.get(temporaryFile.getPath()))); assertEquals(expected, text); @@ -431,6 +431,53 @@ public void keyValuesEscapesTextWhenSerializing() throws IOException { assertEquals(expectedValue, text); } + @Test + public void keyValuesTextPreserveEmptyObjects() throws IOException { + KeyValue kv = new KeyValue("key"); + kv.getChildren().add(new KeyValue("emptyObj")); + kv.getChildren().add(new KeyValue("emptyString", "")); + + String text; + MemoryStream ms = new MemoryStream(); + kv.saveToStream(ms.asOutputStream(), false); + ms.seek(0, SeekOrigin.BEGIN); + + text = new String(ms.toByteArray()); + + String expectedValue = "\"key\"\n{\n\t\"emptyObj\"\n\t{\n\t}\n\t\"emptyString\"\t\t\"\"\n}\n"; + assertEquals(expectedValue, text); + } + + @Test + public void keyValuesBinaryPreserveEmptyObjects() throws IOException { + String expectedHexString = "006B65790000656D7074794F626A000801656D707479537472696E6700000808"; + + KeyValue kv = new KeyValue("key"); + kv.getChildren().add(new KeyValue("emptyObj")); + kv.getChildren().add(new KeyValue("emptyString", "")); + + KeyValue deserializedKv = new KeyValue(); + byte[] binaryValue; + + MemoryStream ms = new MemoryStream(); + kv.saveToStream(ms.asOutputStream(), true); + ms.seek(0, SeekOrigin.BEGIN); + + binaryValue = ms.toByteArray(); + deserializedKv.tryReadAsBinary(ms); + + StringBuilder hexValue = new StringBuilder(); + for (byte value : binaryValue) { + String string = String.format("%02X", value); + hexValue.append(string); + } + + assertEquals(expectedHexString, hexValue.toString()); + assertNull(deserializedKv.get("emptyObj").getValue()); + assertTrue(deserializedKv.get("emptyObj").getChildren().isEmpty()); + assertEquals("", deserializedKv.get("emptyString").getValue()); + } + @Test public void DecodesBinaryWithFieldType10() throws IOException, DecoderException { String hex = "00546573744F626A656374000A6B65790001020304050607080808";