Skip to content

Commit

Permalink
Merge a413297 into 8492d3f
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrush committed Dec 27, 2019
2 parents 8492d3f + a413297 commit 08d8c4d
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 13 deletions.
21 changes: 15 additions & 6 deletions src/libs/conduit/conduit_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ Node::Node(const Generator &gen,
}

//---------------------------------------------------------------------------//
Node::Node(const std::string &json_schema,
Node::Node(const std::string &schema,
void *data,
bool external)
{
init_defaults();
Generator g(json_schema,"conduit_json",data);
Generator g(schema,"conduit_json",data);

if(external)
{
Expand Down Expand Up @@ -268,6 +268,15 @@ Node::Node(const DataType &dtype,
//
//-----------------------------------------------------------------------------

//---------------------------------------------------------------------------//
void
Node::parse(const std::string &text,
const std::string &protocol)
{
Generator gen(text,protocol,NULL);
gen.walk(*this);
}

//---------------------------------------------------------------------------//
void
Node::generate(const Generator &gen)
Expand All @@ -284,23 +293,23 @@ Node::generate_external(const Generator &gen)

//---------------------------------------------------------------------------//
void
Node::generate(const std::string &json_schema,
Node::generate(const std::string &schema,
const std::string &protocol,
void *data)

{
Generator g(json_schema,protocol,data);
Generator g(schema,protocol,data);
generate(g);
}

//---------------------------------------------------------------------------//
void
Node::generate_external(const std::string &json_schema,
Node::generate_external(const std::string &schema,
const std::string &protocol,
void *data)

{
Generator g(json_schema,protocol,data);
Generator g(schema,protocol,data);
generate_external(g);
}

Expand Down
25 changes: 18 additions & 7 deletions src/libs/conduit/conduit_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ class CONDUIT_API Node
// -- begin declaration of Node generate methods --
//
//-----------------------------------------------------------------------------
///@name Generation from JSON Schemas
///@name Generation from JSON or YAML Schemas
///@{
//-----------------------------------------------------------------------------
/// description:
/// These methods use a Generator to parse a json schema into a Node hierarchy.
/// These methods use a Generator to parse a schema into a Node hierarchy.
///
/// * The non external variant with a NULL data parameter will allocate memory
/// for the Node hierarchy and populate with inline values from the json schema
Expand All @@ -194,6 +194,19 @@ class CONDUIT_API Node
/// data, they do not copy the data into the Node hierarchy.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/// Simplifed parsing w/o direct use of a generator instance
///
/// valid protocols:
/// json
/// conduit_json
/// conduit_base64_json
/// yaml
///
//-----------------------------------------------------------------------------
void parse(const std::string &text,
const std::string &protocol = "yaml");

//-----------------------------------------------------------------------------
// -- direct use of a generator --
//-----------------------------------------------------------------------------
Expand All @@ -205,11 +218,11 @@ class CONDUIT_API Node
//-----------------------------------------------------------------------------
// -- json schema optionally coupled with in-core data --
//-----------------------------------------------------------------------------
void generate(const std::string &json_schema,
void generate(const std::string &schema,
const std::string &protocol = std::string("conduit_json"),
void *data = NULL);

void generate_external(const std::string &json_schema,
void generate_external(const std::string &schema,
const std::string &protocol,
void *data);

Expand All @@ -227,7 +240,7 @@ class CONDUIT_API Node
// -- begin declaration of Node basic i/o methods --
//
//-----------------------------------------------------------------------------
///@name Binary and Memory-Mapped I/O
///@name Text, Binary and Memory-Mapped I/O
///@{
//-----------------------------------------------------------------------------
/// description:
Expand All @@ -247,8 +260,6 @@ class CONDUIT_API Node
void mmap(const std::string &stream_path,
const Schema &schema);



//-----------------------------------------------------------------------------
///@}
//-----------------------------------------------------------------------------
Expand Down
91 changes: 91 additions & 0 deletions src/tests/conduit/t_conduit_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,97 @@ TEST(conduit_json, to_json_3)
Generator g(pure_json,"json");
Node n(g,true);
n.print_detailed();

int64_array i_vs = n["a"].value();

for (int64 i = 0; i < 5; i++)
{
EXPECT_EQ(i_vs[i], i);
}

float64_array f_vs = n["b"].value();

EXPECT_NEAR(f_vs[0], 0.0, 1e-6);
EXPECT_NEAR(f_vs[1], 1.1, 1e-6);
EXPECT_NEAR(f_vs[2], 2.2, 1e-6);
EXPECT_NEAR(f_vs[3], 3.3, 1e-6);
}

//-----------------------------------------------------------------------------
TEST(conduit_json, parse_json_1)
{

uint32 a_val = 10;
uint32 b_val = 20;

Node n;
n["a"] = a_val;
n["b"] = b_val;

EXPECT_EQ(n["a"].as_uint32(), a_val);
EXPECT_EQ(n["b"].as_uint32(), b_val);

n.print_detailed();
}
//-----------------------------------------------------------------------------
TEST(conduit_json, parse_json_2)
{

uint32 a_val = 10;
uint32 b_val = 20;
uint32 arr[5];
for (uint32 i = 0; i < 5; i++)
{
arr[i] = i * i;
}

Node n;
n["a"] = a_val;
n["b"] = b_val;
n["arr"].set_external(DataType::uint32(5), arr);


std::string pure_json = n.to_json();

n.print_detailed();
n.print();

Node n2;
// use parse helper method instead of a generator
n2.parse(pure_json, "json");
n2.print_detailed();
n2.print();

//
// JSON parsing will place values into an int64,
// here we use "to_int64" to do a direct comparison
//
EXPECT_EQ(n["a"].to_int64(), n2["a"].to_int64());
EXPECT_EQ(n["b"].to_int64(), n2["b"].to_int64());
}

//-----------------------------------------------------------------------------
TEST(conduit_json, parse_json_3)
{
std::string pure_json = "{a:[0,1,2,3,4],b:[0.0,1.1,2.2,3.3]}";
Node n;
// use parse helper method instead of a generator
n.parse(pure_json, "json");
n.print_detailed();

int64_array i_vs = n["a"].value();

for (int64 i = 0; i < 5; i++)
{
EXPECT_EQ(i_vs[i], i);
}

float64_array f_vs = n["b"].value();

EXPECT_NEAR(f_vs[0], 0.0, 1e-6);
EXPECT_NEAR(f_vs[1], 1.1, 1e-6);
EXPECT_NEAR(f_vs[2], 2.2, 1e-6);
EXPECT_NEAR(f_vs[3], 3.3, 1e-6);
}

//-----------------------------------------------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions src/tests/conduit/t_conduit_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,34 @@ TEST(conduit_node, list_to_obj_cleanup)
}



//-----------------------------------------------------------------------------
TEST(conduit_node, test_parse_all_protos)
{
Node n;

n["a/b/c"] = (int64) 10;
n["a/b/d"] = (float64) 42.2;
n["a/b/e"] = " string !";

std::vector<std::string> txt_cases;
txt_cases.push_back(n.to_json("json"));
txt_cases.push_back(n.to_json("conduit_json"));
txt_cases.push_back(n.to_json("conduit_base64_json"));
txt_cases.push_back(n.to_yaml());

Node n2, info;
n2.parse(txt_cases[0],"json");
EXPECT_FALSE(n.diff(n2,info));
info.print();

n2.parse(txt_cases[1],"conduit_json");
EXPECT_FALSE(n.diff(n2,info));

n2.parse(txt_cases[2],"conduit_base64_json");
EXPECT_FALSE(n.diff(n2,info));

n2.parse(txt_cases[3],"yaml");
EXPECT_FALSE(n.diff(n2,info));

}
36 changes: 36 additions & 0 deletions src/tests/conduit/t_conduit_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,42 @@ TEST(conduit_yaml, parse_yaml_3)
}


//-----------------------------------------------------------------------------
TEST(conduit_yaml, parse_yaml_no_generator)
{
std::string txt_1 = "{\"a\": [0,1,2,3,4], \"b\":[0.0,1.1,2.2,3.3] }";
Node n;
n.parse(txt_1, "yaml");
std::cout << n.to_yaml() << std::endl;

EXPECT_TRUE(n["a"].dtype().is_int64());
EXPECT_TRUE(n["b"].dtype().is_float64());

std::string txt_2 = "a: [0,-1,2,-3,4]\nb: [0.0,-1.1,2.2,-3.3]\n";

Node n2;
n2.parse(txt_2, "yaml");
std::cout << n2.to_yaml() << std::endl;

EXPECT_TRUE(n["a"].dtype().is_int64());
EXPECT_TRUE(n["b"].dtype().is_float64());

int64_array a_val = n2["a"].value();
EXPECT_EQ(a_val[0], 0);
EXPECT_EQ(a_val[1], -1);
EXPECT_EQ(a_val[2], 2);
EXPECT_EQ(a_val[3], -3);
EXPECT_EQ(a_val[4], 4);

float64_array b_val = n2["b"].value();
EXPECT_EQ(b_val[0], 0);
EXPECT_EQ(b_val[1], -1.1);
EXPECT_EQ(b_val[2], 2.2);
EXPECT_EQ(b_val[3], -3.3);
}



//-----------------------------------------------------------------------------
TEST(conduit_yaml, parse_yaml_4)
{
Expand Down

0 comments on commit 08d8c4d

Please sign in to comment.