Skip to content

Commit

Permalink
move to json_base implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Jan 4, 2024
1 parent 838d463 commit 03dc826
Show file tree
Hide file tree
Showing 54 changed files with 691 additions and 686 deletions.
2 changes: 1 addition & 1 deletion CMake/json-download.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ExternalProject_Add(
nlohmann_json
PREFIX .
GIT_REPOSITORY "https://github.com/nlohmann/json.git"
#GIT_TAG v3.11.2
GIT_TAG v3.11.3
GIT_CONFIG advice.detachedHead=false # otherwise we'll get "You are in 'detached HEAD' state..."
SOURCE_DIR "${CMAKE_BINARY_DIR}/third-party/json"
GIT_SHALLOW 1 # No history needed (requires cmake 3.6)
Expand Down
2 changes: 1 addition & 1 deletion common/device-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "device-model.h"

using namespace rs400;
using namespace nlohmann;
using rsutils::json;
using namespace rs2::sw_update;

namespace rs2
Expand Down
5 changes: 2 additions & 3 deletions common/device-model.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.

#pragma once

#include <set>
#include "notifications.h"
#include "realsense-ui-advanced-mode.h"
#include <nlohmann/json.hpp>
#include <rsutils/json.h>
#include "sw-update/dev-updates-profile.h"
#include <rsutils/time/periodic-timer.h>
#include "updates-model.h"
Expand Down Expand Up @@ -426,7 +425,7 @@ namespace rs2
const std::string& error_message);

void load_viewer_configurations(const std::string& json_str);
void save_viewer_configurations(std::ofstream& outfile, nlohmann::json& j);
void save_viewer_configurations(std::ofstream& outfile, rsutils::json& j);
void handle_online_sw_update(
std::shared_ptr< notifications_model > nm,
std::shared_ptr< sw_update::dev_updates_profile::update_profile > update_profile,
Expand Down
8 changes: 4 additions & 4 deletions common/rs-config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <rsutils/json.h>
#include <fstream>

using json = nlohmann::json;
using json = rsutils::json;

using namespace rs2;

Expand All @@ -31,7 +31,7 @@ void config_file::remove(const char* key)

void config_file::reset()
{
_j = nlohmann::json::object();
_j = json::object();
save();
}

Expand All @@ -40,7 +40,7 @@ std::string config_file::get(const char* key, const char* def) const
auto it = _j.find(key);
if (it != _j.end() && it->is_string())
{
return rsutils::json::string_ref( *it );
return it->string_ref();
}
return get_default(key, def);
}
Expand Down Expand Up @@ -105,7 +105,7 @@ void config_file::save()
}

config_file::config_file()
: _j( nlohmann::json::object() )
: _j( rsutils::json::object() )
{
}

Expand Down
4 changes: 2 additions & 2 deletions common/rs-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
#pragma once

#include <nlohmann/json.hpp>
#include <rsutils/json.h>

#include <map>
#include <string>
Expand Down Expand Up @@ -94,6 +94,6 @@ namespace rs2

std::map<std::string, std::string> _defaults;
std::string _filename;
nlohmann::json _j;
rsutils::json _j;
};
}
1 change: 0 additions & 1 deletion common/subdevice-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <unordered_map>
#include <fstream>

#include <nlohmann/json.hpp>
#include "objects-in-frame.h"
#include "processing-block-model.h"

Expand Down
4 changes: 2 additions & 2 deletions common/sw-update/versions-db-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

#include "versions-db-manager.h"
#include <nlohmann/json.hpp>
#include <rsutils/json.h>
#include <rsutils/os/os.h>
#include <rsutils/easylogging/easyloggingpp.h>
#include <fstream>
Expand All @@ -16,7 +16,7 @@ namespace rs2

