Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voxgig Struct - C++ Implementation #5

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
349fdb1
Java Utility Implementation
BeAllAround Feb 25, 2025
2d6cd9a
add Runner, org.json.*
BeAllAround Feb 26, 2025
fe5c333
refine Runner.java with closures
BeAllAround Feb 26, 2025
b354d6e
add gson
BeAllAround Feb 27, 2025
f95b6bc
remove org.json.*
BeAllAround Feb 27, 2025
3cf9159
refine
BeAllAround Feb 27, 2025
878b74c
C++ schema
BeAllAround Feb 28, 2025
8ecd6b8
update
BeAllAround Feb 28, 2025
e8b8027
Merge branch 'voxgig:main' into main
BeAllAround Mar 3, 2025
8e1038f
Runner and Utility: C++ Implementation
BeAllAround Mar 3, 2025
b543dd1
C++ refactor
BeAllAround Mar 4, 2025
261b18f
refine, add getprop
BeAllAround Mar 4, 2025
7464715
update library overview
BeAllAround Mar 4, 2025
7ecd39d
add getprop and fix the runset to take JsonFunction instead of pointe…
BeAllAround Mar 4, 2025
3202854
refine: fix value default
BeAllAround Mar 4, 2025
fd490ef
namespace VoxgigStruct
BeAllAround Mar 4, 2025
408be8d
update
BeAllAround Mar 4, 2025
eaf3e1b
validate_int, haskey
BeAllAround Mar 4, 2025
e632eb4
add minor getprop edge case for C++ implementation
BeAllAround Mar 4, 2025
0ce5b74
add remaining functions to the Utility Struct
BeAllAround Mar 4, 2025
e2c3b0e
build
BeAllAround Mar 4, 2025
bfcb136
refine
BeAllAround Mar 5, 2025
e10d19f
custom struct serializer
BeAllAround Mar 5, 2025
cb182f3
json items(...)
BeAllAround Mar 5, 2025
7c684af
introduce <regex>, and add joiurl
BeAllAround Mar 5, 2025
205d83f
update library_overview
BeAllAround Mar 6, 2025
8c87801
update library_overview
BeAllAround Mar 6, 2025
bb2dd23
implement stringify
BeAllAround Mar 6, 2025
d96bc31
add comment
BeAllAround Mar 6, 2025
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@ dist
__pycache__

package-lock.json


*.class
*.out

*.swp

