serializer.jsonstream - Palamecia/mint GitHub Wiki
Module
load serializer.jsonstream
This module provides the Serializer.DataStream class which allow to read and write JSON documents.
Packages
Classes
Serializer.JsonStream
This class class provides a way to read and write JSON documents.
Inherits
Public members
Modifiers | Member | Description |
---|---|---|
override const |
<< | Writes the content of data to the stream using the write function, then ret... |
override const |
>> | Reads an object from the stream and stores it in data , then returns a refer... |
@ const |
escapeString | Returns the content of str with special characters escaped to be inserted i... |
const |
isIndented | Returns true if the stream indent the JSON when writing; otherwise returns ... |
override const |
read | Reads the content of the stream and return it as an hash. If the content of t... |
const |
setIndented | Enables the indentation of the JSON writed to the stream. If indented is ``... |
override const |
write | Writes the content of data to the stream. The indentation mode set by setIn... |
Private members
Modifiers | Member | Description |
---|---|---|
class |
Parser | This class provides a parser to convert a JSON stream to built-in types. The ... |
final |
indentationLevel | Internal current indentation level. |
final |
indented | Internal indentation format state. |
final const |
valueToJson | Converts the value element to JSON as follow: * hashes are converted to JSO... |
final const |
writeJson | Writes the element value to the stream with the current indentation. The el... |
Serializer.JsonStream.Parser
This class provides a parser to convert a JSON stream to built-in types.
The elements are converted as follow:
- JSON objects are converted to hashes
- JSON arrays are converted to arrays
- JSON boolean values are converted to booleans
- JSON numeric values are converted to numbers
- JSON string values are converted to strings
- JSON null value is converted to
null
Public members
Modifiers | Member | Description |
---|---|---|
const |
new | Creates a new parser on the stream given by stream . |
const |
parse | Returns an instance of hash or array corresponding to the next JSON element i... |
Private members
Modifiers | Member | Description |
---|---|---|
final |
c | Internal last read character. |
final const |
discardToken | Discards the last token that was read. The same token will then be returned b... |
final |
discarded | Internal discarded state. |
final const |
nextChar | Returns the next character of the stream. |
final const |
nextToken | Returns the next token of the stream. A token is a character that does not ma... |
final const |
parseNextArray | Returns the value converted from the next array of the stream. An instance of... |
final const |
parseNextElement | Returns the value converted from the next element of the stream. An instance ... |
final const |
parseNextObject | Returns the value converted from the next object of the stream. An instance o... |
final const |
parseNextString | Returns the value converted from the next string of the stream. An instance o... |
final const |
parseNextValue | Returns the value converted from the next value of the stream. An instance of... |
final |
stream | Internal stream object. |
Descriptions
Serializer.JsonStream.<<
def (self, data)
Writes the content of data
to the stream using the write
function, then returns a reference to the stream.
Serializer.JsonStream.>>
def (self, data)
Reads an object from the stream and stores it in data
, then returns a
reference to the stream.
Serializer.JsonStream.Parser.c
none
Internal last read character.
Serializer.JsonStream.Parser.discardToken
def (self)
Discards the last token that was read. The same token will then be returned by the next call to nextToken.
Serializer.JsonStream.Parser.discarded
false
Internal discarded state.
Serializer.JsonStream.Parser.new
def (self, stream)
Creates a new parser on the stream given by stream
.
Serializer.JsonStream.Parser.nextChar
def (self)
Returns the next character of the stream.
Serializer.JsonStream.Parser.nextToken
def (self)
Returns the next token of the stream. A token is a character that does
not match the /\s/
regex.
Serializer.JsonStream.Parser.parse
def (self)
Returns an instance of hash or array corresponding to the next JSON element in the stream.
An instance of Serializer.DocumentStream.InvalidDocument is raised on error.
Serializer.JsonStream.Parser.parseNextArray
def (self)
Returns the value converted from the next array of the stream.
An instance of Serializer.DocumentStream.InvalidDocument is raised on error.
Serializer.JsonStream.Parser.parseNextElement
def (self)
Returns the value converted from the next element of the stream.
An instance of Serializer.DocumentStream.InvalidDocument is raised on error.
Serializer.JsonStream.Parser.parseNextObject
def (self)
Returns the value converted from the next object of the stream.
An instance of Serializer.DocumentStream.InvalidDocument is raised on error.
Serializer.JsonStream.Parser.parseNextString
def (self)
Returns the value converted from the next string of the stream.
An instance of Serializer.DocumentStream.InvalidDocument is raised on error.
Serializer.JsonStream.Parser.parseNextValue
def (self, token)
Returns the value converted from the next value of the stream.
An instance of Serializer.DocumentStream.InvalidDocument is raised on error.
Serializer.JsonStream.Parser.stream
null
Internal stream object.
Serializer.JsonStream.escapeString
def (str)
Returns the content of str
with special characters escaped to be
inserted in a JSON document.
Serializer.JsonStream.indentationLevel
0
Internal current indentation level.
Serializer.JsonStream.indented
true
Internal indentation format state.
Serializer.JsonStream.isIndented
def (const self)
Returns true
if the stream indent the JSON when writing; otherwise
returns false
.
Serializer.JsonStream.read
def (self)
Reads the content of the stream and return it as an hash. If the content of the stream is not a valid JSON, an instance of Serializer.DocumentStream.InvalidDocument is raised.
Serializer.JsonStream.setIndented
def (self, indented)
Enables the indentation of the JSON writed to the stream. If indented
is true
, the JSON will be indented; otherwise the JSON will be
compact.
Indented example:
{
"array": [
{
"object": {
"str": "value",
"int": 32,
"bool": false,
"null": null
}
}
]
}
Compact example:
{"array": [{"object": {"str": "value","int": 32,"bool": false,"null": null}}]}
By default, the data is indented.
Serializer.JsonStream.valueToJson
def (self, value)
Converts the value
element to JSON as follow:
- hashes are converted to JSON objects.
- arrays are converted to JSON arrays.
- numbers are converted to JSON numeric values.
- strings are converted to JSON string values using escapeString.
- booleans are converted to JSON boolean values.
null
is converted to JSON null value.- for other types, the conversion methods are tested in the following
order:
toBoolean
,toNumber
,toString
. If no conversion method is found, the data is cast to string.
Serializer.JsonStream.write
def (self, data)
Writes the content of data
to the stream. The indentation mode set by
setIndented is used to adapt the format. The content is converted
to JSON using the valueToJson function.
Serializer.JsonStream.writeJson
def (self, value)
Writes the element value
to the stream with the current indentation.
The element is converted to JSON using valueToJson.