Skip to content

Commit

Permalink
Update Parsson, add test for big numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
swallez committed Jan 24, 2024
1 parent cc703e5 commit c37aa98
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

package co.elastic.clients.json;

import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.json.jsonb.JsonbJsonpMapper;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import jakarta.json.JsonArray;
import jakarta.json.JsonNumber;
import jakarta.json.bind.spi.JsonbProvider;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import org.eclipse.parsson.JsonProviderImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -46,16 +49,18 @@ public class JsonpMapperTest extends Assertions {

@Test
public void testJsonb() {
JsonpMapper mapper = new JsonbJsonpMapper();
JsonpMapper mapper = new JsonbJsonpMapper(new JsonProviderImpl(), JsonbProvider.provider());
testSerialize(mapper, json);
testDeserialize(mapper, json);
testLargeNumber(mapper);
}

@Test
public void testJackson() {
JacksonJsonpMapper mapper = new JacksonJsonpMapper();
testSerialize(new JacksonJsonpMapper(), json);
testDeserialize(new JacksonJsonpMapper(), json);
testSerialize(mapper, json);
testDeserialize(mapper, json);
testLargeNumber(mapper);
}

@Test
Expand Down Expand Up @@ -149,6 +154,20 @@ private void testDeserialize(JsonpMapper mapper, String json) {
assertNull(child.getChildren());
}

private void testLargeNumber(JsonpMapper mapper) {
String json = "[1e999999]";
JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
parser.next();

JsonArray array = parser.getValue().asJsonArray();

JsonNumber number = array.getJsonNumber(0);

assertTrue(Double.isInfinite(number.doubleValue()));

Assertions.assertThrows(Exception.class, number::bigIntegerValue);
}

public static class SomeClass {
private List<SomeClass> children;
private double doubleValue;
Expand Down

0 comments on commit c37aa98

Please sign in to comment.