Libraries.Data.Formats.JavaScriptObjectNotationLexer Documentation

This class is used by the JavaScriptObjectNotation object to read in JavaScriptObjectNotation (JSON) formatted data from text values or from disk. It can be used directly by an advanced user who wants to perform custom reading of token data from a file.

Example Code

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Data.Formats.JavaScriptObjectNotationDefaultListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer
use Libraries.Data.Formats.JavaScriptObjectNotationParser

private action ReadToObject(text value) returns JavaScriptObjectNotation
if value = undefined
    return undefined
end
JavaScriptObjectNotationDefaultListener listen
JavaScriptObjectNotationLexer lex
JavaScriptObjectNotationParser parse

lex:SetListener(listen)
parse:SetListener(listen)
lex:Read(value)
parse:Parse(lex)
return listen:GetObject()
end

Inherits from: Libraries.Language.Object

Variables Table

VariablesDescription
integer STRING
integer LEFT_BRACE
integer NULLThe raw tokens from the lex.
integer TRUE
integer COMMA
integer FALSE
integer INTEGER
integer NUMBER
integer COLON
integer RIGHT_BRACE
integer LEFT_BRACKET
integer RIGHT_BRACKET

Actions Documentation

Compare(Libraries.Language.Object object)

This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.

Parameters

Return

integer: The Compare result, Smaller, Equal, or Larger.

Example

Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)

Empty()

This method empties the list of tokens.

Example

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Data.Formats.JavaScriptObjectNotationDefaultListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer
use Libraries.Data.Formats.JavaScriptObjectNotationToken

private action EmptyTokens(text value)
    if value = undefined
        return undefined
    end
    JavaScriptObjectNotationDefaultListener listen
    JavaScriptObjectNotationLexer lex
    JavaScriptObjectNotationParser parse

    lex:SetListener(listen)
    parse:SetListener(listen)
    lex:Read(value)
    lex:Empty()

Equals(Libraries.Language.Object object)

This action determines if two objects are equal based on their hash code values.

Parameters

Return

boolean: True if the hash codes are equal and false if they are not equal.

Example

use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)

GetHashCode()

This action gets the hash code for an object.

Return

integer: The integer hash code of the object.

Example

Object o
integer hash = o:GetHashCode()

GetListener()

This method is used to get the JavaScriptObjectNotationListener object used by the lexer.

Return

Libraries.Data.Formats.JavaScriptObjectNotationListener: The JavaScriptObjectNotationListener.

Example

use Libraries.Data.Formats.JavaScriptObjectNotationListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer

action GetParserListener(JavaScriptObjectNotationLexer lex) returns JavaScriptObjectNotationListener
    return lex:GetListener()
end

GetSize()

This method returns the number of tokens in the token stream.

Return

integer: The number of tokens in the stream.

Example

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Data.Formats.JavaScriptObjectNotationDefaultListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer
use Libraries.Data.Formats.JavaScriptObjectNotationToken

private action GetNumberOfTokens(text value) returns integer
    if value = undefined
        return undefined
    end
    JavaScriptObjectNotationDefaultListener listen
    JavaScriptObjectNotationLexer lex
    JavaScriptObjectNotationParser parse

    lex:SetListener(listen)
    parse:SetListener(listen)
    lex:Read(value)
    return lex:GetSize()

GetToken(integer i)

This method returns a token at a particular position in the lex.

Parameters

  • integer i: The index of the JavaScriptObjectNotationToken to get.

Return

Libraries.Data.Formats.JavaScriptObjectNotationToken: The JavaScriptObjectNotationToken.

Example

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Data.Formats.JavaScriptObjectNotationDefaultListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer
use Libraries.Data.Formats.JavaScriptObjectNotationToken

JavaScriptObjectNotationDefaultListener listener

