stdlib/json.rb in opal-0.10.6 vs stdlib/json.rb in opal-0.11.0.rc1
- old
+ new
@@ -1,10 +1,23 @@
module JSON
+ class JSONError < StandardError
+ end
+
+ class ParserError < JSONError
+ end
+
%x{
- var $parse = JSON.parse,
- $hasOwn = Opal.hasOwnProperty;
+ var $hasOwn = Opal.hasOwnProperty;
+ function $parse(source) {
+ try {
+ return JSON.parse(source);
+ } catch (e) {
+ #{raise JSON::ParserError, `e.message`};
+ }
+ };
+
function to_opal(value, options) {
var klass, arr, hash, i, ii, k;
switch (typeof value) {
case 'string':
@@ -39,11 +52,10 @@
#{`hash`[`k`] = `to_opal(value[k], options)`};
}
}
if (!options.parse && (klass = #{`hash`[JSON.create_id]}) != nil) {
- klass = Opal.get(klass);
- return #{`klass`.json_create(`hash`)};
+ return #{::Object.const_get(`klass`).json_create(`hash`)};
}
else {
return hash;
}
}