Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::ComplexJSON Struct Reference

Custom struct to handle both strings and nested JSON objects. More...

#include <duckdb.hpp>

Collaboration diagram for duckdb::ComplexJSON:

Public Member Functions

 ComplexJSON (const string &str)
 Constructor for string values.
 ComplexJSON ()
 Basic empty constructor.
void AddObjectEntry (const string &key, unique_ptr< ComplexJSON > object)
 Adds entry to the underlying map, also sets the type to OBJECT.
void AddArrayElement (unique_ptr< ComplexJSON > object)
 Adds element to the underlying list, also sets the type to ARRAY.
ComplexJSON & GetObject (const string &key)
 Gets a ComplexJSON object from the map.
ComplexJSON & GetArrayElement (const idx_t &index)
 Gets a ComplexJSON element from the list.
string GetValue (const string &key) const
 Gets a string version of the underlying ComplexJSON object from the map.
string GetValue (const idx_t &index) const
 Gets a string version of the underlying ComplexJSON array from the list.
unordered_map< string, string > Flatten () const
 Flattens this json to a top level key -> nested json.

Static Public Member Functions

static string GetValueRecursive (const ComplexJSON &child)
 Recursive function for GetValue.

Private Attributes

string str_value
 Basic string value, in case this is the last value of a nested json.
unordered_map< string, unique_ptr< ComplexJSON > > obj_value
 If this is a json object a map of key/value.
vector< unique_ptr< ComplexJSON > > arr_value
 If this is a json array a list of values.
ComplexJSONType type
 If this json is an object (i.e., map or not).

Detailed Description

Custom struct to handle both strings and nested JSON objects.

Constructor & Destructor Documentation

◆ ComplexJSON() [1/2]

duckdb::ComplexJSON::ComplexJSON ( const string & str)
explicit

Constructor for string values.

49989 : str_value(str), type(ComplexJSONType::VALUE) {
49990}
ComplexJSONType type
If this json is an object (i.e., map or not).
Definition duckdb.hpp:3728
string str_value
Basic string value, in case this is the last value of a nested json.
Definition duckdb.hpp:3722
Here is the caller graph for this function:

◆ ComplexJSON() [2/2]

duckdb::ComplexJSON::ComplexJSON ( )

Basic empty constructor.

49992 : type(ComplexJSONType::VALUE) {
49993}

Member Function Documentation

◆ AddObjectEntry()

void duckdb::ComplexJSON::AddObjectEntry ( const string & key,
unique_ptr< ComplexJSON > object )

Adds entry to the underlying map, also sets the type to OBJECT.

49995 {
49996 type = ComplexJSONType::OBJECT;
49997 obj_value[key] = std::move(object);
49998}
unordered_map< string, unique_ptr< ComplexJSON > > obj_value
If this is a json object a map of key/value.
Definition duckdb.hpp:3724

◆ AddArrayElement()

void duckdb::ComplexJSON::AddArrayElement ( unique_ptr< ComplexJSON > object)

Adds element to the underlying list, also sets the type to ARRAY.

50000 {
50001 type = ComplexJSONType::ARRAY;
50002 arr_value.push_back(std::move(object));
50003}
vector< unique_ptr< ComplexJSON > > arr_value
If this is a json array a list of values.
Definition duckdb.hpp:3726

◆ GetObject()

ComplexJSON & duckdb::ComplexJSON::GetObject ( const string & key)

Gets a ComplexJSON object from the map.

50005 {
50006 if (type == ComplexJSONType::OBJECT) {
50007 if (obj_value.find(key) == obj_value.end()) {
50008 throw InvalidInputException("Complex JSON Key not found");
50009 }
50010 return *obj_value[key];
50011 }
50012 throw InvalidInputException("ComplexJson is not an object");
50013}
Here is the call graph for this function:

◆ GetArrayElement()

ComplexJSON & duckdb::ComplexJSON::GetArrayElement ( const idx_t & index)

Gets a ComplexJSON element from the list.

50015 {
50016 if (type == ComplexJSONType::ARRAY) {
50017 if (index >= arr_value.size()) {
50018 throw InvalidInputException("Complex JSON array element out of bounds");
50019 }
50020 return *arr_value[index];
50021 }
50022 throw InvalidInputException("ComplexJson is not an array");
50023}
index
Here is the call graph for this function:

