laserninja commented on code in PR #12859: URL: https://github.com/apache/gravitino/pull/12859#discussion_r4148256408
########## 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: Fixed in def1a9be0. Before JSON encoding, whole-number Decimals with exponent zero receive a fractional zero, so they serialize as decimal tokens and restore as Decimal. This is recursive and does not mutate stored values or depend on decimal-context precision. The regression covers 1, 0, -1, negative zero, large values, nested properties, and definition/model round trips, including equality, hashes, and dictionary lookups. Integer and boolean types remain unchanged. All 1,163 unit tests pass; Black and Pylint pass. -- 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]
