Skip to content

Commit

Permalink
standardize on rsutils::json
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Jan 4, 2024
1 parent 950e776 commit 5a1d195
Show file tree
Hide file tree
Showing 75 changed files with 977 additions and 889 deletions.
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
35 changes: 11 additions & 24 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +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
std::ifstream f( rsutils::os::get_special_folder( rsutils::os::special_folder::app_data ) + RS2_CONFIG_FILENAME );
if( f.good() )
{
try
{
config = nlohmann::json::parse( f );
}
catch( std::exception const & e )
{
throw std::runtime_error( "failed to load configuration file: " + std::string( e.what() ) );
}
}
auto const filename = rsutils::os::get_special_folder( rsutils::os::special_folder::app_data ) + RS2_CONFIG_FILENAME;
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 @@ -191,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
13 changes: 6 additions & 7 deletions src/context.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.

#pragma once

#include <rsutils/signal.h>
#include <nlohmann/json.hpp>
#include <rsutils/json.h>
#include <vector>
#include <map>

Expand All @@ -18,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 @@ -61,12 +60,12 @@ namespace librealsense
void add_device( std::shared_ptr< device_info > const & );
void remove_device( std::shared_ptr< device_info > const & );

const nlohmann::json & get_settings() const { return _settings; }
const rsutils::json & get_settings() const { return _settings; }

// 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 All @@ -78,7 +77,7 @@ namespace librealsense
std::vector< std::shared_ptr< device_info > > const & /*added*/ >
_devices_changed;

nlohmann::json _settings; // Save operation settings
rsutils::json _settings; // Save operation settings
unsigned const _device_mask;

std::vector< std::shared_ptr< device_factory > > _factories;
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
8 changes: 4 additions & 4 deletions src/dds/rs-dds-depth-sensor-proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ void dds_depth_sensor_proxy::add_no_metadata( frame * const f, streaming_impl &
}


void dds_depth_sensor_proxy::add_frame_metadata( frame * const f, nlohmann::json const & dds_md, streaming_impl & streaming )
void dds_depth_sensor_proxy::add_frame_metadata( frame * const f, rsutils::json const & dds_md, streaming_impl & streaming )
{
if( auto du = rsutils::json::nested( dds_md, metadata_header_key, depth_units_key ) )
if( auto du = dds_md.nested( metadata_header_key, depth_units_key ) )
{
try
{
f->additional_data.depth_units = rsutils::json::value< float >( du );
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
3 changes: 1 addition & 2 deletions src/dds/rs-dds-depth-sensor-proxy.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.

#pragma once

#include "rs-dds-sensor-proxy.h"
Expand Down Expand Up @@ -29,7 +28,7 @@ class dds_depth_sensor_proxy

protected:
void add_no_metadata( frame *, streaming_impl & ) override;
void add_frame_metadata( frame *, nlohmann::json const & md, streaming_impl & ) override;
void add_frame_metadata( frame *, rsutils::json const & md, streaming_impl & ) override;
};


Expand Down
50 changes: 25 additions & 25 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 @@ -296,9 +296,9 @@ dds_device_proxy::dds_device_proxy( std::shared_ptr< const device_info > const &
if( _dds_dev->supports_metadata() )
{
_metadata_subscription = _dds_dev->on_metadata_available(
[this]( std::shared_ptr< const nlohmann::json > const & dds_md )
[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,9 +354,9 @@ 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 = rsutils::json::nested( _dds_dev->participant()->settings(), "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 ) )
if( ! matcher_j.is_string() || ! try_parse( matcher_j.string_ref(), matcher ) )
LOG_WARNING( "Invalid 'device/matcher' value " << matcher_j );
}
set_matcher_type( matcher );
Expand Down Expand Up @@ -506,8 +506,8 @@ void dds_device_proxy::tag_profiles( stream_profiles profiles ) const

void dds_device_proxy::hardware_reset()
{
nlohmann::json control = nlohmann::json::object( { { "id", "hw-reset" } } );
nlohmann::json reply;
rsutils::json control = rsutils::json::object( { { "id", "hw-reset" } } );
rsutils::json reply;
_dds_dev->send_control( control, &reply );
}

Expand All @@ -516,11 +516,11 @@ std::vector< uint8_t > dds_device_proxy::send_receive_raw_data( const std::vecto
{
// debug_interface function
auto hexdata = rsutils::string::hexarray::to_string( input );
nlohmann::json control = nlohmann::json::object( { { "id", "hwm" }, { "data", hexdata } } );
nlohmann::json reply;
rsutils::json control = rsutils::json::object( { { "id", "hwm" }, { "data", hexdata } } );
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 @@ -536,17 +536,17 @@ std::vector< uint8_t > dds_device_proxy::build_command( uint32_t opcode,
{
// debug_interface function
rsutils::string::hexarray hexdata( std::vector< uint8_t >( data, data + dataLength ) );
nlohmann::json control = nlohmann::json::object( { { "id", "hwm" },
{ "data", hexdata },
{ "opcode", opcode },
{ "param1", param1 },
{ "param2", param2 },
{ "param3", param3 },
{ "param4", param4 },
{ "build-command", true } } );
nlohmann::json reply;
rsutils::json control = rsutils::json::object( { { "id", "hwm" },
{ "data", hexdata },
{ "opcode", opcode },
{ "param1", param1 },
{ "param2", param2 },
{ "param3", param3 },
{ "param4", param4 },
{ "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
Loading

0 comments on commit 5a1d195

Please sign in to comment.