Skip to content

Adding Sql Blob/SerialBlob Serializer #2924

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

Closed
wants to merge 0 commits into from
Closed

Conversation

rsatrio
Copy link
Contributor

@rsatrio rsatrio commented Nov 6, 2020

Hi,
I'm adding Sql Blob/SerialBlob automatic serializer to base64 String. I hope it helps, especially when trying to serializes POJO from DB Query to Json Form.

Thanks,

@cowtowncoder
Copy link
Member

Sounds reasonable, although there is one challenge here: recent change #2913 will make all of "java.sql" optional wrt Java 9+ module system, and so we'd need new (de)serializer to be similarly dynamic. The simplest (even if not very clean) way would probably to register them via DateDeserializers, so if you could do that, I could merge this first, and then consider if other refactoring makes sense to move all "java.sql" deserializers under, say, com.fasterxml.jackson.databind.ext.
Not quite sure about serializer but something similar would be needed.

Similarly I think tests probably belong somewhere else: I can move them after merge as well.

So if you could move deserialization registration I think this would be mergeable.

The other thing I will need before merging is CLA (unless I have asked and received one before?). It's this:

https://github.com/FasterXML/jackson/blob/master/contributor-agreement.pdf

and is easiest to print, fill & sign, scan/photo, email to info at fasterxml dot com. Only needs to be done once, good for all contributions for all Jackson projects.

Thank you again for this PR, looking forward to getting it merged!

@rsatrio
Copy link
Contributor Author

rsatrio commented Nov 6, 2020

Hi,
Thanks for your review. I will fill out the contributor form shortly. I want to clear up what you are saying. So, basically I have to made the deserializer also for Blob, right? I think I can do it, but I need to studiednit first, including the DateDeserializers you mention above.

Thanks,

@cowtowncoder
Copy link
Member

cowtowncoder commented Nov 6, 2020

@rsatrio ah, I only skimmed through changes. Adding just serializer first is fine by me -- deserializer would be nice but that can be added as a later step too.

I moved "java.sql" based tests to under com.fasterxml.jackson.databind.ext fwtw, to help isolate; can add test(s) for Sql Blob there too.

int bLength = (int) value.length();
byte[] blob1 = value.getBytes(1, bLength);

gen.writeString(Base64.getEncoder().encodeToString(blob1));
Copy link
Member

@cowtowncoder cowtowncoder Nov 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? call JsonGenerator.writeBinary() which handles base64 encoding.
We also couldn't use java.util.Base64 with 2.12 anyway, as that was added in JDK 8 (and baseline for now is still JDK7), but fortunately no need.

@JacksonStdImpl
@SuppressWarnings("serial")
public class SqlBlobSerializer
extends JsonSerializer<Blob>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use StdScalarSerializer as base instead (serializes as scalar value, JSON String; as opposed to structured Object/Array).

@@ -48,12 +64,16 @@
sers.put(Void.class, NullSerializer.instance);
sers.put(Void.TYPE, NullSerializer.instance);

//And Sql Blob
sers.put(Blob.class, new SqlBlobSerializer());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this down to next block, along with java.sql.Timestamp etc; that way it can be optional wrt Module system.

@@ -48,12 +64,16 @@
sers.put(Void.class, NullSerializer.instance);
sers.put(Void.TYPE, NullSerializer.instance);

//And Sql Blob
sers.put(Blob.class, new SqlBlobSerializer());
sers.put(SerialBlob.class, SqlBlobSerializer.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is needed as it implements Blob? I guess unit test should show either way.
But if needed, should also be moved within try-block and register SqlBlobSerializer.class and not pre-create serializer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed because when I use SerialBlob in the unit test, it will show error "No Serializer". I think this is because somehow it cannot comprehend that SerialBlob is actually implementation of Blob. If you have other suggestion, please do share.

@cowtowncoder
Copy link
Member

Actually going through PR, registering via StdJDKSerializers is fine (as other java.sql types are registered already).
But need to add registrations within try-catch block, to allow for optional existence of classes for Java 9+ (and possibly Android).

@rsatrio
Copy link
Contributor Author

rsatrio commented Nov 7, 2020

info at fasterxml dot com
Hi,

I have sent the CLA through email.

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

Successfully merging this pull request may close these issues.

2 participants