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

Help wanted with nested objects #2556

Open
skhaz opened this issue Oct 26, 2023 · 0 comments
Open

Help wanted with nested objects #2556

skhaz opened this issue Oct 26, 2023 · 0 comments

Comments

@skhaz
Copy link

skhaz commented Oct 26, 2023

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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant