class WirisPlugin::JSon

Attributes

addNewLines[RW]
depth[RW]
lastDepth[RW]

Public Class Methods

compare(a, b, eps) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 187
def self.decode(str)
    json = JSon.new()
    return json::localDecodeString(str)
end
encode(o) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 23
def self.encode(o)
    js = JSon.new()
    return js::encodeObject(o)
end
getArray(a) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 447
def self.getArray(a)
    return (a)
end
getBoolean(b) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 444
def self.getBoolean(b)
    return ((b))::booleanValue()
end
getDepth(o) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 450
def self.getHash(a)
    return (a)
end
getInt(n) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 419
def self.getString(o)
    return (o)
end
new() click to toggle source
Calls superclass method
# File lib/com/wiris/util/json/JSon.rb, line 20
def initialize()
    super()
end
sb() click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 11
def self.sb
    @@sb
end
sb=(sb) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 14
def self.sb=(sb)
    @@sb = sb
end

Public Instance Methods

decodeArray() click to toggle source
# 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
decodeBooleanOrNull() click to toggle source
# 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
decodeHash() click to toggle source
# 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
decodeNumber() click to toggle source
# 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
decodeString() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 175
def encodeBoolean(sb, b)
    sb::add(b::booleanValue() ? "true" : "false")
end
encodeFloat(sb, d) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 178
def encodeFloat(sb, d)
    sb::add(TypeTools::floatToString(d))
end
encodeHash(sb, h) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 172
def encodeInteger(sb, i)
    sb::add("" + i.to_s)
end
encodeIntegerFormat(sb, i) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 184
def encodeIntegerFormat(sb, i)
    sb::add(i::toString())
end
encodeLong(sb, i) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 181
def encodeLong(sb, i)
    sb::add("" + i.to_s)
end
encodeObject(o) click to toggle source
# 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) click to toggle source
# 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
localDecode() click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 191
def localDecodeString(str)
    init(str)
    return localDecode()
end
newLine(depth, sb) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/json/JSon.rb, line 408
def setAddNewLines(addNewLines)
    self.addNewLines = addNewLines
end