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

Make JsonNode serializable? #18

Closed
jakajancar opened this issue May 24, 2012 · 14 comments
Closed

Make JsonNode serializable? #18

jakajancar opened this issue May 24, 2012 · 14 comments
Milestone

Comments

@jakajancar
Copy link

Would it be possible to make JsonNode java.io.Serializable?

@cowtowncoder
Copy link
Member

I'll have to think about this. In theory, probably. Someone would just need to go ahead and test that it all actually works.
I would not make it serialize as JSON, but just using standard method, which means that it'd be necessary to mark fields as transient (etc) as appropriate.

One possible problem is that of reference to JsonNodeFactory, which may be problematic (or possibly not).

So I guess I would accept contributions for such feature, yes.

@jakajancar
Copy link
Author

Why wouldn't you make is serialize to JSON, given that it's faster, more compact, etc.?

@cowtowncoder
Copy link
Member

One problem is that there isn't enough contextual information (no ObjectMapper). I guess one could just use default settings to overcome that; but a JsonFactory is still needed. And it would be wasteful and slow to construct one for one time serialization. For that it would be possible to perhaps use Weak- or SoftReference cached copy.

So I am open to contibutions as long as they work well and are clean -- you may try to do JSON back serialization if you want. That would most likely use Externalizable interface.

@tryabin
Copy link

tryabin commented Aug 21, 2012

Is it possible to create a class that extends ObjectNode and implements Serializable to have a JsonNode-like class that supports Serializable functionality?

@cowtowncoder
Copy link
Member

Depending on what you are trying to achieve, it's probably better to just use aggregation: create your own class, have ObjectNode as member, do it that way.

Using Jackson for replacing JDK serialization, this might be useful: http://www.cowtowncoder.com/blog/archives/2012/08/entry_477.html

@tryabin
Copy link

tryabin commented Aug 22, 2012

If I used aggregation would I have to create readObject and writeObject methods in order to serialize and de-serialize the ObjectNode member?

@cowtowncoder
Copy link
Member

Yes, the root object would need to implement Externalizable, and from that point you can use custom serialization/deserialization, such as just using JSON output.

@cowtowncoder
Copy link
Member

I don't think I will work on this -- if anybody else wants to tackle it, feel free to re-open, but only if you actually work on it (not just wish to do that).

codicusmaximus pushed a commit to codicusmaximus/jackson-databind that referenced this issue Mar 10, 2017
@jwgmeligmeyling
Copy link

I'd love to see this feature and would be willing to pick it up.

From this thread there seem to be two options:

  • Implement it as java.io.Serializable, mark the JsonNodeFactory as transient, and possibly define a readObject that instantiates this JsonNodeFactory with a default instance.
  • Implement it as java.io.Externalizable, using the JSON representation as data. Use a default JsonNodeFactory here as well?

I have two questions:

  1. Does JsonNode still depend on the JsonNodeFactory in the current situation?
  2. Wouldn't it be an option to serialize the JsonNodeFactory with the JsonNode?

BTW, I think commit 92223fe is wrongly pointed to this issue #18 , perhaps it references an issue from core?

@cowtowncoder
Copy link
Member

@jwgmeligmeyling ok. Just a quick question: what is the use case you are thinking of?

On implementation itself: this would be a bit easier with Jackson 3.0 because of #2176.
That would solve a few issues...

But if you really want to work on this for 2.10, I am ok with it. I'd probably just go with throw-away JsonFactory, if possible; otherwise code must create default ObjectMapper and that's guaranteed to make this rather wasteful operation.

@jwgmeligmeyling
Copy link

My JPA entities need to be serializable in order for my EntityManager to passivate into a stateful session bean. Some of my entities map a JsonNode from JSONB columns, if the data type foe the JSON input is not fixed for that entity. Now I’m required to implement custom serialization for all entity classes that reference such a JsonNode. Even more tedious is that I cant call defaultSerialization in my custom serializer, because transient fields will always be ignored by JPA and there is apparently no way to work around that. So for JPA entities that means you have to manually searialize/deserialize every field which is tedious and error prone. So Id rather make JsonNode serializable in one way or another :)

@demetrio812
Copy link

Hello, same problem for me, I'm trying to use EhCache with some Entities containing JsonNode and it's not working because of this issue

@cowtowncoder
Copy link
Member

Actually. Now with 2.10 and #2187 I wonder...

There would be some limitations, of course:

  1. Only json would be supported as serialization format (but it's JDK serialization so no one sees it anyway)
  2. On deserialization, no configuration of JsonNode (via custom JsonNodeFactory, or, in future, with NodeConfig or such) could be applied; you would get stock vanilla JsonNode back
  3. POJONode would probably lose its identity (although maybe not...)

So... I'll re-consider this issue.

@demetrio812
Copy link

Hello @cowtowncoder thanks for reconsidering, I think the limitations are fine and understandable but this basic support will help a lot for use cases like the caching one.

Thanks,
Dem

@cowtowncoder cowtowncoder changed the title Make JsonNode serializable? Make ObjectNode and ArrayNode serializable? Jun 5, 2019
@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Jun 5, 2019
@cowtowncoder cowtowncoder changed the title Make ObjectNode and ArrayNode serializable? Make JsonNode serializable? Jun 5, 2019
cowtowncoder added a commit that referenced this issue Jun 5, 2019
@cowtowncoder cowtowncoder removed the 2.10 label Apr 12, 2020
arte-dev pushed a commit to eDatos/external-users that referenced this issue Mar 10, 2022
> Actualiza la versión de Jackson. Al actualizar jackson, también se
actualiza una dependencia indirecta: jackson-databind, la cual es
necesaria para la serialización de ObjectNode, tipo usado para almacenar
objetos de tipo "jsonb" en PostgreSQL a través de Hibernate.

Hasta ahora se estaba usando la versión 2.8.8 de jackson-databind. El
problema es que esta versión no marca como Serializable a ObjectNode,
causando una excepción. Esto se corrigió en la v2.10 (véase
FasterXML/jackson-databind#18), por lo cual la
actualización corrige este problema.
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

5 participants