write-msgpack ( obj -- ) (original) (raw)

write-msgpack ( obj -- )
MessagePack

Prev: msgpack> ( seq -- obj )
Next: >msgpack ( obj -- bytes )

Vocabulary
msgpack

Inputs

obj an object

Outputs
None

Word description
Encodes an object into the MessagePack format, writing to an output-stream.

Definition

IN: msgpack

GENERIC: write-msgpack ( obj -- )

Methods

USING: io kernel msgpack ;

M: +msgpack-nil+ write-msgpack drop 192 write1 ;

USING: assocs kernel msgpack msgpack.private ;

M: assoc write-msgpack
dup assoc-size write-map-header
[ [ write-msgpack ] bi@ ] assoc-each ;

USING: byte-arrays combinators endian io kernel math msgpack
sequences ;

M: byte-array write-msgpack
dup length {
{ [ dup 255 <= ] [ 196 write1 write1 ] }
{ [ dup 65535 <= ] [ 197 write1 2 >be write ] }
{ [ dup 4294967295 <= ] [ 198 write1 4 >be write ] }
[ cannot-convert ]
} cond write ;

USING: io kernel msgpack ;

M: f write-msgpack drop 194 write1 ;

USING: endian io math msgpack ;

M: float write-msgpack 203 write1 double>bits 8 >be write ;

USING: combinators endian io kernel math msgpack ;

M: integer write-msgpack
dup 0 >= [
{
{ [ dup 127 <= ] [ write1 ] }
{ [ dup 255 <= ] [ 204 write1 write1 ] }
{ [ dup 65535 <= ] [ 205 write1 2 >be write ] }
{ [ dup 4294967295 <= ] [ 206 write1 4 >be write ] }
{
[ dup 18446744073709551615 <= ]
[ 207 write1 8 >be write ]
}
[ cannot-convert ]
} cond
] [
{
{ [ dup -31 >= ] [ write1 ] }
{ [ dup -128 >= ] [ 208 write1 write1 ] }
{ [ dup -32768 >= ] [ 209 write1 2 >be write ] }
{
[ dup -2147483648 >= ]
[ 210 write1 4 >be write ]
}
{
[ dup -9223372036854775808 >= ]
[ 211 write1 8 >be write ]
}
[ cannot-convert ]
} cond
] if ;

USING: accessors arrays kernel msgpack msgpack.rpc ;

M: notification write-msgpack
2 swap [ method>> ] [ params>> ] bi 3array write-msgpack ;

USING: accessors arrays kernel msgpack msgpack.rpc ;

M: request write-msgpack
0 swap [ msgid>> ] [ method>> ] [ params>> ] tri 4array
write-msgpack ;

USING: accessors arrays kernel msgpack msgpack.rpc ;

M: response write-msgpack
1 swap [ msgid>> ] [ error>> ] [ result>> ] tri 4array
write-msgpack ;

USING: kernel msgpack msgpack.private sequences ;

M: sequence write-msgpack
dup length write-array-header [ write-msgpack ] each ;

USING: combinators endian io io.encodings io.encodings.utf8
kernel math msgpack namespaces sequences strings ;

M: string write-msgpack
dup length {
{ [ dup 31 <= ] [ 160 bitor write1 ] }
{ [ dup 255 <= ] [ 217 write1 write1 ] }
{ [ dup 65535 <= ] [ 218 write1 2 >be write ] }
{ [ dup 4294967295 <= ] [ 219 write1 4 >be write ] }
[ cannot-convert ]
} cond output-stream get utf8 encode-string ;

USING: io kernel msgpack ;

M: t write-msgpack drop 195 write1 ;