1 change: 1 addition & 0 deletions build/test/minor.jsonic
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ getprop: {
{ in: { val: [], key: 'x', alt: false }, out: false }
{ in: { val: ['a'], key: 0, alt:{E:[77]} }, out: 'a' }
{ in: { val: ['a'], key: '0', alt:[{F:66}] }, out: 'a' }
{ in: { val: ['a'], key: '0a', alt:false }, out: false }
{ in: { val: ['a'], key: 'x', alt:[{G:551},{G:552}] }, out:[{G:551},{G:552}] }
{ in: { val: [{x:11},{x:22}], key: 1, alt:{H:[441,442,443]} }, out: {x:22} }
{ in: { val: [[111],[222]], key: 1, alt:[[]] }, out: [222] }
Expand Down
10 changes: 10 additions & 0 deletions build/test/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,16 @@
},
"out": "a"
},
{
"in": {
"val": [
"a"
],
"key": "0a",
"alt": false
},
"out": false
},
{
"in": {
"val": [
Expand Down
5 changes: 5 additions & 0 deletions cpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
compile_and_run_tests:
g++ tests/test_voxgig_struct.cpp -Werror --std=c++11 -I ./src -I ./tests -I ~/Project/json/include -o out.out && ./out.out

check_leak:
valgrind --leak-check=full --show-leak-kinds=all ./out.out
319 changes: 319 additions & 0 deletions cpp/overview/library_overview.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
#include <iostream>
#include <sstream>
#include <vector>

#include <nlohmann/json.hpp>


using json = nlohmann::json;

using JsonFunction = std::function<json(std::vector<json>)>;
using function_pointer = json(*)(std::vector<json>);

// g++ library_overview.cpp --std=c++11 -o out.out -I ~/Project/json/include && ./out.out


struct address
{
std::string m_val;
address(std::string rhs = "") : m_val(std::move(rhs)) {}
};

struct function_wrapper
{
JsonFunction function;

function_wrapper(JsonFunction function) {
this->function = function;
}

std::string to_string() const {
std::stringstream ss;

// std::cout << (void*)(this->function).target() << std::endl;
// ss << typeid(function).name();
ss << &function;

return ss.str();

}

};

static void to_json(nlohmann::json& j, const address& a)
{
j = a.m_val;
}

static void to_json(nlohmann::json& j, const function_wrapper& function)
{
j = function.to_string();
}

/*
template <typename BasicJsonType>
static void from_json(BasicJsonType& j, const JsonFunction& function)
{
}
*/


json d(std::vector<json> args) {
return true;
}

int main() {

{
JsonFunction dd = d;
function_wrapper f = dd;


json obj = json(f);

std::cout << obj << std::endl;

}

{
json d = "AAA";

json a = nullptr;

json b = "";

std::cout << (a == nullptr) << std::endl;
std::cout << (b == "") << std::endl;

if(d.is_string()) {
std::string conv = d.get<std::string>();

std::cout << conv << std::endl;
}
}

// vector conversion
{
json obj = json::parse("[1, 2, 3, \"A\"]");
// This conversion takes the same amount of auxiliary space both for std::move and copy operations
std::vector<json> obj1 = obj;

std::cout << obj << std::endl;
// std::cout << obj1 << std::endl;

}

{
json obj = json::parse("{}");

// non-existent key for this lookup creates an entry in the memory so contains or better, "find" is recommended.
std::cout << obj["a"] << std::endl;
std::cout << obj.contains("a") << std::endl; // True :)

}

{
// Non-string key lookup test
json a = json::parse("{\"1\": 2}");

json key = "1"; // fails
key = 1;
// a[1] fails
std::cout << a["1"] << std::endl;
std::cout << a[key.dump()] << std::endl;


json arr = json::parse("[ \"a\" ]");

// TODO: Add a testcase for this
key = "0a1"; // THIS WILL CAUSE PROBLEMS

std::cout << arr[std::stoi(key.get<std::string>())] << std::endl;


std::cout << json::parse(R"({"b": 1, "a": 2})") << std::endl;
std::cout << json::parse(R"(["b", "a"])") << std::endl;

/*
key = "0";

key.get<int>();
*/

}

{
assert(json::parse(R"({"a": 1})") == json::parse(R"({"a": 1.0})"));
assert(json::parse(R"({"a": 1})") != json::parse(R"({"a": 1.1})"));
assert(json::parse(R"({"a": 1})") == json::parse(R"({"a": 1})"));

}

{
json value = 1;

value = value.dump();


std::string c = value.get<std::string>();

assert(c == "1");
}

/*
{
// Provider check
Provider provider;
hash_table<std::string, Utility> utility = provider.utility();

Utility _struct = utility["struct"];

std::cout << "_struct key: " << _struct["isnode"]({ json::array() }) << std::endl;



}
*/

/*
// Equality Checks
{
// Shallow Equal
json obj1 = json::parse(R"({"a": 1, "b": 2})");
json obj2 = json::parse(R"({"a": 1, "b": 2})");

// Deep Equal
json obj3 = json::parse(R"({"a": {"b": 1}})");
json obj4 = json::parse(R"({"a": {"b": 1}})");


assert(obj1 == obj2);
assert(obj1 != obj3);
assert(obj2 != obj3);

assert(obj3 == obj4);

// List Equal

json list1 = json::parse(R"([ 1, 2, []])");
json list2 = json::parse(R"([ 1, 2])");
json list3 = json::parse(R"([ 1, 2, []])");
json list4 = json::parse(R"([ 1, 2, {}])");

assert(list1 != list2);
assert(list1 == list3);
assert(list3 != list4);

}
*/

/*
{
json null = nullptr;


std::cout << "null == nullptr: " << (null == nullptr) << std::endl;
}
*/

/*
{
json ex1 = json::parse(R"(
{
"happy": true,
"pi": 2
}
)");

std::cout << isNode({ ex1 }) << std::endl;
}

{

std::ifstream f("../build/test/test.json");
json alltests = json::parse(f);

std::cout << "spec: " << alltests["minor"]["isnode"] << std::endl;

}
*/


/*
{
Struct _struct {new isList()};

std::cout << _struct.islist->apply({ 1 }) << std::endl;
std::cout << _struct.islist->apply({ json::array() }) << std::endl;
std::cout << _struct.islist->apply({ json::object() }) << std::endl;

}
*/


/*

json ex1 = json::parse(R"(
{
"happy": true,
"pi": 2
}
)");

json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};

// Using initializer lists
json ex3 = {
{"happy", true},
{"pi", 3.141},
};

json happy = ex1.at("happy");


json list1 = json::parse("[ 1, \"a\"]");
const json& list2 = list1;

std::vector<json> vec1;

if(list1.is_array()) {
for(json::iterator it = list1.begin(); it != list1.end(); ++it) {
vec1.push_back(it.value());
}
}


std::cout << ex1.dump(2) << std::endl;
std::cout << happy << std::endl;
std::cout << ex1.is_object() << std::endl;

for(size_t i = 0; i < vec1.size(); i++) {
std::cout << "vec[i]: " << vec1[i] << std::endl;
}

// Deep Copy
{
json obj1 = json::parse("{\"a\": {\"1\": \"2\" }}");
json obj2 = obj1;

obj1["a"]["1"] = 3;


std::cout << obj1.dump(2) << std::endl;
std::cout << obj2.dump(2) << std::endl;
}
*/
}
Loading