◆ GetValue() [1/2]

string duckdb::ComplexJSON::GetValue ( const string & key) const

Gets a string version of the underlying ComplexJSON object from the map.

110044 {
110045 if (type == ComplexJSONType::OBJECT) {
110046 if (obj_value.find(key) != obj_value.end()) {
110047 return GetValueRecursive(*obj_value.at(key));
110048 }
110049 }
110050 // Object either doesn't exist or this is just a string
110051 return "";
110052}
static string GetValueRecursive(const ComplexJSON &child)
Recursive function for GetValue.
Definition duckdb.cpp:110064
Here is the call graph for this function:

◆ GetValue() [2/2]

string duckdb::ComplexJSON::GetValue ( const idx_t & index) const

Gets a string version of the underlying ComplexJSON array from the list.

110054 {
110055 if (type == ComplexJSONType::ARRAY) {
110056 if (index >= arr_value.size()) {
110057 return "";
110058 }
110059 return GetValueRecursive(*arr_value[index]);
110060 }
110061 return "";
110062}
Here is the call graph for this function:

◆ GetValueRecursive()

string duckdb::ComplexJSON::GetValueRecursive ( const ComplexJSON & child)
static

Recursive function for GetValue.

110064 {
110065 if (child.type == ComplexJSONType::OBJECT) {
110066 // We have to construct the nested json
110067 yyjson_mut_doc *doc = yyjson_mut_doc_new(nullptr);
110068 yyjson_mut_val *root = yyjson_mut_obj(doc);
110069 yyjson_mut_doc_set_root(doc, root);
110070 for (const auto &object : child.obj_value) {
110071 auto key = yyjson_mut_strncpy(doc, object.first.c_str(), object.first.size());
110072 auto value_str = GetValueRecursive(*object.second);
110073 auto value = yyjson_mut_strncpy(doc, value_str.c_str(), value_str.size());
110074 yyjson_mut_obj_add(root, key, value);
110075 }
110076 return WriteJsonToString(doc);
110077 } else if (child.type == ComplexJSONType::ARRAY) {
110078 yyjson_mut_doc *doc = yyjson_mut_doc_new(nullptr);
110079 yyjson_mut_val *root = yyjson_mut_arr(doc);
110080 yyjson_mut_doc_set_root(doc, root);
110081 for (const auto &elem : child.arr_value) {
110082 auto value_str = GetValueRecursive(*elem);
110083 auto value = yyjson_mut_strncpy(doc, value_str.c_str(), value_str.size());
110084 yyjson_mut_arr_append(root, value);
110085 }
110086 return WriteJsonToString(doc);
110087 } else {
110088 // simple string we can just write
110089 return child.str_value;
110090 }
110091}
yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, yyjson_mut_val *root)
Definition duckdb.cpp:42425
yyjson_api yyjson_mut_doc * yyjson_mut_doc_new(const yyjson_alc *alc)
Definition duckdb.cpp:599384
yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, yyjson_mut_val *val)
Definition duckdb.cpp:43163
struct yyjson_mut_doc yyjson_mut_doc
Definition duckdb.cpp:37762
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj(yyjson_mut_doc *doc)
Definition duckdb.cpp:43608
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr(yyjson_mut_doc *doc)
Definition duckdb.cpp:42941
yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val)
Definition duckdb.cpp:43762
yyjson_api_inline yyjson_mut_val * yyjson_mut_strncpy(yyjson_mut_doc *doc, const char *str, size_t len)
Definition duckdb.cpp:42824
struct yyjson_mut_val yyjson_mut_val
Definition duckdb.cpp:37769
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Flatten()

unordered_map< string, string > duckdb::ComplexJSON::Flatten ( ) const

Flattens this json to a top level key -> nested json.

50025 {
50026 unordered_map<string, string> result;
50027 for (auto &obj : obj_value) {
50028 result[obj.first] = obj.second->GetValueRecursive(*obj.second);
50029 }
50030 return result;
50031}

The documentation for this struct was generated from the following files: