Class: WirisPlugin::JSon

Inherits:
StringParser show all
Includes:
Wiris
Defined in:
lib/com/wiris/util/json/JSon.rb

Instance Attribute Summary collapse

Attributes inherited from StringParser

#c, #i, #n, #str

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StringParser

#getPositionRepresentation, #init, isBlank, #isHexDigit, #nextSafeToken, #nextToken, #skipBlanks

Constructor Details

#initializeJSon

Returns a new instance of JSon.



20
21
22
# File 'lib/com/wiris/util/json/JSon.rb', line 20

def initialize()
    super()
end

Instance Attribute Details

#addNewLinesObject

Returns the value of attribute addNewLines.



17
18
19
# File 'lib/com/wiris/util/json/JSon.rb', line 17

def addNewLines
  @addNewLines
end

#depthObject

Returns the value of attribute depth.



18
19
20
# File 'lib/com/wiris/util/json/JSon.rb', line 18

def depth
  @depth
end

#lastDepthObject

Returns the value of attribute lastDepth.



19
20
21
# File 'lib/com/wiris/util/json/JSon.rb', line 19

def lastDepth
  @lastDepth
end

Class Method Details

.compare(a, b, eps) ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/com/wiris/util/json/JSon.rb', line 453

def self.compare(a, b, eps)
    if TypeTools::isHash(a)
        isBHash = TypeTools::isHash(b)
        if !isBHash
            return false
        end
        ha = (a)
        hb = (b)
        it = ha::keys()
        itb = hb::keys()
        while it::hasNext()
            if !itb::hasNext()
                return false
            end
            itb::next()
            key = it::next()
            if !hb::exists(key) || !JSon.compare(ha::get(key),hb::get(key),eps)
                return false
            end
        end
        if itb::hasNext()
            return false
        end
        return true
    else 
        if TypeTools::isArray(a)
            isBArray = TypeTools::isArray(b)
            if !isBArray
                return false
            end
            aa = (a)
            ab = (b)
            if aa::length() != ab::length()
                return false
            end
            for i in 0..aa::length() - 1
                if !JSon.compare(aa::_(i),ab::_(i),eps)
                    return false
                end
                i+=1
            end
            return true
        else 
            if a.instance_of?String
                if !(b.instance_of?String)
                    return false
                end
                return (a == b)
            else 
                if a.instance_of?Integer
                    if !(b.instance_of?Integer)
                        return false
                    end
                    return (a == b)
                else 
                    if a.instance_of?Bignum
                        isBLong = b.instance_of?Bignum
                        if !isBLong
                            return false
                        end
                        return (a == b)
                    else 
                        if a.instance_of?JSonIntegerFormat
                            if !(b.instance_of?JSonIntegerFormat)
                                return false
                            end
                            ja = (a)
                            jb = (b)
                            return (ja::toString() == jb::toString())
                        else 
                            if a.instance_of?Boolean
                                if !(b.instance_of?Boolean)
                                    return false
                                end
                                return (a == b)
                            else 
                                if a.instance_of?Double
                                    if !(b.instance_of?Double)
                                        return false
                                    end
                                    da = JSon.getFloat(a)
                                    db = JSon.getFloat(b)
                                    return (da >= (db - eps)) && (da <= (db + eps))
                                end
                            end
                        end
                    end
                end
            end
        end
    end
    return true
end

.decode(str) ⇒ Object



187
188
189
190
# File 'lib/com/wiris/util/json/JSon.rb', line 187

def self.decode(str)
    json = JSon.new()
    return json::localDecodeString(str)
end

.encode(o) ⇒ Object



23
24
25
26
# File 'lib/com/wiris/util/json/JSon.rb', line 23

def self.encode(o)
    js = JSon.new()
    return js::encodeObject(o)
end

.getArray(a) ⇒ Object



447
448
449
# File 'lib/com/wiris/util/json/JSon.rb', line 447

def self.getArray(a)
    return (a)
end

.getBoolean(b) ⇒ Object



444
445
446
# File 'lib/com/wiris/util/json/JSon.rb', line 444

def self.getBoolean(b)
    return ((b))::booleanValue()
end

.getDepth(o) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/com/wiris/util/json/JSon.rb', line 375

