-
Notifications
You must be signed in to change notification settings - Fork 56
Support conversion from an LDUser to a JsonObject #136
Conversation
Based on what you said about your use case, you may not need a custom adapter. Calling The reason The one property that isn't captured by Gson's default serialization is |
@eli-darkly You are right. I hadn't read the TypeAdapter impl closely enough. I ran into the privateAttributeNames problem. I think I can workaround it by telling Gson to serialize transient fields, though that still wouldn't reflect private attributes set in the LDConfig. I updated the PR with logic that seems to do what I want. Do you think there is value in merging this, or am I going to run into additional problems? |
|
||
Set<String> privateAttributeSet = new HashSet<>(); | ||
privateAttributeSet.addAll(this.privateAttributeNames); | ||
privateAttributeSet.addAll(config.privateAttrNames); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure of the reason for this line. The front end already has a privateAttributeNames
configuration option for the client, just like the back end; if you're using that option on the back end, presumably you would be doing the same on the front end. The privateAttributeNames
property in the user is meant for attributes that have been marked private for this user.
I mean, doing this won't hurt anything, but I think it is unnecessary and it also conflicts with what is arguably the normal goal of JSON serialization: capturing the state of this particular object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, I think adding the allAttributesPrivate
property from the client configuration into the user object is misleading (since the JS client will not actually use the property there). If you want your front-end client to always use the same configuration as the back end without having to specify it in code, I think it would be better to pass those configuration options separately.
I appreciate the work you've done on this, but I think I would be more comfortable with a simpler approach where we just make the default Gson behavior for this object more complete (i.e. allow There also may be a valid use case for making the |
Also, about this--
--as far as I can tell there was never really a good reason for that field to be transient. That's only relevant when we're serializing users for events (in UserAdapter) in which case we're not using Gson's default serializer anyway, therefore it doesn't matter whether that field is transient. Not sure what we were thinking there! |
Removing transient would work for me!
Yeah, starting to get the feeling I'm getting in over my head here. Thanks for your help! Also, for background: I have several JS apps that need to pull the user context, and initialize themselves. I'm hoping to centralize all that via REST. |
As of v4.3.1, the default Gson serialization for LDUser includes all properties so it is suitable for passing to the front end. As I mentioned, you'll need a different mechanism for passing configuration parameters like |
Perfect! Thanks Eli! Yeah, we will just avoid setting allAttributesPrivate in the config. |
avoid concurrency problem with date parser for event responses
We want to extract the LDUser state from server, so it can be used to initialize client-side properties. Since we are already sending a secureModeHash to the client, it is natural to collect user context on the server side as well.
I couldn't find any way to get access to LDUser.UserAdapter, or any other solution.