Closed SanderMertens closed 6 years ago
API 2.0 example:
#include <corto/corto.h>
// Dynamically create a 'Point' type with two members x & y
corto_struct Point_o = corto_declare(root_o, "Point", corto_struct_o);
corto_member x_o = corto_declare(Point_o, "x", corto_member_o);
x_o->type = corto_int32_o;
corto_define(x_o);
corto_member y_o = corto_declare(Point_o, "y", corto_member_o);
y_o->type = corto_int32_o;
corto_define(y_o);
corto_define(Point_o);
// Create an instance of Point in root with id "p"
corto_object p_o = corto_create(root_o, "p", Point_o);
// Dynamically set the value of p to {10, 20}
corto_any p_a = corto_any_init(p);
corto_any_dotset_int(p_a, "x", 10);
corto_any_dotset_int(p_a, "y", 20);
// Walk the value of p, print member names + member value
int i;
for (i = 0; i < corto_any_count(pa); i ++) {
corto_any a = corto_any_get(p_a, i);
char *s = corto_any_to_string(a);
printf("%s = %s\n", corto_any_get_key(pa, i), s);
free(s);
}
// Serialize p to JSON
char *json = corto_any_serialize(p_a, "json");
printf("%s\n", json);
free(json);
// Cleanup resources
corto_delete(p_o);
corto_delete(Point_o);
The any API will be implemented in a separate package, and issues/progress will be tracked in that repository. The rest of the 2.0 API is done.
To improve usability, the v2.0 API will be smaller, more consistent, easier to learn, and have a couple of features that will make working with dynamic data in particular easier. In addition, the new API will lend itself better for encapsulation in other languages, which will hopefully result in development of more language bindings.
Highlights:
const
to indicate where strings should be freed and can be modifiedcorto_any
API that provides an easy way for dynamic reading/writing of data