0.1.32 • Published 4 years ago

@genie-networks/gcl_gsonjs v0.1.32

Weekly downloads
4
License
Apache-2.0
Repository
github
Last release
4 years ago

gcl_gsonjs

Genie Common Library - GSON js

changeLog

  • 0.1.32 sync with company Library
  • 0.1.7 Chaneg Readme.md syntax
  • 0.1.6 remove Oper.Reg

Install

npm install @genie-networks/gcl_gsonjs

Usage

/* Require gclGson */
const gclGson = require('gcl_gsonjs')

/* Construct value for each type */
let myNum = 3

let myStr = 'hello'

let myBool = true

let myNull = null

let myUndefine

let myIndex = new gclGson.Index(true, false, 100, 1)

let myDate = new Date()

let myPair = new gclGson.Pair('null', 2)

let myOper = new gclGson.Oper(0x10)

let myBin = new gclGson.Bin([1, 2, 3, 4, 5])

let myUuid = new gclGson.Uuid()

let myMac = new gclGson.Mac('0f:ff:03:04:05:06')

let myDerive = new gclGson.Derive(100)

let myIp = new gclGson.Ip([192, 168, 0, 1])

let myIpv6 = new gclGson.Ip('FE00:1234::1')

let myCidr = new gclGson.Cidr('192.168.0.0/16')

let myTag = new gclGson.Tag(0x12345678123456781, false)

let myArr = [1, "s", myTag]

let myPair = new gclGson.Pair('null', 2)

let myDoc = {x:1, y:"str"}

// Get size for serialize buffer
let bufferSize = gclGson.calcSize(myDoc)

// serialize value
let buf = Buffer.alloc(gclGson.calcSize(myDoc), 0x00)
gclGson.serialize(myDoc, buf, 0)

// deserialize
let desDoc = gclGson.deserialize(buf)

// toJSON
let jsonObj = myDoc.toJSON()

// construct a GSON value from a JSON value
let gsonDoc = gclGson.fromJSON(jsonObj)

// Print as GSON format
console.log(gclGson.print(myDoc))

// Print as GSON format without quotation marks
console.log(gclGson.toString(myDoc))

// serialize GSON and import to a file
gclGson.exportFile(myDoc, 'somewhere')

// Import and deserialize GSON from a file
let importDoc = gclGson.importFile('somewhere')

Class

  • gclGsonOper - Operator
  • gclGsonBin - Binary
  • gclGsonTag - Tag
  • gclGsonIndex - Index
  • gclGsonPair - Pair
  • gclGsonUuid - uuid
  • gclGsonMac - MAC
  • gclGsonIp - IPv4/IPv6
  • gclGsonDerive - Derive

Method

  • toJSON(gson_obj)
  • fromJSON(json_obj)
  • calcSize(gson_obj)
  • print(gson_obj)
  • deserialize(buffer)
  • serialize(gson_obj, buffer, offset)
  • exportFile(gson_obj, filePath) - retrun a promise
  • importFile(filePath) - return a promise and resolve gson_obj

SPEC

Type System

  • Simple
    • Undefined represents the unexpected absence of value.
    • Null represents the intentional absence of value.
    • Boolean represents a logical data that can have only the values true or false.
    • Date represents the number of milliseconds since 1 January, 1970 UTC.
    • Number represents a double-precision floating point number.
    • Oper represents a software defined operator.
    • Index represents an 64-bit database index with 2-bit direction flag.
    • String represents a null-terminated UTF-8 string.
    • Bin represents a byte array.
    • Uuid represents universally unique identifier defined by RFC-4122.
    • Mac represents MAC address.
    • Derive represents a derive function ID.
    • Ip represents IPv4 or IPv6 address.
    • Cidr represents IPv4 or IPv6 CIDR.
    • Tag represents tagged id with 1-bit boundary flag.
  • Complex
    • Array represents a sequence of elements, each is a GSON value.
    • Doc represents a sequence of fields, each is a key-value pair.
      • The type of key must be a Number or String.
      • The type of value could be any GSON value.
    • Pair represents a pair of values.

Value Format

