Changeset

11880:d30c81b875f2

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
author Kim Alvefur <zash@zash.se>
date Thu, 28 Oct 2021 13:00:24 +0200
parents 11879:b8b889ba8d27
children 11881:2138fc13d53a
files spec/util_dataforms_spec.lua util/dataforms.lua
diffstat 2 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_dataforms_spec.lua	Tue Oct 26 15:41:54 2021 +0200
+++ b/spec/util_dataforms_spec.lua	Thu Oct 28 13:00:24 2021 +0200
@@ -458,6 +458,12 @@
 				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 ()
--- a/util/dataforms.lua	Tue Oct 26 15:41:54 2021 +0200
+++ b/util/dataforms.lua	Thu Oct 28 13:00:24 2021 +0200
@@ -107,8 +107,10 @@
 					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