Help wanted with nested objects
skhaz opened this issue · comments
First of all, thank you very much for the library. I'm really enjoying using it in a 2D engine project.
I would like to know if you could or would like to help me with the following issue. I have a SAX that I use to convert a JSON string into an object. I could use the one from Duktape directly, but for other reasons, I want to use a different library.
What I've done works partially, but I'm not able to create the relationship of nested objects. As you can see below, I have an input JSON that goes through the SAX, and in JavaScript, when I do a JSON.stringify, I only get the last serialized object.
What could be the issue? I'm probably forgetting to push at some point, I've already tried several things.
Thank you.
The SAX:
class UnmarshalSax : public nlohmann::json_sax<nlohmann::json> {
private:
duk_context *ctx;
public:
UnmarshalSax(duk_context *ctx) : ctx(ctx){};
virtual ~UnmarshalSax() = default;
virtual bool null() override {
duk_push_null(ctx);
duk_put_prop(ctx, -3);
return true;
}
virtual bool boolean(bool val) override {
duk_push_boolean(ctx, val);
duk_put_prop(ctx, -3);
return true;
}
virtual bool number_integer(number_integer_t val) override {
duk_push_int(ctx, val);
duk_put_prop(ctx, -3);
return true;
}
virtual bool number_unsigned(number_unsigned_t val) override {
duk_push_uint(ctx, val);
duk_put_prop(ctx, -3);
return true;
}
virtual bool number_float(number_float_t val, const string_t &s) override {
duk_push_number(ctx, val);
duk_put_prop(ctx, -3);
return true;
}
virtual bool string(string_t &val) override {
duk_push_string(ctx, val.c_str());
duk_put_prop(ctx, -3);
return true;
}
virtual bool binary(binary_t &val) override {
return true;
}
virtual bool start_object(std::size_t elements) override {
duk_push_object(ctx);
return true;
}
virtual bool key(string_t &val) override {
std::cout << "key: " << val << std::endl;
duk_push_string(ctx, val.c_str());
return true;
}
virtual bool end_object() override {
return true;
}
virtual bool start_array(std::size_t elements) override {
duk_push_array(ctx);
return true;
}
virtual bool end_array() override {
return true;
}
virtual bool parse_error(std::size_t position, const std::string &last_token, const nlohmann::detail::exception &ex) override {
return false;
}
};
The input JSON:
{"jsonrpc":"2.0","id":null,"result":{"integer":1,"boolean":true,"string":"hello","null":null}}
The result:
{"integer":1,"boolean":true,"string":"hello","null":null}