EncodingTypeFormatDescription
000 00000Undefined0x00unexpected absence of value
000 00001Null0x01intentional absence of value
000 00010False0x02boolean false
000 00011True0x03boolean true
000 00100Uint80x04 + 1{uint}8-bits non-negative integer number
000 00101Uint160x05 + 2{uint}16-bits non-negative integer number
000 00110Uint320x06 + 4{uint}32-bits non-negative integer number
000 00111Number0x07 + 8{double}double precision floating point number
000 010xxIndex0x08~0x0B + 8{uint)64-bit database index with 2-bit direction flag
000 01100Doc80x0C + 1{len} + *{value}document up to 2^8-1 values
000 01101Doc160x0D + 2{len} + *{value}document up to 2^16-1 values
000 01110Doc320x0E + 4{len} + *{value}document up to 2^32-1 values
000 01111Date0x0F + 8{double}number of milliseconds since 1 January, 1970 UTC
000 10000String80x10 + 1{size} + *{char}string up to 2^8-1 chars
000 10001String160x11 + 2{size} + *{char}string up to 2^16-1 chars
000 10010String320x12 + 4{size} + *{char}string up to 2^32-1 chars
000 10011Oper0x13 + 1{code}operator
000 10100Bin80x14 + 1{size} + *{data}byte array up to 2^8-1 bytes
000 10101Bin160x15 + 2{size} + *{data}byte array up to 2^16-1 bytes
000 10110Bin320x16 + 4{size} + *{data}byte array up to 2^32-1 bytes
000 10111Uuid0x17 + 16{uuid}universal unique id defined by RFC-4122
000 11000Array80x18 + 1{len} + *{value}array up to 2^8-1 values
000 11001Array160x19 + 2{len} + *{value}array up to 2^16-1 values
000 11010Array320x1A + 4{len} + *{value}array up to 2^32-1 values
000 11011Pair0x1B + {value} + {value}pair of values
000 11100Mac0x1C + 6{mac}mac address
000 11101Derive0x1D + 1{uint}lazy derive
000 11110Reserved0x1Ereserved
000 11111Reserved0x1Freserved
001 00000Ip40x20 + 4{ip}ipv4 address
001 00001Ip60x21 + 16{ip}ipv6 address
001 00010Cidr40x22 + 1{uint} + 4{ip}ipv4 CIDR
001 00011Cidr60x23 + 1{uint} + 16{ip}ipv6 CIDR
001 001xxReserved0x24~0x27reserved
001 0100xTag80x28~0x29 + 1{uint}8-bits non-negative integer tag id
001 0101xTag160x2A~0x2B + 2{uint}16-bits non-negative integer tag id
001 0110xTag320x2C~0x2D + 4{uint}32-bits non-negative integer tag id
001 0111xTag520x2E~0x2F + 8{double}52-bits non-negative integer tag id
001 1xxxxReserved0x30~0x3Freserved
010 xxxxxReserved0x40~0x5Freserved
011 xxxxxFixbin0x60~0x7F + *{data}byte array up to 2^5-1 bytes
100 xxxxxFixstring0x80~0x9F + *{char}string up to 2^5-1 bytes
101 xxxxxFixuint0xA0~0xBFunsigned interger up to 2^5-1
110 xxxxxFixarray0xC0~0xDF + *{value}array up to 2^5-1 values
111 xxxxxFixdoc0xE0~0xFF + *{value}document up to 2^5-1 values
  • 1{uint} is 8-bits unsigned integer in host order.
  • 2{uint} is 16-bits unsigned integer in host order.
  • 4{uint} is 32-bits unsigned integer in host order.
  • 8{uint} is 64-bits unsigned integer in host order.
  • 8{double} is 64-bits double precision floating number in host order.
  • 1{code} is 8-bits unsigned integer in host order.
  • 1{len}: is 8-bits unsigned integer indicates number of values.
  • 2{len}: is 16-bits unsigned integer indicates number of values.
  • 4{len}: is 32-bits unsigned integer indicates number of values.
  • 1{size}: is 8-bits unsigned integer indicates number of bytes.
  • 2{size}: is 16-bits unsigned integer indicates number of bytes.
  • 4{size}: is 32-bits unsigned integer indicates number of bytes.
  • *{char} is null-terminated string.
  • *{data} is variable size byte array
  • 16{uuid}: is 128-bits UUID in RFC-4122 format.
  • 6{mac} is 48-bits mac address
  • 4{ip} is 32-bits ipv4 address
  • 16{ip} is 128-bits ipv6 address
  • {value} is a serialized GSON value.
  • *{value} is zero or more serialized GSON values.

Type String

typeformat
Undefined"undefined"
Null"null"
Boolean"boolean"
Date"date"
Number"number"
Oper"oper"
Index"index"
String"string"
Bin"bin"
Uuid"uuid"
Mac"mac"
Derive"derive"
Ip"ip"
Cidr"cidr"
Tag"tag"
Array"array"
Doc"doc"
Pair"pair"

Text Format

typeformat
Undefinedundefined
Nullnull
Booleantrue or false
DateDate("<string>")
Number<number>
Oper$<name> or $<name>(<argument>,...) for expression
IndexIndex("<src>.<dst>.<msb>.<lsb>")
String"<string>"
BinBin("<hexstring>")
UuidUuid("<hexstring>")
MacMac("<macstring>")
DeriveDerive(<number>)
IpIp("<ipstring>")
CidrCidr("<ipstring>")
TagTag("<boundary>.<id>")
Array[<value>,...]
Doc{<key>: <value>,...}
PairPair(<value>, <value>)

JSON Format

typeJSON format
Undefinedundefined
Nullnull
Booleanboolean
Datestring
Numbernumber
Oper"__OPER":<opcode>
Index"__INDEX:{"__DIR":[src, dst], "__VALUE":[hi, lo]}"
String"<string>"
Bin"__BIN":<hex string>
Uuid"__UUID":<hex string>
Mac"__MAC":<macstring>
Ip"__IP":<ipstring>
Cidr"__CIDR":<cidrstring>
Tag"__TAG:{"__BOUNDARY":boolean, "__VALUE":number}"
Derive"__DERIVE":<number>
Arrayarray
Docobject which keys are string
Pair"__PAIR":[<src>, <dst>]

JavaScript Implement

  • simple
    • Undefined is mapped to the standard value undefined.
    • Null is mapped to the standard value null.
    • Boolean is mapped to the standard class Boolean.
    • Date is mapped to the standard class Date.
    • Number is mapped to the standard class Number.
    • Oper is mapped to the proprietry class gclGsonOper.
    • Index is mapped to the proprietry class gclGsonIndex.
    • String is mapped to the standard class String.
    • Bin is mapped to the proprietry class gclGsonBin.
    • Uuid is mapped to the proprietry class gclGsonUuid.
    • Mac is mapped to the proprietry class gclGsonMac.
    • Ip is mapped to the proprietry class gclGsonIp.
    • Cidr is mapped to the proprietry class gclGsonCidr.
    • Tag is mapped to the proprietry class gclGsonTag.
    • Derive is mapped to the proprietry class gclGsonDerive.
  • complex

    • Array is map to the standard class Array.
    • Doc is map to the standard class Object.
    • Pair is map to the proprietry class gclGsonPair.