Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.ai.util.json;

import java.lang.reflect.Type;
import java.math.BigDecimal;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -128,13 +129,15 @@ else if (javaType == Byte.class) {
return Byte.parseByte(value.toString());
}
else if (javaType == Integer.class) {
return Integer.parseInt(value.toString());
BigDecimal bigDecimal = new BigDecimal(value.toString());
return bigDecimal.intValueExact();
}
else if (javaType == Short.class) {
return Short.parseShort(value.toString());
}
else if (javaType == Long.class) {
return Long.parseLong(value.toString());
BigDecimal bigDecimal = new BigDecimal(value.toString());
return bigDecimal.longValueExact();
}
else if (javaType == Double.class) {
return Double.parseDouble(value.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ var record = new TestRecord("John", 30);
assertThat(value).isEqualTo(new TestRecord("John", 30));
}

@Test
void fromScientificNotationToInteger() {
var value = JsonParser.toTypedObject("1.5E7", Integer.class);
assertThat(value).isInstanceOf(Integer.class);
assertThat(value).isEqualTo(15_000_000);
}

@Test
void fromScientificNotationToLong() {
var value = JsonParser.toTypedObject("1.5E12", Long.class);
assertThat(value).isInstanceOf(Long.class);
assertThat(value).isEqualTo(1_500_000_000_000L);
}

record TestRecord(String name, Integer age) {
}

Expand Down