Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONWire and YamlWire incorrectly parse numbers in single/double ques fixes #971 #972

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/net/openhft/chronicle/wire/JSONWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/net/openhft/chronicle/wire/TextWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -1944,11 +1944,13 @@ public <T> WireIn int32(@NotNull T t, @NotNull ObjIntConsumer<T> 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':
Expand All @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/openhft/chronicle/wire/MyTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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());
}
}