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

Custom Serializer Not Used Inside Map #40

Open
kaerfredoc opened this issue Jul 10, 2013 · 0 comments
Open

Custom Serializer Not Used Inside Map #40

kaerfredoc opened this issue Jul 10, 2013 · 0 comments

Comments

@kaerfredoc
Copy link

package com.app.security

class AuthUser {
String username
String password
}

package com.app.utils.gson

import com.app.security.AuthUser
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer

import java.lang.reflect.Type

class AuthUserSerializer implements JsonSerializer {

public AuthUserSerializer() {
super();
}

JsonElement serialize(AuthUser value, Type type, JsonSerializationContext context) {
final JsonObject jsonObj = new JsonObject();

['id', 'username'].each {
  jsonObj.add(it, context.serialize(value[it]))
}

return jsonObj;

}
}

...In BootStrap...

def gsonBuilder
def init = { servletContext ->
gsonBuilder.registerTypeAdapter(AuthUser.class, new AuthUserSerializer());
}

...In Controller...
def authUser = new AuthUser(username:"someuser", password:"NOT SUPPOSED TO SHOW").save()

// Works fine
render (authUser as GSON) // = {"id":2, "username":"someuser"}

// Serializer not leveraged
render ([profile:memberService.currentMember] as GSON) // = {"profile":{"id":2, "username":"someuser", "password":"NOT SUPPOSED TO SHOW"}}

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