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

Access to JsonElement in @PostDeserialize #23

Closed
molexx opened this issue Dec 8, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@molexx
Copy link

commented Dec 8, 2015

It seems that @PostDeserialize'd methods need to have 0 arguments. Is there a way to access the source JsonElement from within the method?

If not, could it be added as an argument please?

This would be useful when the JSON contains keys that do not have matching property names in the class.

I'd be happy to submit a pull request with changes to FireTypeAdapter.java and HooksInvoker.java if that makes sense.

@julman99

This comment has been minimized.

Copy link
Owner

commented Dec 9, 2015

Hello, I'll evaluate adding that feature (automatically inject a JsonElement if specified as a parameter on the method). However, I suggest using GsonFireBuilder.registerPostProcessor since it is more flexible and decouples the post processing logic from the models. You can still have a static class inside your model to handle private fields, for example

public class MyModel {
    private String name;
    private String nameLowercase;

    public static class MyModelPostProcessor implements PostProcessor<MyModel> {

        void postDeserialize(MyModel result, JsonElement src, Gson gson) {
            result.nameLowercase = result.name.toLowerCase();
        }

        void postSerialize(JsonElement result, MyModel src, Gson gson) {
            //nothing
        }
    }
}

// In the code you initialize GsonFire
new GsonFireBuilder()
  .registerPostProcessor(MyModel.class, new MyModel.MyModelPostProcessor())

Sorry if the code above does not compiles, I did it in a plain text editor.

Let me know if that fixes your issue

@molexx

This comment has been minimized.

Copy link
Author

commented Dec 10, 2015

The PostProcessor would do it but the @PostDeserialize annotation makes more concise and tidier code. The boilerplate of the PostProcessor static class can be skipped and it's simpler to setup the builder by calling .enableHooks() than .registerPostProcessor() because you only need to mention the Model.class, not the PostProcessor too.

(I intend to dynamically generate the calls to .enableHooks() so I don't need to manually list out all the Model classes)

Clearer and simpler code is the reason I'm attracted to gson-fire on top of standard GSON. Thanks BTW :-)

julman99 added a commit that referenced this issue Dec 13, 2015

@julman99

This comment has been minimized.

Copy link
Owner

commented Dec 13, 2015

Hello, this enhancement was just added in 1.5.0

Let me know if it works for you. Thanks!

@julman99 julman99 closed this Dec 13, 2015

@molexx

This comment has been minimized.

Copy link
Author

commented Dec 15, 2015

Hi, yes works great for me, thanks!

@julman99

This comment has been minimized.

Copy link
Owner

commented Dec 15, 2015

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.