Skip to content

Commit

Permalink
Consider long next to int when parsing integer numbers
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
schulzp authored and ok2c committed May 15, 2024
1 parent e899aae commit 2260ba4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ public void value(int value) {
}
}

@Override
public void value(long value) {
if (currentObject instanceof ObjectNode) {
((ObjectNode) currentObject).put(currentField, value);
} else if (currentObject instanceof ArrayNode) {
((ArrayNode) currentObject).add(value);
} else {
throw new IllegalStateException("Unexpected node class: " + currentObject.getClass());
}
}

@Override
public void value(double value) {
if (currentObject instanceof ObjectNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public interface JsonTokenEventHandler {
*/
void value(int value);

/**
* Triggered to signal occurrence of a long value.
*/
void value(long value);

/**
* Triggered to signal occurrence of an double precision value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ public void accept(int tokenId, JsonParser jsonParser) throws IOException {
eventHandler.value(jsonParser.getText());
break;
case JsonTokenId.ID_NUMBER_INT:
eventHandler.value(jsonParser.getIntValue());
final JsonParser.NumberType numberType = jsonParser.getNumberType();
final Number numberValue = jsonParser.getNumberValue();
if (numberType == JsonParser.NumberType.LONG) {
eventHandler.value(numberValue.longValue());
} else {
eventHandler.value(numberValue.intValue());
}
break;
case JsonTokenId.ID_NUMBER_FLOAT:
eventHandler.value(jsonParser.getDoubleValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public void testJsonNodeAssembly() throws Exception {
.add(2)
.add(2.2)
.add(JsonNodeFactory.instance.objectNode().put("name2", "value2"));
expectedObject2.addArray().add(JsonNodeFactory.instance.objectNode().put("long", 2153599188L));

Assertions.assertThat(jsonNode2).isEqualTo((expectedObject2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public void testTokenizer() throws Exception {
JsonTokenId.ID_STRING,
JsonTokenId.ID_END_OBJECT,
JsonTokenId.ID_END_ARRAY,
JsonTokenId.ID_START_ARRAY,
JsonTokenId.ID_START_OBJECT,
JsonTokenId.ID_FIELD_NAME,
JsonTokenId.ID_NUMBER_INT,
JsonTokenId.ID_END_OBJECT,
JsonTokenId.ID_END_ARRAY,
JsonTokenId.ID_END_ARRAY,
JsonTokenId.ID_NO_TOKEN
);
Expand Down Expand Up @@ -339,7 +345,7 @@ public void testAsyncTokenizer() throws Exception {
Assertions.assertThat(resource2).isNotNull();

URL resource3 = getClass().getResource("/sample3.json");
Assertions.assertThat(resource2).isNotNull();
Assertions.assertThat(resource3).isNotNull();

for (int bufSize : new int[]{2048, 1024, 256, 32, 16, 8}) {
List<Integer> tokens1 = new ArrayList<>();
Expand Down Expand Up @@ -430,6 +436,12 @@ public void testAsyncTokenizer() throws Exception {
JsonTokenId.ID_STRING,
JsonTokenId.ID_END_OBJECT,
JsonTokenId.ID_END_ARRAY,
JsonTokenId.ID_START_ARRAY,
JsonTokenId.ID_START_OBJECT,
JsonTokenId.ID_FIELD_NAME,
JsonTokenId.ID_NUMBER_INT,
JsonTokenId.ID_END_OBJECT,
JsonTokenId.ID_END_ARRAY,
JsonTokenId.ID_END_ARRAY,
JsonTokenId.ID_NO_TOKEN
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public void value(int value) {
System.out.print(value + "/");
}

@Override
public void value(long value) {
System.out.print(value + "/");
}

@Override
public void value(double value) {
System.out.print(value + "/");
Expand Down
5 changes: 5 additions & 0 deletions hc5-async-json/src/test/resources/sample2.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@
{
"name2": "value2"
}
],
[
{
"long": 2153599188
}
]
]

0 comments on commit 2260ba4

Please sign in to comment.