Skip to Content

Hallo!

Problem:

In a SAPUI5 application with JSON model the method getJSON(); is oftenly used to convert a JSON model to string variable. When trying to run such an application in MSIE9 the exception occurs with the following message:

‘JSON’ is undefined

This message is displayed while catching the exception, e.g.:

try

{

  var jollyString=jollyModel.getJSON();

}

catch(e)

{

  alert(e.message);

}

Background:

The class sap.ui.model.json.JSONModel is inherited from class JSON. The method  sap.ui.model.json.JSONModel.getJSON() is inherited from  JSON.stringify() method. Seems the JSON.stringify() is not supported in MSIE9. This is the reason for error in the inherited method.

Solution:

Include in your application SAPUI5 code the implementation of JSON.stringify() method. Also JSON.parse() would be reasonable.

var JSON = JSON || {};
// implement JSON.stringify serialization
JSON
.stringify = JSON.stringify || function(obj) {
   
var t = typeof (obj);
   
if (t != “object” || obj === null) {
       
// simple data type
       
if (t == “string”)
            obj
= ‘”‘ + obj + ‘”‘;
       
return String(obj);
   
} else {
       
// recurse array or object
       
var n, v, json = [], arr = (obj && obj.constructor == Array);
       
for (n in obj) {
            v
= obj[n];
            t
= typeof (v);
           
if (t == “string”)
                v
= ‘”‘ + v + ‘”‘;
           
else if (t == “object” && v !== null)
                v
= JSON.stringify(v);
            json
.push((arr ? “” : ‘”‘ + n + ‘”:’) + String(v));
       
}
       
return (arr ? “[“ : “{“) + String(json) + (arr ? “]” : “}”);
   
}
};
// implement JSON.parse de-serialization
JSON
.parse = JSON.parse || function() {
   
var r = “(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)”, k = ‘(?:[^\\0-\\x08\\x0a-\\x1f”\\\\]|\\\\(?:[“/\\\\bfnrt]|u[0-9A-Fa-f]{4}))’;
    k
= ‘(?:”‘ + k + ‘*”)’;
   
var s = new RegExp(
           
“(?:false|true|null|[\\{\\}\\[\\]]|” + r + “|” + k + “)”, “g”), t = new RegExp(
           
“\\\\(?:([^u])|u(.{4}))”, “g”), u = {
       
‘”‘ : ‘”‘,
       
“/” : “/”,
       
“\\” : “\\”,
        b
: “\u0008”,
        f
: “\u000c”,
        n
: “\n”,
        r
: “\r”,
        t
: “\t”
   
};
   
function v(h, j, e) {
       
return j ? u[j] : String.fromCharCode(parseInt(e, 16));
   
}
   
var w = new String(“”), x = Object.hasOwnProperty;
   
return function(h, j) {
        h
= h.match(s);
       
var e, c = h[0], l = false;
       
if (“{“ === c)
            e
= {};
       
else if (“[“ === c)
            e
= [];
       
else {
            e
= [];
            l
= true;
       
}
       
for ( var b, d = [ e ], m = 1 l, y = h.length; m = 0;)
                           
delete f[i[g]];
               
}
               
return j.call(n, o, f);
           
};
            e
= p({
               
“” : e
           
}, “”);
       
}
       
return e;
   
};
}();

Mit freundlischen Gruessen,

Dmitry Irsheyns

BW Hana CoE

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply