Skip to content

Adding js member listeners #50

@sheepy9

Description

@sheepy9

Hi, I really like your library. I think it provides a really elegant interface for serializing and deserializing json.

I would like to propose adding a feature that I think could be useful to the users of json_struct. The feature I would like to propose is having the ability to register listeners to struct members that will be called during deserialization.
For example, deserializing a Person object with the fields name and age could trigger methods registered as property listeners like onNameChanged(std::string) and onAgeChanged(int).
The syntax for adding these property listeners could be something similar to the existing workflow. Something along the lines of:

struct Person
{
    std::string name;
    unsigned age;
    void onNameChanged(std::string);
    void onAgeChanged(unsigned);

    JS_OBJECT(JS_MEMBER_WITH_LISTENER(name, &Person::onNameChanged), 
              JS_MEMBER_WITH_LISTENER(age, &Person::onAgeChanged));
};

The outcome of this would be that upon deserializing, the registered listeners would be triggered, which could enable some additional custom logic to run for every deserialized field.

I understand this feature might not exactly be part of the original scope for json_struct, but I think it might add real value to the library and increase its utility by adding something more than serializing and deserializing.

I would like to volunteer to write the implementation of this. Even if you think this feature doesn't belong in json_struct, I would still appreciate some pointers on how you would recommend integrating this in json_stuct, as I would like to give it a whirl anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions