diff --git a/json/assembly/deserializer.ts b/json/assembly/deserializer.ts index 974385c..103c15f 100644 --- a/json/assembly/deserializer.ts +++ b/json/assembly/deserializer.ts @@ -164,10 +164,18 @@ export class ValueDeserializer extends Deserializer{ } decode_float(): T { - assert(this.currentVal.isFloat, `Expected JSON.Float but found ${this.currentVal.stringify()}`); - return (this.currentVal).valueOf(); + assert( + this.currentVal.isFloat || this.currentVal.isInteger, + `Expected JSON.Float || JSON.Integer but found ${this.currentVal.stringify()}`, + ); + + if (this.currentVal.isFloat) { + return (this.currentVal).valueOf(); + } + return (this.currentVal).valueOf(); } + // We override decode_number, for which we don't need these decode_u8(): u8 { return this.decode_int() } decode_i8(): i8 { return this.decode_int() } diff --git a/json/assembly/tests/jvalue.spec.ts b/json/assembly/tests/jvalue.spec.ts index 5b6af6b..7933563 100644 --- a/json/assembly/tests/jvalue.spec.ts +++ b/json/assembly/tests/jvalue.spec.ts @@ -20,6 +20,7 @@ import { HasConstructorArgs, } from "@serial-as/tests"; import { initMixtureTwo } from "./utils"; +import { Point } from "../../../tests/assembly"; function roundTrip(t:T): void { const encoded = ValueSerializer.encode(t); @@ -57,6 +58,9 @@ describe("JSONSerializer Serializing Types - Two", () => { }); it("should encode/decode floats", () => { + roundTrip(1) + roundTrip(-2) + roundTrip(7.23) roundTrip(10e2) roundTrip(10E2) @@ -69,7 +73,11 @@ describe("JSONSerializer Serializing Types - Two", () => { roundTrip(7.23) }); - it("should encode/decode u128", () => { + it("should decode int as float", () => { + check_decode('{ "x": 3.4, "y": 3 }', { x: 3.4, y: 3.0 }) + }); + + it("should encode/decode u128", () => { let N: u128 = u128.from("100") roundTrip(N); }); diff --git a/tests/assembly/index.ts b/tests/assembly/index.ts index 6063765..72c3da3 100644 --- a/tests/assembly/index.ts +++ b/tests/assembly/index.ts @@ -28,6 +28,12 @@ export class Numbers { public f64: f64 = 0; } +@serializable +export class Point { + public x: f64; + public y: f64; +} + export function init_numbers(N:Numbers):void{ N.u8 = 1; N.u16 = 2;