mchades commented on code in PR #12859:
URL: https://github.com/apache/gravitino/pull/12859#discussion_r4141424907


##########
clients/client-python/gravitino/dto/semantic/json_serdes/semantic_json_mixin.py:
##########
@@ -0,0 +1,54 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Lossless JSON text conversion for Semantic Model DTOs."""
+
+import json
+from decimal import Decimal
+
+import simplejson
+from dataclasses_json import DataClassJsonMixin
+
+
+class SemanticJsonMixin(DataClassJsonMixin):
+    """JSON text conversion preserving numbers in semantic AI-context 
properties.
+
+    Dict conversion leaves Decimal values intact. The standard dataclasses-json
+    encoder turns them into strings, so use a decimal-aware encoder at the JSON
+    boundary. Decode fractional numbers as Decimal before DTO conversion to 
avoid
+    rounding nested additional properties through binary floats.
+    """
+
+    def to_json(self, **kwargs) -> str:
+        """Serialize to JSON, emitting Decimal values as exact JSON numbers.
+
+        Keyword arguments are forwarded to simplejson.dumps, including 
formatting
+        options such as indent and sort_keys.
+        """
+        return simplejson.dumps(self.to_dict(), use_decimal=True, **kwargs)

Review Comment:
   [P2] Preserve decimal types for whole-number values
   
   `simplejson` emits `Decimal("1")` (also `Decimal("0")` and `Decimal("-1")`) 
as an integer JSON token. `from_json()` then restores it as `int`, since 
`parse_float=Decimal` does not apply to integer tokens. The API distinguishes 
integers from decimals, so a valid definition no longer equals its 
round-tripped value and dictionary lookup by that definition fails. Please 
retain a decimal/exponent token for these values and add a round-trip 
regression, including nested additional properties.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to