namespace sw_update
{
using json = nlohmann::json;
using json = rsutils::json;
using namespace http;

query_status_type versions_db_manager::query_versions(const std::string &device_name, component_part_type component, const update_policy_type policy, version& out_version)
Expand Down
34 changes: 10 additions & 24 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,34 @@
#include <rsutils/easylogging/easyloggingpp.h>
#include <rsutils/string/from.h>
#include <rsutils/json.h>
using json = nlohmann::json;

#include <fstream>
#include <rsutils/json-config.h>
using json = rsutils::json;


namespace librealsense {


static nlohmann::json load_settings( nlohmann::json const & context_settings )
static rsutils::json load_settings( rsutils::json const & context_settings )
{
// Allow ignoring of any other settings, global or not!
if( ! rsutils::json::get( context_settings, "inherit", true ) )
if( ! context_settings.nested( "inherit" ).default_value( true ) )
return context_settings;

nlohmann::json config;

// Load the realsense configuration file settings
auto const filename = rsutils::os::get_special_folder( rsutils::os::special_folder::app_data ) + RS2_CONFIG_FILENAME;
std::ifstream f( filename );
if( f.good() )
{
try
{
config = nlohmann::json::parse( f );
}
catch( std::exception const & e )
{
throw std::runtime_error( "failed to load configuration file (" + filename + "): " + std::string( e.what() ) );
}
}
auto config = rsutils::json_config::load_from_file( filename );

config = rsutils::json::load_settings( config, "context", "config-file" );
// Take only the 'context' part of it
config = rsutils::json_config::load_settings( config, "context", "config-file" );

// Patch the given context settings into the configuration
rsutils::json::patch( config, context_settings, "context settings" );
config.override( context_settings, "context settings" );
return config;
}


context::context( json const & settings )
: _settings( load_settings( settings ) ) // global | application | local
, _device_mask( rsutils::json::get< unsigned >( _settings, "device-mask", RS2_PRODUCT_LINE_ANY ) )
, _device_mask( _settings.nested( "device-mask" ).default_value< unsigned >( RS2_PRODUCT_LINE_ANY ) )
{
static bool version_logged = false;
if( ! version_logged )
Expand Down Expand Up @@ -192,7 +178,7 @@ namespace librealsense {


std::shared_ptr< processing_block_interface > context::create_pp_block( std::string const & name,
nlohmann::json const & settings )
rsutils::json const & settings )
{
return rscore_pp_block_factory().create_pp_block( name, settings );
}
Expand Down
6 changes: 3 additions & 3 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ namespace librealsense

class context
{
context( nlohmann::json const & ); // private! use make()
context( rsutils::json const & ); // private! use make()

void create_factories( std::shared_ptr< context > const & sptr );

public:
static std::shared_ptr< context > make( nlohmann::json const & );
static std::shared_ptr< context > make( rsutils::json const & );
static std::shared_ptr< context > make( char const * json_settings );

~context();
Expand Down Expand Up @@ -65,7 +65,7 @@ namespace librealsense
// Create processing blocks given a name and settings.
//
std::shared_ptr< processing_block_interface > create_pp_block( std::string const & name,
nlohmann::json const & settings );
rsutils::json const & settings );

private:
void invoke_devices_changed_callbacks( std::vector< std::shared_ptr< device_info > > const & devices_removed,
Expand Down
4 changes: 2 additions & 2 deletions src/core/pp-block-factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
#pragma once

#include <nlohmann/json_fwd.hpp>
#include <rsutils/json-fwd.h>
#include <string>


Expand Down Expand Up @@ -30,7 +30,7 @@ class pp_block_factory
// The name is case-insensitive.
//
virtual std::shared_ptr< processing_block_interface > create_pp_block( std::string const & name,
nlohmann::json const & settings )
rsutils::json const & settings )
= 0;
};

Expand Down
6 changes: 3 additions & 3 deletions src/dds/rs-dds-depth-sensor-proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ void dds_depth_sensor_proxy::add_no_metadata( frame * const f, streaming_impl &

void dds_depth_sensor_proxy::add_frame_metadata( frame * const f, rsutils::json const & dds_md, streaming_impl & streaming )
{
if( auto du = dds_md.find( metadata_header_key, depth_units_key ) )
if( auto du = dds_md.nested( metadata_header_key, depth_units_key ) )
{
try
{
f->additional_data.depth_units = du.value< float >();
f->additional_data.depth_units = du.get< float >();
}
catch( nlohmann::json::exception const & )
catch( rsutils::json::exception const & )
{
f->additional_data.depth_units = get_depth_scale();
}
Expand Down
20 changes: 10 additions & 10 deletions src/dds/rs-dds-device-proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ dds_device_proxy::dds_device_proxy( std::shared_ptr< const device_info > const &

auto & j = dev->device_info().to_json();
std::string str;
if( rsutils::json::get_ex( j, "serial", &str ) )
if( j.nested( "serial" ).get_ex( str ) )
{
register_info( RS2_CAMERA_INFO_SERIAL_NUMBER, str );
rsutils::json::get_ex( j, "fw-update-id", &str ); // if fails, str will be the serial
j.nested( "fw-update-id" ).get_ex( str ); // if fails, str will be the serial
register_info( RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID, str );
}
else if( rsutils::json::get_ex( j, "fw-update-id", &str ) )
else if( j.nested( "fw-update-id" ).get_ex( str ) )
register_info( RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID, str );
if( rsutils::json::get_ex( j, "fw-version", &str ) )
if( j.nested( "fw-version" ).get_ex( str ) )
register_info( RS2_CAMERA_INFO_FIRMWARE_VERSION, str );
if( rsutils::json::get_ex( j, "product-line", &str ) )
if( j.nested( "product-line" ).get_ex( str ) )
register_info( RS2_CAMERA_INFO_PRODUCT_LINE, str );
register_info( RS2_CAMERA_INFO_CAMERA_LOCKED, rsutils::json::get( j, "locked", true ) ? "YES" : "NO" );
register_info( RS2_CAMERA_INFO_CAMERA_LOCKED, j.nested( "locked" ).default_value( true ) ? "YES" : "NO" );

// Assumes dds_device initialization finished
struct sensor_info
Expand Down Expand Up @@ -298,7 +298,7 @@ dds_device_proxy::dds_device_proxy( std::shared_ptr< const device_info > const &
_metadata_subscription = _dds_dev->on_metadata_available(
[this]( std::shared_ptr< const rsutils::json > const & dds_md )
{
std::string const & stream_name = rsutils::json::nested( *dds_md, stream_name_key ).string_ref();
std::string const & stream_name = dds_md->nested( stream_name_key ).string_ref();
auto it = _stream_name_to_owning_sensor.find( stream_name );
if( it != _stream_name_to_owning_sensor.end() )
it->second->handle_new_metadata( stream_name, dds_md );
Expand Down Expand Up @@ -354,7 +354,7 @@ dds_device_proxy::dds_device_proxy( std::shared_ptr< const device_info > const &
// Depth & IR matched by frame-number, time-stamp-matched to color.
// Motion streams will not get synced.
rs2_matchers matcher = RS2_MATCHER_DLR_C;
if( auto matcher_j = _dds_dev->participant()->settings().find( "device", "matcher" ) )
if( auto matcher_j = _dds_dev->participant()->settings().nested( "device", "matcher" ) )
{
if( ! matcher_j.is_string() || ! try_parse( matcher_j.string_ref(), matcher ) )
LOG_WARNING( "Invalid 'device/matcher' value " << matcher_j );
Expand Down Expand Up @@ -520,7 +520,7 @@ std::vector< uint8_t > dds_device_proxy::send_receive_raw_data( const std::vecto
rsutils::json reply;
_dds_dev->send_control( control, &reply );
rsutils::string::hexarray data;
if( ! rsutils::json::get_ex( reply, "data", &data ) )
if( ! reply.nested( "data" ).get_ex( data ) )
throw std::runtime_error( "Failed HWM: missing 'data' in reply" );
return data.detach();
}
Expand All @@ -546,7 +546,7 @@ std::vector< uint8_t > dds_device_proxy::build_command( uint32_t opcode,
{ "build-command", true } } );
rsutils::json reply;
_dds_dev->send_control( control, &reply );
if( ! rsutils::json::get_ex( reply, "data", &hexdata ) )
if( ! reply.nested( "data" ).get_ex( hexdata ) )
throw std::runtime_error( "Failed HWM: missing 'data' in reply" );
return hexdata.detach();
}
Expand Down
19 changes: 8 additions & 11 deletions src/dds/rs-dds-sensor-proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ void dds_sensor_proxy::handle_new_metadata( std::string const & stream_name,
auto it = _streaming_by_name.find( stream_name );
if( it != _streaming_by_name.end() )
{
if( auto timestamp = rsutils::json::nested( *dds_md, metadata_header_key, timestamp_key ) )
it->second.syncer.enqueue_metadata( timestamp.value< realdds::dds_nsec >(), dds_md );
if( auto timestamp = dds_md->nested( metadata_header_key, timestamp_key ) )
it->second.syncer.enqueue_metadata( timestamp.get< realdds::dds_nsec >(), dds_md );
else
throw std::runtime_error( "missing metadata header/timestamp" );
}
Expand All @@ -369,14 +369,14 @@ void dds_sensor_proxy::add_frame_metadata( frame * const f,
rsutils::json const & dds_md,
streaming_impl & streaming )
{
auto md_header = dds_md.find( metadata_header_key );
auto md = dds_md.find( metadata_key );
auto md_header = dds_md.nested( metadata_header_key );
auto md = dds_md.nested( metadata_key );

// A frame number is "optional". If the server supplies it, we try to use it for the simple fact that,
// otherwise, we have no way of detecting drops without some advanced heuristic tracking the FPS and
// timestamps. If not supplied, we use an increasing counter.
// Note that if we have no metadata, we have no frame-numbers! So we need a way of generating them
if( rsutils::json::get_ex( md_header, frame_number_key, &f->additional_data.frame_number ) )
if( md_header.nested( frame_number_key ).get_ex( f->additional_data.frame_number ) )
{
f->additional_data.last_frame_number = streaming.last_frame_number.exchange( f->additional_data.frame_number );
if( f->additional_data.frame_number != f->additional_data.last_frame_number + 1
Expand All @@ -396,7 +396,7 @@ void dds_sensor_proxy::add_frame_metadata( frame * const f,
// purposes, so we ignore here. The domain is optional, and really only rs-dds-adapter communicates it
// because the source is librealsense...
f->additional_data.timestamp;
rsutils::json::get_ex( md_header, timestamp_domain_key, &f->additional_data.timestamp_domain );
md_header.nested( timestamp_domain_key ).get_ex( f->additional_data.timestamp_domain );

if( ! md.empty() )
{
Expand All @@ -408,11 +408,8 @@ void dds_sensor_proxy::add_frame_metadata( frame * const f,
std::string const & keystr = librealsense::get_string( key );
try
{
if( auto value_j = md.find( keystr ) )
{
if( value_j.is_number_integer() )
metadata[key] = { true, value_j.value< rs2_metadata_type >() };
}
if( auto value_j = md.nested( keystr, &rsutils::json::is_number_integer ) )
metadata[key] = { true, value_j.get< rs2_metadata_type >() };
}
catch( rsutils::json::exception const & )
{
Expand Down
10 changes: 5 additions & 5 deletions src/dds/rsdds-device-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ static std::mutex domain_context_by_id_mutex;
rsdds_device_factory::rsdds_device_factory( std::shared_ptr< context > const & ctx, callback && cb )
: super( ctx )
{
auto dds_settings = ctx->get_settings().find( std::string( "dds", 3 ) );
auto dds_settings = ctx->get_settings().nested( std::string( "dds", 3 ) );
if( ! dds_settings.exists()
|| dds_settings.is_object() && dds_settings.find( std::string( "enabled", 7 ) ).default_value( true ) )
|| dds_settings.is_object() && dds_settings.nested( std::string( "enabled", 7 ) ).default_value( true ) )
{
auto domain_id = dds_settings.find( std::string( "domain", 6 ) ).default_value< realdds::dds_domain_id >( 0 );
auto participant_name_j = dds_settings.find( std::string( "participant", 11 ) );
auto domain_id = dds_settings.nested( std::string( "domain", 6 ) ).default_value< realdds::dds_domain_id >( 0 );
auto participant_name_j = dds_settings.nested( std::string( "participant", 11 ) );
auto participant_name = participant_name_j.default_value( rsutils::os::executable_name() );

std::lock_guard< std::mutex > lock( domain_context_by_id_mutex );
Expand Down Expand Up @@ -145,7 +145,7 @@ std::vector< std::shared_ptr< device_info > > rsdds_device_factory::query_device
return true;
}
std::string product_line;
if( rsutils::json::get_ex( dev->device_info().to_json(), "product-line", &product_line ) )
if( dev->device_info().to_json().nested( "product-line" ).get_ex( product_line ) )
{
if( product_line == "D400" )
{
Expand Down
2 changes: 1 addition & 1 deletion src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ format_conversion device::get_format_conversion() const
return format_conversion::full;
std::string const format_conversion( "format-conversion", 17 );
std::string const full( "full", 4 );
auto const value = rsutils::json::get( context->get_settings(), format_conversion, full );
auto const value = context->get_settings().nested( format_conversion ).default_value( full );
if( value == full )
return format_conversion::full;
if( value == "basic" )
Expand Down
Loading

0 comments on commit 03dc826

Please sign in to comment.