Class: JSON
- Inherits:
-
Object
- Object
- JSON
- Defined in:
- lib/red_query/json.rb
Class Method Summary collapse
- .classify(js_native) ⇒ Object
-
.classify_ruby(stuff) ⇒ Object
Stringify =============================================.
- .generate(stuff) ⇒ Object
-
.parse(text) ⇒ Object
Parsing =============================================.
- .stringify(stuff) ⇒ Object
- .stringify_array(stuff) ⇒ Object
- .stringify_hash(stuff) ⇒ Object
- .stringify_string(str) ⇒ Object
- .translate(js_native) ⇒ Object
- .translate_array(js_native) ⇒ Object
- .translate_object(js_native) ⇒ Object
Class Method Details
.classify(js_native) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/red_query/json.rb', line 9 def self.classify(js_native) type = String.new(`typeof #{js_native}`) if (type == 'object' && `!#{js_native}`) return "null" end if (type == 'object' && `Object.prototype.toString.apply(#{js_native}) === '[object Array]'`) return "array" end type end |
.classify_ruby(stuff) ⇒ Object
Stringify =============================================
62 63 64 65 66 67 |
# File 'lib/red_query/json.rb', line 62 def self.classify_ruby(stuff) if (stuff.class) return stuff.class.inspect.split("::").last end "Boolean" end |
.generate(stuff) ⇒ Object
139 140 141 |
# File 'lib/red_query/json.rb', line 139 def self.generate(stuff) self.stringify(stuff) end |
.parse(text) ⇒ Object
Parsing =============================================
3 4 5 6 7 |
# File 'lib/red_query/json.rb', line 3 def self.parse(text) raise "[JSON.parse] empty JSON-String" if text == "" json_native = `eval("("+#{text}.__value__+")");` JSON.translate(json_native) end |
.stringify(stuff) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/red_query/json.rb', line 113 def self.stringify(stuff) type = JSON.classify_ruby(stuff) if (type == "Numeric" || type == "Boolean") return stuff.inspect end if (type == "String") return JSON.stringify_string(stuff) end if (type == "Symbol") return JSON.stringify_string(stuff.to_s) end if (type == "Array") return JSON.stringify_array(stuff) end if (type == "Hash") return JSON.stringify_hash(stuff) end "null" end |
.stringify_array(stuff) ⇒ Object
102 103 104 105 |
# File 'lib/red_query/json.rb', line 102 def self.stringify_array(stuff) stuff.map! { |elem| JSON.stringify(elem) } return '[' + stuff.join(',') + ']' end |
.stringify_hash(stuff) ⇒ Object
107 108 109 110 111 |
# File 'lib/red_query/json.rb', line 107 def self.stringify_hash(stuff) pairs = [] stuff.each { |key, value| pairs.push(JSON.stringify(key) + ':' + JSON.stringify(value)) } return '{' + pairs.join(',') + '}' end |
.stringify_string(str) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/red_query/json.rb', line 69 def self.stringify_string(str) ` var cx = /[\\u0000\\u00ad\\u0600-\\u0604\\u070f\\u17b4\\u17b5\\u200c-\\u200f\\u2028-\\u202f\\u2060-\\u206f\\ufeff\\ufff0-\\uffff]/g, escapable = /[\\\\\\"\\x00-\\x1f\\x7f-\\x9f\\u00ad\\u0600-\\u0604\\u070f\\u17b4\\u17b5\\u200c-\\u200f\\u2028-\\u202f\\u2060-\\u206f\\ufeff\\ufff0-\\uffff]/g, gap, indent, meta = { // table of character substitutions '\\b': '\\\\b', '\\t': '\\\\t', '\\n': '\\\\n', '\\f': '\\\\f', '\\r': '\\\\r', '"' : '\\\\"', '\\\\': '\\\\\\\\' }, rep; function quote(string) { escapable.lastIndex = 0; return escapable.test(string) ? '"' + string.replace(escapable, function (a) { var c = meta[a]; return typeof c === 'string' ? c : '\\\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); }) + '"' : '"' + string + '"'; } ` value = String.new(`quote(#{str}.__value__)`) end |
.translate(js_native) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/red_query/json.rb', line 40 def self.translate(js_native) type = JSON.classify(js_native) if (type == 'string') return String.new(js_native) end if (type == 'number' || type == 'boolean') return js_native end if (type == 'array') return JSON.translate_array(js_native) end if (type == 'object') return JSON.translate_object(js_native) end return nil end |
.translate_array(js_native) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/red_query/json.rb', line 20 def self.translate_array(js_native) js_natives = [] `for (var i=0; i<#{js_native}.length; i++) { #{js_natives}.push(#{js_native}[i]); }` js_natives.collect { |x| JSON.translate(x) } end |
.translate_object(js_native) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/red_query/json.rb', line 27 def self.translate_object(js_native) stuff = [] `for(var member in #{js_native}){#{stuff}.push(new Array($q(member), #{js_native}[member]));}` obj = {} stuff.each { |x| key = x[0] value = x[1] obj[key] = JSON.translate(value) } return obj end |