Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:19:13+00:00 2026-05-27T14:19:13+00:00

Pretty simple question, hard to find an answer: I want to Serialize in JSON

  • 0

Pretty simple question, hard to find an answer:

I want to Serialize in JSON my POJO JPA entities with references being Dojo-like to use it with dojox.data.JsonRestStore.

The server part is made with Spring MVC and HttpConverters to make it a RESTfull web-app. I’m using JacksonJson to convert JPA entities to JSON.

As a lot of people have, I do have problems with multiple references. @JsonBackReference and @JsonManagedReference do not do the trick!

Considering this basic example:

public class A {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable=false)
    private String field;

    @OneToMany(mappedBy = "b")
    private List<B> bs;
}

public class B {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToOne
    private A a;
}

With the basic Jackson Json converter you obtain something like this (considering you have put the @JsonBackReference and @JsonManagedReference):

{ id:1, fieldB:"bbbbb", a: { id:1, fieldA: "aaaaaa" }}

In a concrete application, having the list of bs from the A objects is pointless in most cases. But having knowing what kind of A object is in the B.a field is important (at least to me).

Now, the problem lies in the references which are copied and you can end up having some serious big JSON with not much data in it.

The Dojo Framework provides a reference standard explained in here : http://www.sitepen.com/blog/2008/06/17/json-referencing-in-dojo/ and
http://dojotoolkit.org/reference-guide/dojox/data/JsonRestStore.html.

So what I’m searching is a way to define this as the following:

{ id:1, fieldB:"bbbbb", a: { $ref: "getA/1"}}

First problem: Jackson obviously can’t know what url you will use for the Reference.

To resolve that problem I made very simple interface like this:

public interface EntityWithId {
    public Long getId();
    public void setId(Long id);
}

This interface in then implemented by all my entities and I made this very very simple JsonSerializer:

public class EntityIdSerializer extends JsonSerializer<EntityWithId> {

    @Override
    public void serialize(EntityWithId value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
                jgen.writeStartObject();
                jgen.writeStringField("$ref", "db/" + value.getClass().getSimpleName() + "/" + value.getId().toString());
                jgen.writeEndObject();
    }
}

With those two I got the desired JSON, adding @JsonSerialize(using = EntityIdSerializer.class) to the @ManyToOne fields.

Is there no more standard approach? The Dojo dojox.data.JsonRestStore sounds really promising as you can combine it with lots of Dijits widgets, but is it really a nice thing to do and try? Won’t my REST app be to much Dojo-restrictive?

Thanks for your answers!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:19:14+00:00Added an answer on May 27, 2026 at 2:19 pm

    Dojo approach sounds like it requires dealing with a complete logical tree, to be able to use location-based references. This will not work well with Jackson, which supports fully incremental data-binding, i.e. only subset of the logical being available at the point where data is read or written.

    So Jackson does not support it by default; nor is that approach considered much of a standard in general (AFAIK). At least I havent seen it supported by Java libraries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty simple question, but can't seem to find a complete answer on here... I
Pretty simple question, perhaps not so simple answer though: I've got a clear view
A pretty simple question, but I couldn't find an example for it: How do
This is pretty simple question, but I'm having really hard time with it. I
This is a hard question to word, but is pretty simple when explained a
I have a pretty simple question, but with an answer that I am unable
Pretty simple question: When i have a persistable object, it usually has a property
Pretty simple question, I'm writing an XML document and i'm not sure how to
Pretty simple question. I have a few ASP RequiredFieldValdators checking some text boxes. Out
Pretty simple question. I've got some Polygons and GroundOverlays defined in KML. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.