Advertisement
dragonbane

ZSR Binary JSON Spec

Nov 18th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. //Packed format is created as a node buffer on node, but still uses DataView. First loop over template and check arrays to calculate the max data size before creating buffer. Browser uses DataView and a main Uint8Array
  2.  
  3. //Packed message format
  4. 0 = main event (e.g. Stream maps to 0)
  5. 1 = sub event (e.g. Get maps to 0 or 1)
  6. 2/3 = 2 byte UInt16 nonce follows if descriptor requires one or field is skipped if none needed (max nonce is thus 65535)
  7.  
  8. //can start recursive here
  9. 4 = var type and size
  10.  
  11. Types: 0 = null or undefined (size has to be 0 for this too!), 1 = bool, 2 = int, 3 = float, 4 = string, 5 = object, 6 = array with regular members, 7 = raw buffer data
  12.  
  13. size = 0 (bool = false, 1 byte = object always, array/byte/packed float as byte/string or buffer with 1 byte length), 16 (bool = true, 2 byte = e.g. half float packed or 2 byte length for array/string/etc.), 32 (4 byte unsigned or float or 4 byte length), 64 (8 byte unsigned or double). 128 (1 byte signed/negative, 1 byte negative packed float), 144 (2 byte signed/negative, 2 byte negative packed float), 160 (4 byte signed/negative), 192 (8 byte signed/negative). Negative doesn't necessarily mean that the result will fit into its type still, e.g. a negative value can be stored unsigned as 1 byte, but signed will only fit into an int16 (the descriptor is important to consult here!)
  14.  
  15. On encode just add the size and type
  16. On decode AND with 0xF0 (-16) for size AND with 0xF (15) for type
  17.  
  18. Type and type size (or length of type) is known now no matter what
  19.  
  20. 5/6/8 = size //skipped if type bool, int, float and null. Otherwise for object always 1 byte. For array, string types and raw buffer use 1/2/4 bytes length
  21. value follows in whole (unless empty array/object/string/buffer or null/bool which value is encoded into the size field)
  22.  
  23. //if object
  24. //per param
  25. 6 = param index (always 1 byte due objects being limited to 1 byte, skipped if inside an array)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement