Skip to content

Commit

Permalink
Fix binary deserialization tests against YAML
Browse files Browse the repository at this point in the history
closes #633
  • Loading branch information
alamar committed Jun 9, 2023
1 parent 10ab7a8 commit 9754763
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
32 changes: 16 additions & 16 deletions src/main/java/net/openhft/chronicle/wire/YamlWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class YamlWire extends YamlWireOut<YamlWire> {
static final String SEQ_MAP = "!seqmap";
static final String BINARY_TAG = "!binary";
static final String DATA_TAG = "!data";
static final String NULL_TAG = "!null";

//for (char ch : "?%&*@`0123456789+- ',#:{}[]|>!\\".toCharArray())
private final TextValueIn valueIn = createValueIn();
Expand Down Expand Up @@ -845,9 +846,19 @@ public StringBuilder textTo(@NotNull StringBuilder sb) {
public Bytes<?> textTo(@NotNull Bytes<?> bytes) {
bytes.clear();
if (yt.current() == YamlToken.TEXT) {
bytes.clear();
bytes.append(yt.text());
yt.next();
} else if (yt.current() == YamlToken.TAG) {
if (yt.isText(NULL_TAG)) {
yt.next();
yt.next();
return null;
} else if (yt.isText(BINARY_TAG)) {
yt.next();
bytes.write((byte[]) decodeBinary(byte[].class));
} else {
throw new UnsupportedOperationException(yt.toString());
}
} else {
throw new UnsupportedOperationException(yt.toString());
}
Expand Down Expand Up @@ -878,17 +889,6 @@ public BracketType getBracketType() {
}
}

@Nullable
Bytes<?> textTo0(@NotNull Bytes<?> a) {
consumePadding();
if (yt.current() == YamlToken.TEXT) {
a.append(yt.text());
} else {
throw new UnsupportedOperationException(yt.toString());
}
return a;
}

@Nullable
StringBuilder textTo0(@NotNull StringBuilder a) {
consumePadding();
Expand All @@ -900,7 +900,7 @@ StringBuilder textTo0(@NotNull StringBuilder a) {
unescape(a, yt.blockQuote());
yt.next();
} else if (yt.current() == YamlToken.TAG) {
if (yt.isText("!null")) {
if (yt.isText(NULL_TAG)) {
yt.next();
yt.next();
return null;
Expand Down Expand Up @@ -969,7 +969,7 @@ public WireIn bytes(@NotNull ReadBytesMarshallable bytesConsumer) {
bytes.releaseLast();
}

} else if (StringUtils.isEqual(sb, "!null")) {
} else if (StringUtils.isEqual(sb, NULL_TAG)) {
bytesConsumer.readMarshallable(null);
yt.next();

Expand Down Expand Up @@ -1639,7 +1639,7 @@ private <K, V> Map<K, V> map(@NotNull final Class<K> kClass,
private <K, V> Map<K, V> typedMap(@NotNull Class<K> kClazz, @NotNull Class<V> vClass, @NotNull Map<K, V> usingMap, @NotNull StringBuilder sb) throws InvalidMarshallableException {
yt.text(sb);
yt.next();
if (("!null").contentEquals(sb)) {
if (NULL_TAG.contentEquals(sb)) {
text();
return null;

Expand Down Expand Up @@ -1766,7 +1766,7 @@ public float float32() {
public boolean isNull() {
consumePadding();

if (yt.current() == YamlToken.TAG && yt.isText("!null")) {
if (yt.current() == YamlToken.TAG && yt.isText(NULL_TAG)) {
consumeAny(0);
return true;
}
Expand Down
39 changes: 26 additions & 13 deletions src/test/java/net/openhft/chronicle/wire/BinaryInTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,49 @@
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesStore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(value = Parameterized.class)
public class BinaryInTextTest extends WireTestCommon {
private final WireType wireType;

public BinaryInTextTest(WireType wireType) {
this.wireType = wireType;
}

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> wireTypes() {
return Arrays.asList(
new Object[]{WireType.TEXT},
new Object[]{WireType.YAML});
}

@SuppressWarnings("rawtypes")
@Test
public void testBytesFromText() {
Bytes<?> a = Marshallable.fromString(Bytes.class, "A==");
Bytes<?> a = wireType.fromString(Bytes.class, "A==");
assertEquals("A==", a.toString());

BytesStore a2 = Marshallable.fromString(BytesStore.class, "A==");
BytesStore a2 = wireType.fromString(BytesStore.class, "A==");
assertEquals("A==", a2.toString());

Bytes<?> b = Marshallable.fromString(Bytes.class, "!!binary BA==");
assertEquals("[pos: 0, rlim: 1, wlim: 2147483632, cap: 2147483632 ] ǁ⒋‡٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠", b.toDebugString());
Bytes<?> b = wireType.fromString(Bytes.class, "!!binary BA==");
assertEquals("00000000 04", b.toHexString().substring(0, 58).trim());

Bytes<?> b2 = Marshallable.fromString(Bytes.class, "!!binary A1==");
assertEquals("[pos: 0, rlim: 1, wlim: 2147483632, cap: 2147483632 ] ǁ⒊‡٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠", b2.toDebugString());
Bytes<?> b2 = wireType.fromString(Bytes.class, "!!binary A1==");
assertEquals("00000000 03", b2.toHexString().substring(0, 58).trim());
}

@Test
public void testReserialize() {
BIT b = new BIT();
byte[] a = new byte[5];
b.b = Bytes.wrapForRead(a);
b.c = Bytes.wrapForRead(a);
// System.out.println(b);

BIT bit = Marshallable.fromString(BIT.class, "{\n" +
BIT bit = wireType.fromString(BIT.class, "{\n" +
"b: !!binary AAAAAAA=,\n" +
"c: !!binary CCCCCCCC,\n" +
"}");
Expand Down

0 comments on commit 9754763

Please sign in to comment.