Fix wrong json conversion from numbers

This commit is contained in:
DBotThePony 2024-04-22 21:16:56 +07:00
parent dfcc59d218
commit 3bb7e50315
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 11 additions and 8 deletions

View File

@ -172,8 +172,12 @@ public final class LuaFormat {
private static ByteString finiteDoubleToByteString(double f) { private static ByteString finiteDoubleToByteString(double f) {
// f assumed not to be NaN or infinite // f assumed not to be NaN or infinite
// TODO: check precision used in Lua
// TODO: don't go via java.lang.String // Lua 5.2 compat
if (Math.abs(f) % 1.0 <= 0.000000001) {
return ByteString.of(Long.toString((long) f));
}
String s = Double.toString(f).toLowerCase(); String s = Double.toString(f).toLowerCase();
return ByteString.of(s); return ByteString.of(s);
} }

View File

@ -18,25 +18,23 @@ import org.classdump.luna.Table
import org.classdump.luna.TableFactory import org.classdump.luna.TableFactory
import org.classdump.luna.runtime.ExecutionContext import org.classdump.luna.runtime.ExecutionContext
import ru.dbotthepony.kommons.gson.contains import ru.dbotthepony.kommons.gson.contains
import ru.dbotthepony.kommons.util.KOptional
import ru.dbotthepony.kstarbound.lua.StateMachine
import ru.dbotthepony.kstarbound.lua.from
import ru.dbotthepony.kommons.gson.get import ru.dbotthepony.kommons.gson.get
import ru.dbotthepony.kommons.gson.set
import ru.dbotthepony.kommons.gson.value import ru.dbotthepony.kommons.gson.value
import ru.dbotthepony.kommons.io.StreamCodec import ru.dbotthepony.kommons.io.StreamCodec
import ru.dbotthepony.kommons.io.readVarLong import ru.dbotthepony.kommons.io.readVarLong
import ru.dbotthepony.kommons.io.writeBinaryString import ru.dbotthepony.kommons.io.writeBinaryString
import ru.dbotthepony.kommons.io.writeVarLong import ru.dbotthepony.kommons.io.writeVarLong
import ru.dbotthepony.kommons.util.KOptional
import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.Starbound
import ru.dbotthepony.kstarbound.VersionRegistry import ru.dbotthepony.kstarbound.VersionRegistry
import ru.dbotthepony.kstarbound.io.readInternedString import ru.dbotthepony.kstarbound.io.readInternedString
import ru.dbotthepony.kstarbound.item.ItemRegistry import ru.dbotthepony.kstarbound.item.ItemRegistry
import ru.dbotthepony.kstarbound.item.ItemStack import ru.dbotthepony.kstarbound.item.ItemStack
import ru.dbotthepony.kstarbound.json.mergeJson
import ru.dbotthepony.kstarbound.json.readJsonElement import ru.dbotthepony.kstarbound.json.readJsonElement
import ru.dbotthepony.kstarbound.json.writeJsonElement import ru.dbotthepony.kstarbound.json.writeJsonElement
import ru.dbotthepony.kstarbound.lua.LuaEnvironment import ru.dbotthepony.kstarbound.lua.LuaEnvironment
import ru.dbotthepony.kstarbound.lua.StateMachine
import ru.dbotthepony.kstarbound.lua.from
import ru.dbotthepony.kstarbound.lua.get import ru.dbotthepony.kstarbound.lua.get
import ru.dbotthepony.kstarbound.lua.indexNoYield import ru.dbotthepony.kstarbound.lua.indexNoYield
import ru.dbotthepony.kstarbound.lua.toJson import ru.dbotthepony.kstarbound.lua.toJson

View File

@ -9,6 +9,7 @@ import ru.dbotthepony.kommons.io.writeBinaryString
import ru.dbotthepony.kommons.io.writeSignedVarLong import ru.dbotthepony.kommons.io.writeSignedVarLong
import ru.dbotthepony.kommons.io.writeVarInt import ru.dbotthepony.kommons.io.writeVarInt
import java.io.DataOutputStream import java.io.DataOutputStream
import kotlin.math.absoluteValue
fun DataOutputStream.writeJsonElement(value: JsonElement) { fun DataOutputStream.writeJsonElement(value: JsonElement) {
when (value) { when (value) {

View File

@ -187,7 +187,7 @@ fun TableFactory.from(value: JsonElement?): Any? {
when (value) { when (value) {
is JsonPrimitive -> { is JsonPrimitive -> {
if (value.isNumber) { if (value.isNumber) {
return value.asDouble return value.asNumber
} else if (value.isString) { } else if (value.isString) {
return ByteString.of(value.asString) return ByteString.of(value.asString)
} else if (value.isBoolean) { } else if (value.isBoolean) {