0

In Spring MVC and Jackson I have a really big Java object that I parse to a JSON string myBigJSONString (~1MB) and inline it into my HTML.

Then in my Javascript I do a JSON.parse(myBigJSONString) and everything works dandy.

However if I were to inline an syntactically correct Javascript object into my HTML and not a string representation thereof, my Javascript wouldn't have to read this huge string and parse it. It would already be parsed.

Question: How do I create a Javascript object from my Java object and inline it directly without going through a JSON string?

EDIT:

This is how my finished HTML looks right now:

var staticDataString = '[{\"propertyA\":\"valueA\"}, {\"propertyB\":\"valueB\"}]';
var staticData = JSON.parse(staticDataString);

This is how i want it to look

var staticData = [{propertyA:"valueA"}, {propertyB:"valueB"}];

1 Answer 1

2

In all current browsers this should work:

<script> 
  var obj =  <c:out value="${$myserlvetmapping.myjson}"></c:out>;
  alert(obj.test);
</script> 

Whearat this is the Spring-Handler:

model.addAttribute("myjson","{test:2}"); 

Finally this would be the browsers sourcecode:

var obj =  {test:2};
alert(obj.test);

And the alert reports 2.

About the creation of the JSON i suggest to use the "adapter-pattern", this means a lot of hand-written-code.

Why do you need the hand-written-adapter? Assuming you have crosswise referenced objects in java like This:

class Man {
  Wife wife;
}
class Wife {
  Man man;
}
Man joe = new Man();
Wife ann = new Wife();
joe.wife = ann;
ann.man = joe;

Your json would be

{man:{
  name:'joe',
  wife: {
    name: 'ann',
    man: {
      name: 'joe',
      wife: {
        name: 'ann',
        man: {
          name: 'joe',
          wife: {
            .....
          }
        }
      }
    }
  }
}}

To prevent recursion you can only use the Adapter-Pattern. This would work:

public final class ManJSONAdapter {
  private final Man man;
  public ManJSONAdapter(Man man){
    this.man = man;
  }
  public String toJSON(){
    String result="{";
    if (man != null) {
      result += "name:";
      if (man.name == null){
        result += "undefined"
      } else {
        result += "'" + StringEscapeUtils.ESCAPE_ECMASCRIPT.translate(man.name) +"'";
      }
      result += ",wife:";
      if (man.wife == null) {
         result += "undefined";
      } else {
         ...
      }
    }
    result += "}";
    return result;
  }
}
Sign up to request clarification or add additional context in comments.

10 Comments

"About the creation of the JSON" that is the point of this question.
Do you miss something?
I don't think About the creation of the JSON i suggest to use the "adapter-pattern", this means a lot of hand-written-code. is enough of an answer to the heart of the question. I don't have any circular dependencies in my object structure and it is perfectly possible for me to create a normal JSON string with all the extra " and \ included. I will try to expand my question with examples to better illustrate my point...
You mean you have no circular dependencies yet! What if someone add a circular dependency? A OutOfMemoryError kills your server and you will join the hall-of-fame.
Ok, that may be true, but don't Jackson handle such circular dependencies in its serialization from Java to JSON string?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.