aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-10-28 13:00:24 +0200
committerKim Alvefur <zash@zash.se>2021-10-28 13:00:24 +0200
commit1a0be02fe85a105c48b82aac82c6d082a08f25b0 (patch)
tree888de051608f03aa1d5a346abc6a218df9409d33
parent9a080dc12e5692dcfceaf427a33013085e6f4b72 (diff)
downloadprosody-1a0be02fe85a105c48b82aac82c6d082a08f25b0.tar.gz
prosody-1a0be02fe85a105c48b82aac82c6d082a08f25b0.zip
util.dataforms: Ensure larger integers are serialized as such
Assumes that most number fields are integers, as most numeric types listed in XEP-0122 are, as are all such fields in Prosody as of this. Otherwise %g produces something like 1.1259e+15
-rw-r--r--spec/util_dataforms_spec.lua6
-rw-r--r--util/dataforms.lua4
2 files changed, 9 insertions, 1 deletions
diff --git a/spec/util_dataforms_spec.lua b/spec/util_dataforms_spec.lua
index 426dce33..e0a9bb9b 100644
--- a/spec/util_dataforms_spec.lua
+++ b/spec/util_dataforms_spec.lua
@@ -458,6 +458,12 @@ describe("util.dataforms", function ()
assert.table(e);
assert.string(e.number);
end);
+
+ it("serializes largeer ints okay", function ()
+ local x = f:form{number=1125899906842624}
+ assert.equal("1125899906842624", x:find("field/value#"))
+ end);
+
end)
describe("datetime", function ()
diff --git a/util/dataforms.lua b/util/dataforms.lua
index 6bd44bdc..4daebbcb 100644
--- a/util/dataforms.lua
+++ b/util/dataforms.lua
@@ -107,8 +107,10 @@ function form_t.form(layout, data, formtype)
value = datetime.datetime(value);
elseif field_type == "boolean" then
value = value ~= 0;
+ elseif field.datatype == "xs:double" or field.datatype == "xs:decimal" then
+ value = ("%f"):format(value);
else
- value = ("%g"):format(value);
+ value = ("%d"):format(value);
end
end
-- Add value, depending on type