def self.getDepth(o)
    if TypeTools::isHash(o)
        h = (o)
        m = 0
        if h::exists("_left_") || h::exists("_right_")
            if h::exists("_left_")
                m = WInteger::max(JSon.getDepth(h::get("_left_")),m)
            end
            if h::exists("_right_")
                m = WInteger::max(JSon.getDepth(h::get("_right_")),m)
            end
            return m
        end
        iter = h::keys()
        while iter::hasNext()
            key = iter::next()
            m = WInteger::max(JSon.getDepth(h::get(key)),m)
        end
        return m + 2
    else 
        if TypeTools::isArray(o)
            a = (o)
            m = 0
            for i in 0..a::length() - 1
                m = WInteger::max(JSon.getDepth(a::_(i)),m)
                i+=1
            end
            return m + 1
        else 
            return 1
        end
    end
end

.getFloat(n) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
# File 'lib/com/wiris/util/json/JSon.rb', line 422

def self.getFloat(n)
    if n.instance_of?Double
        return (n)
    else 
        if n.instance_of?Integer
            return (n) + 0.0
        else 
            return 0.0
        end
    end
end

.getHash(a) ⇒ Object



450
451
452
# File 'lib/com/wiris/util/json/JSon.rb', line 450

def self.getHash(a)
    return (a)
end

.getInt(n) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
# File 'lib/com/wiris/util/json/JSon.rb', line 433

def self.getInt(n)
    if n.instance_of?Double
        return (Math::round((n)))
    else 
        if n.instance_of?Integer
            return (n)
        else 
            return 0
        end
    end
end

.getString(o) ⇒ Object



419
420
421
# File 'lib/com/wiris/util/json/JSon.rb', line 419

def self.getString(o)
    return (o)
end

.sbObject



11
12
13
# File 'lib/com/wiris/util/json/JSon.rb', line 11

def self.sb
    @@sb
end

.sb=(sb) ⇒ Object



14
15
16
# File 'lib/com/wiris/util/json/JSon.rb', line 14

def self.sb=(sb)
    @@sb = sb
end

Instance Method Details

#decodeArrayObject



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/com/wiris/util/json/JSon.rb', line 351

def decodeArray()
    v = Array.new()
    nextToken()
    skipBlanks()
    if c == 93
        nextToken()
        return v
    end
    while c != 93
        o = localDecode()
        v::push(o)
        skipBlanks()
        if c == 44
            nextToken()
            skipBlanks()
        else 
            if c != 93
                raise Exception,"Expected \',\' or \']\'."
            end
        end
    end
    nextToken()
    return v
end

#decodeBooleanOrNullObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/com/wiris/util/json/JSon.rb', line 223

def decodeBooleanOrNull()
    sb = StringBuf.new()
    while WCharacterBase::isLetter(c)
        sb::addChar(c)
        nextToken()
    end
    word = sb::toString()
    if (word == "true")
        return Boolean::TRUE
    else 
        if (word == "false")
            return Boolean::FALSE
        else 
            if (word == "null")
                return nil
            else 
                raise Exception,("Unrecognized keyword \"" + word) + "\"."
            end
        end
    end
end

#decodeHashObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/com/wiris/util/json/JSon.rb', line 320

def decodeHash()
    h = Hash.new()
    nextToken()
    skipBlanks()
    if c == 125
        nextToken()
        return h
    end
    while c != 125
        key = decodeString()
        skipBlanks()
        if c != 58
            raise Exception,"Expected \':\'."
        end
        nextToken()
        skipBlanks()
        o = localDecode()
        h::set(key,o)
        skipBlanks()
        if c == 44
            nextToken()
            skipBlanks()
        else 
            if c != 125
                raise Exception,"Expected \',\' or \'}\'. " + getPositionRepresentation()
            end
        end
    end
    nextToken()
    return h
end

#decodeNumberObject



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/com/wiris/util/json/JSon.rb', line 297

def decodeNumber()
    sb = StringBuf.new()
    hex = false
    floating = false
    loop do
        sb::add(Std::fromCharCode(c))
        nextToken()
        if c == 120
            hex = true
            sb::add(Std::fromCharCode(c))
            nextToken()
        end
        if ((c == 46) || (c == 69)) || (c == 101)
            floating = true
        end
    break if not (((c >= 48) && (c <= 58)) || (hex && JSon.isHexDigit(c))) || (floating && ((((c == 46) || (c == 69)) || (c == 101)) || (c == 45)))
    end
    if floating
        return Std::parseFloat(sb::toString())
    else 
        return Std::parseInt(sb::toString())
    end
