Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions third-party/rsutils/include/rsutils/py/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,24 @@ inline rsutils::json py_to_json( const py::handle & obj )

if( py::isinstance< py::int_ >( obj ) )
{
// NOTE: JSON RFC 8259 section 6 describes number formats, without 'signed' vs 'unsigned' distinctions:
// https://json.nlohmann.me/features/types/number_handling/
// When parsing text, Nlohmann's JSON chooses 'unsigned' by default! See lexer.hpp (scan_number()).
// So we match here (and diverge from the default pybind11_json handling!):
try
{
rsutils::json::number_integer_t s = obj.cast< rsutils::json::number_integer_t >();
if( py::int_( s ).equal( obj ) )
return s;
rsutils::json::number_unsigned_t u = obj.cast< rsutils::json::number_unsigned_t >();
if( py::int_( u ).equal( obj ) )
return u;
}
catch( ... ) {}
try
{
rsutils::json::number_unsigned_t u = obj.cast< rsutils::json::number_unsigned_t >();
if( py::int_( u ).equal( obj ) )
return u;
rsutils::json::number_integer_t s = obj.cast< rsutils::json::number_integer_t >();
if( py::int_( s ).equal( obj ) )
return s;
}
catch( ... ) { }
catch( ... ) {}
throw std::runtime_error( "py_to_json received out-of-range for both number_integer_t and number_unsigned_t: "
+ py::repr( obj ).cast< std::string >() );
}
Expand Down