action Start
    listener:EnterStart()
    currentToken = 0
    JavaScriptObjectNotationToken token = lexer:GetToken(currentToken)
    if token:tokenID = lexer:LEFT_BRACKET
        ParseObject()
    else 
        ThrowError(token, undefined, "I can only begin reading files that start with a left bracket")
    end
    listener:ExitStart()
end

IsEmpty()

This method Returns whether or not there are tokens in the token stream

Return

boolean: A boolean value to indicate if the stream is empty.

Example

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Data.Formats.JavaScriptObjectNotationDefaultListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer
use Libraries.Data.Formats.JavaScriptObjectNotationToken

private action ReadToObject(text value)
    if value = undefined
        return undefined
    end
    JavaScriptObjectNotationDefaultListener listen
    JavaScriptObjectNotationLexer lex
    JavaScriptObjectNotationParser parse

    lex:SetListener(listen)
    parse:SetListener(listen)
    lex:Read(value)
    if lex:IsEmpty()
        output "No tokens"
    else
        parse:Parse(lex)
    end
end

Read(text value)

This method is used to read a text value and converts it into a token strea.

Parameters

  • text value: The text value to lex.

Example

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Data.Formats.JavaScriptObjectNotationDefaultListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer
use Libraries.Data.Formats.JavaScriptObjectNotationParser

private action ReadToObject(text value) returns JavaScriptObjectNotation
    if value = undefined
        return undefined
    end
    JavaScriptObjectNotationDefaultListener listen
    JavaScriptObjectNotationLexer lex
    JavaScriptObjectNotationParser parse

    lex:SetListener(listen)
    parse:SetListener(listen)
    lex:Read(value)
    parse:Parse(lex)
    return listen:GetObject()
end

Read(Libraries.System.File file)

This method is used to read a text file and converts it into a token strea.

Parameters

Example

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Data.Formats.JavaScriptObjectNotationDefaultListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer
use Libraries.Data.Formats.JavaScriptObjectNotationParser

private action ReadToObject(File file) returns JavaScriptObjectNotation
    JavaScriptObjectNotationDefaultListener listen
    JavaScriptObjectNotationLexer lex
    JavaScriptObjectNotationParser parse

    lex:SetListener(listen)
    parse:SetListener(listen)
    lex:Read(file)
    parse:Parse(lex)
    return listen:GetObject()
end

SetListener(Libraries.Data.Formats.JavaScriptObjectNotationListener listener)

This method is used to set the JavaScriptObjectNotationListener object used by the lexer.

Parameters

Example

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Data.Formats.JavaScriptObjectNotationDefaultListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer
use Libraries.Data.Formats.JavaScriptObjectNotationParser

private action ReadToObject(text value) returns JavaScriptObjectNotation
    if value = undefined
        return undefined
    end
    JavaScriptObjectNotationDefaultListener listen
    JavaScriptObjectNotationLexer lex
    JavaScriptObjectNotationParser parse

    lex:SetListener(listen)
    parse:SetListener(listen)
    lex:Read(value)
    parse:Parse(lex)
    return listen:GetObject()
end

ThrowError(integer line, integer column, integer index, text message)

This method handles error message throwing if something goes wrong.

Parameters

  • integer line: The message to throw.
  • integer column
  • integer index
  • text message

Example

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Data.Formats.JavaScriptObjectNotationDefaultListener
use Libraries.Data.Formats.JavaScriptObjectNotationLexer
use Libraries.Data.Formats.JavaScriptObjectNotationParser

JavaScriptObjectNotationDefaultListener listen

private action Start
    listener:EnterStart()
    currentToken = 0
    JavaScriptObjectNotationToken token = lexer:GetToken(currentToken)
    if token:tokenID = lexer:LEFT_BRACKET
        ParseObject()
    elseif token:tokenID = lexer:LEFT_BRACE
        ParseArray()
    else 
        ThrowError(token, undefined, "I can only begin reading files that start with a left bracket ({) or a left brace {[), but I found a, " + token:value)
    end
    listener:ExitStart()
end