end

#decodeStringObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/com/wiris/util/json/JSon.rb', line 244

def decodeString()
    sb = StringBuf.new()
    d = c
    nextToken()
    while c != d
        if c == 92
            nextToken()
            if c == 110
                sb::add("\n")
            else 
                if c == 114
                    sb::add("\r")
                else 
                    if c == 34
                        sb::add("\"")
                    else 
                        if c == 39
                            sb::add("\'")
                        else 
                            if c == 116
                                sb::add("\t")
                            else 
                                if c == 92
                                    sb::add("\\")
                                else 
                                    if c == 117
                                        nextToken()
                                        code = Utf8::uchr(c)
                                        nextToken()
                                        code += Utf8::uchr(c)
                                        nextToken()
                                        code += Utf8::uchr(c)
                                        nextToken()
                                        code += Utf8::uchr(c)
                                        dec = Std::parseInt("0x" + code)
                                        sb::add(Utf8::uchr(dec))
                                    else 
                                        raise Exception,("Unknown scape sequence \'\\" + Utf8::uchr(c).to_s) + "\'"
                                    end
                                end
                            end
                        end
                    end
                end
            end
        else 
            sb::add(Std::fromCharCode(c))
        end
        nextToken()
    end
    nextToken()
    return sb::toString()
end

#encodeArray(sb, v) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/com/wiris/util/json/JSon.rb', line 110

def encodeArray(sb, v)
    newLines = @addNewLines && (JSon.getDepth(v) > 2)
    @depth+=1
    myDepth = @lastDepth
    sb::add("[")
    if newLines
        newLine(@depth,sb)
    end
    for i in 0..v::length() - 1
        o = v::_(i)
        if i > 0
            sb::add(",")
            if newLines
                newLine(@depth,sb)
            end
        end
        encodeImpl(sb,o)
        i+=1
    end
    if newLines
        newLine(myDepth,sb)
    end
    sb::add("]")
    @depth-=1
end

#encodeArrayBoolean(sb, v) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/com/wiris/util/json/JSon.rb', line 153

def encodeArrayBoolean(sb, v)
    v2 = Array.new()
    i = 0
    while i < v::length
        v2::push(v[i])
        i+=1
    end
    self.encodeArray(sb,v2)
end

#encodeArrayDouble(sb, v) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/com/wiris/util/json/JSon.rb', line 144

def encodeArrayDouble(sb, v)
    v2 = Array.new()
    i = 0
    while i < v::length
        v2::push(v[i])
        i+=1
    end
    self.encodeArray(sb,v2)
end

#encodeArrayInt(sb, v) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/com/wiris/util/json/JSon.rb', line 135

def encodeArrayInt(sb, v)
    v2 = Array.new()
    i = 0
    while i < v::length
        v2::push(v[i])
        i+=1
    end
    self.encodeArray(sb,v2)
end

#encodeBoolean(sb, b) ⇒ Object



175
176
177
# File 'lib/com/wiris/util/json/JSon.rb', line 175

def encodeBoolean(sb, b)
    sb::add(b::booleanValue() ? "true" : "false")
end

#encodeFloat(sb, d) ⇒ Object



178
179
180
# File 'lib/com/wiris/util/json/JSon.rb', line 178

def encodeFloat(sb, d)
    sb::add(TypeTools::floatToString(d))
end

#encodeHash(sb, h) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/com/wiris/util/json/JSon.rb', line 80

def encodeHash(sb, h)
    newLines = @addNewLines && (JSon.getDepth(h) > 2)
    @depth+=1
    myDepth = @lastDepth
    sb::add("{")
    if newLines
        newLine(@depth,sb)
    end
    e = h::keys()
    first = true
    while e::hasNext()
        if first
            first = false
        else 
            sb::add(",")
            if newLines
                newLine(@depth,sb)
            end
        end
        key = e::next()
        encodeString(sb,key)
        sb::add(":")
        encodeImpl(sb,h::get(key))
    end
    if newLines
        newLine(myDepth,sb)
    end
    sb::add("}")
    @depth-=1
