diff --git a/src/main/java/net/openhft/chronicle/wire/JSONWire.java b/src/main/java/net/openhft/chronicle/wire/JSONWire.java index 758d10567..cf3a7e3b5 100644 --- a/src/main/java/net/openhft/chronicle/wire/JSONWire.java +++ b/src/main/java/net/openhft/chronicle/wire/JSONWire.java @@ -230,6 +230,9 @@ public double float64() { int end = peekBack(); if (end != sep) throw new IORuntimeException("Expected " + (char) sep + " but was " + (char) end); + consumePadding(); + if (peekCode() == ',') + bytes.readSkip(1); } else { checkRewindDouble(); } diff --git a/src/main/java/net/openhft/chronicle/wire/TextWire.java b/src/main/java/net/openhft/chronicle/wire/TextWire.java index aa808ac09..55f23a363 100644 --- a/src/main/java/net/openhft/chronicle/wire/TextWire.java +++ b/src/main/java/net/openhft/chronicle/wire/TextWire.java @@ -1944,11 +1944,13 @@ public WireIn int32(@NotNull T t, @NotNull ObjIntConsumer ti) { */ long getALong() { final int code = peekCode(); + int checkForChar = 0; switch (code) { case '"': case '\'': // Skip quote characters if present around a number (e.g., "123") bytes.readSkip(1); + checkForChar = code; break; case 't': @@ -1969,7 +1971,21 @@ long getALong() { } // Read and return the long value from the stream - return bytes.parseLong(); + long l = bytes.parseLong(); + // if an end character was expected, check for it + if (checkForChar != 0) + readCheckForChar(checkForChar); + return l; + } + + private void readCheckForChar(int checkForChar) { + int lastWas = bytes.readByte(bytes.readPosition() - 1); + if (lastWas != checkForChar) { + throw new IORuntimeException("Expected a " + (char) checkForChar + " but was " + (char) lastWas); + } + consumePadding(); + if (peekCode() == ',') + bytes.readSkip(1); } /** diff --git a/src/test/java/net/openhft/chronicle/wire/MyTypes.java b/src/test/java/net/openhft/chronicle/wire/MyTypes.java index 90c40b816..db90a6e65 100644 --- a/src/test/java/net/openhft/chronicle/wire/MyTypes.java +++ b/src/test/java/net/openhft/chronicle/wire/MyTypes.java @@ -20,7 +20,7 @@ import org.jetbrains.annotations.NotNull; // Class representing a collection of various data types, extending SelfDescribingMarshallable for serialization support -class MyTypes extends SelfDescribingMarshallable { +public class MyTypes extends SelfDescribingMarshallable { // StringBuilder instance to hold and manipulate string data efficiently final StringBuilder text = new StringBuilder(); diff --git a/src/test/java/net/openhft/chronicle/wire/marshallable/ReadQuotedTest.java b/src/test/java/net/openhft/chronicle/wire/marshallable/ReadQuotedTest.java new file mode 100644 index 000000000..84f1819b8 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/wire/marshallable/ReadQuotedTest.java @@ -0,0 +1,75 @@ +package net.openhft.chronicle.wire.marshallable; + +import net.openhft.chronicle.wire.MyTypes; +import net.openhft.chronicle.wire.WireType; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class ReadQuotedTest { + private final WireType wireType; + + public ReadQuotedTest(WireType wireType) { + this.wireType = wireType; + } + + // Define parameters for this parameterized test + @Parameterized.Parameters(name = "wireType={0}") + public static Collection data() { + return Arrays.asList( + new Object[]{WireType.JSON_ONLY}, + new Object[]{WireType.YAML_ONLY} + ); + } + + static final String EXPECTED = "!net.openhft.chronicle.wire.MyTypes {\n" + + " text: hi,\n" + + " flag: false,\n" + + " b: 1,\n" + + " s: 2,\n" + + " ch: \"3\",\n" + + " i: 4,\n" + + " f: 5.6,\n" + + " d: 7.8,\n" + + " l: 9\n" + + "}\n"; + + @Test + public void testReadSingleQuoted() { + MyTypes mt = wireType.fromString(MyTypes.class, "{" + + " text: 'hi',\n" + + " flag: 'false',\n" + + " b: '1' ,\n" + + " s: '2'\t,\n" + + " ch: '3'\n,\n" + + " i: '4',\n" + + " f: '5.6',\n" + + " d: '7.8',\n" + + " l: '9'\n" + + "}"); + assertEquals(EXPECTED, mt.toString()); + } + + @Test + public void testReadDoubleQuoted() { + MyTypes mt = wireType.fromString(MyTypes.class, "{" + + " text: \"hi\",\n" + + " flag: \"false\",\n" + + " b: \"1\" ,\n" + + " s: \"2\"\t,\n" + + " ch: \"3\"\n,\n" + + " i: \"4\",\n" + + " f: \"5.6\",\n" + + " d: \"7.8\",\n" + + " l: \"9\"\n" + + "}"); + assertEquals(EXPECTED, mt.toString()); + } +} +