question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing Context of Serializing Object from Serializer

See original GitHub issue
public class UserProfile{
   Integer userId;

   Gender gender;

   @JsonPhotoUserUrl
   Integer photoId;
}

@Retention(RetentionPolicy.RUNTIME)
@JsonProperty("photoUrl")
@JsonSerialize(using = JsonPhotoUserUrlSerializer.class)
public @interface JsonPhotoUserUrl {

}

We would like to serialize photoId as a URL. However there are some users who have not a photo yet and for these conditions we would like to show a default picture according to gender. For this case we desperately need the gender.

Please provide a way to access object which is being serialized.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
sshcherbakovcommented, Sep 30, 2013

It looks like some generic context passing mechanism would be very useful. I need to serialize a field depending on the information not inherent to the object(s) being serialized but to the thread that is serializing. For instance, I have a message object with payload and headers, I’d like to convert its payload to JSON (or back) with some fields represented in different format (like string literal or numeric value for Enums) depending on the initial message header value, which is not part of the object tree being converted. Creating a new ObjectMapper with custom (de)serializers for each message to achieve the goal looks expensive.

0reactions
cowtowncodercommented, Dec 19, 2014

Actually, #631 does this with 2.5.0 (due to release soon).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializers - Django REST framework
Serializers · Declaring Serializers · Serializing objects · Deserializing objects · Saving instances · Validation · Accessing the initial data and ...
Read more >
Serializing context on Django CBV? - Stack Overflow
1 · I wish to get the data in the context to a JSON format, E.g 'organization_user': <OrganizationUser: Erik (MyOrgName)> so I can...
Read more >
Serializers - Django REST framework - Tom Christie
We'll declare a serializer that we can use to serialize and deserialize Comment objects. ... The first part of serializer class defines the...
Read more >
Serialization and custom serializers - Orleans - Microsoft Learn
[SerializerMethod] static private void Serialize( object input, ISerializationContext context, Type expected) { // ... } As with copiers, the " ...
Read more >
Serialization Context in O3DE - Open 3D Engine
Serializers are a useful way to provide custom data formats. If you want to do custom processing on an object before writing or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found