end

#encodeImpl(sb, o) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/com/wiris/util/json/JSon.rb', line 33

def encodeImpl(sb, o)
    if TypeTools::isHash(o)
        encodeHash(sb,(o))
    else 
        if TypeTools::isArray(o)
            encodeArray(sb,(o))
        else 
            if o.instance_of?Array
                encodeArrayInt(sb,(o))
            else 
                if o.instance_of?Array
                    encodeArrayDouble(sb,(o))
                else 
                    if o.instance_of?Array
                        encodeArrayBoolean(sb,(o))
                    else 
                        if o.instance_of?String
                            encodeString(sb,(o))
                        else 
                            if o.instance_of?Integer
                                encodeInteger(sb,(o))
                            else 
                                if o.instance_of?Bignum
                                    encodeLong(sb,(o))
                                else 
                                    if o.instance_of?JSonIntegerFormat
                                        encodeIntegerFormat(sb,(o))
                                    else 
                                        if o.instance_of?Boolean
                                            encodeBoolean(sb,(o))
                                        else 
                                            if o.instance_of?Double
                                                encodeFloat(sb,(o))
                                            else 
                                                raise Exception,"Impossible to convert to json object of type " + Type::getClass(o).to_s
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
    end
end

#encodeInteger(sb, i) ⇒ Object



172
173
174
# File 'lib/com/wiris/util/json/JSon.rb', line 172

def encodeInteger(sb, i)
    sb::add("" + i.to_s)
end

#encodeIntegerFormat(sb, i) ⇒ Object



184
185
186
# File 'lib/com/wiris/util/json/JSon.rb', line 184

def encodeIntegerFormat(sb, i)
    sb::add(i::toString())
end

#encodeLong(sb, i) ⇒ Object



181
182
183
# File 'lib/com/wiris/util/json/JSon.rb', line 181

def encodeLong(sb, i)
    sb::add("" + i.to_s)
end

#encodeObject(o) ⇒ Object



27
28
29
30
31
32
# File 'lib/com/wiris/util/json/JSon.rb', line 27

def encodeObject(o)
    sb = StringBuf.new()
    @depth = 0
    encodeImpl(sb,o)
    return sb::toString()
end

#encodeString(sb, s) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/com/wiris/util/json/JSon.rb', line 162

def encodeString(sb, s)
    s = StringTools::replace(s,"\\","\\\\")
    s = StringTools::replace(s,"\"","\\\"")
    s = StringTools::replace(s,"\r","\\r")
    s = StringTools::replace(s,"\n","\\n")
    s = StringTools::replace(s,"\t","\\t")
    sb::add("\"")
    sb::add(s)
    sb::add("\"")
end

#localDecodeObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/com/wiris/util/json/JSon.rb', line 195

def localDecode()
    skipBlanks()
    if c == 123
        return decodeHash()
    else 
        if c == 91
            return decodeArray()
        else 
            if c == 34
                return decodeString()
            else 
                if c == 39
                    return decodeString()
                else 
                    if (c == 45) || ((c >= 48) && (c <= 58))
                        return decodeNumber()
                    else 
                        if ((c == 116) || (c == 102)) || (c == 110)
                            return decodeBooleanOrNull()
                        else 
                            raise Exception,"Unrecognized char " + c.to_s
                        end
                    end
                end
            end
        end
    end
end

#localDecodeString(str) ⇒ Object



191
192
193
194
# File 'lib/com/wiris/util/json/JSon.rb', line 191

def localDecodeString(str)
    init(str)
    return localDecode()
end

#newLine(depth, sb) ⇒ Object



411
412
413
414
415
416
417
418
# File 'lib/com/wiris/util/json/JSon.rb', line 411

def newLine(depth, sb)
    sb::add("\r\n")
    for i in 0..depth - 1
        sb::add("  ")
        i+=1
    end
    @lastDepth = depth
end

#setAddNewLines(addNewLines) ⇒ Object



408
409
410
# File 'lib/com/wiris/util/json/JSon.rb', line 408

def setAddNewLines(addNewLines)
    self.addNewLines = addNewLines
end