├── .gitignore ├── LICENSE ├── PSON.png ├── PSONspec.txt ├── README.md ├── dicts ├── README.md ├── empty.json └── package.json ├── dist ├── PSON.js └── PSON.min.js ├── docs ├── PSON.Decoder.html ├── PSON.Encoder.html ├── PSON.Pair.html ├── PSON.ProgressivePair.html ├── PSON.StaticPair.html ├── PSON.T.html ├── PSON.html ├── index.html ├── module-PSON.html ├── scripts │ ├── html5.js │ └── prettify │ │ ├── Apache-License-2.0.txt │ │ ├── lang-css.js │ │ └── prettify.js └── styles │ ├── jsdoc-esoccer.css │ ├── prettify-jsdoc.css │ └── prettify-tomorrow.css ├── index.js ├── jsdoc.json ├── package.json ├── src ├── PSON.js └── PSON │ ├── Decoder.js │ ├── Encoder.js │ ├── Pair.js │ ├── ProgressivePair.js │ ├── StaticPair.js │ └── T.js └── tests └── suite.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | .idea 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /PSON.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dcodeIO/PSON/be3bb7c10260c4bf7d57aec57e98937d950e938d/PSON.png -------------------------------------------------------------------------------- /PSONspec.txt: -------------------------------------------------------------------------------- 1 | Working Draft D. Wirtz 2 | Version 2 July 2013 3 | 4 | Protocol JSON - PSON 5 | 6 | Status of This Memo 7 | 8 | This memo provides information for the Internet community. It does 9 | not specify an Internet standard of any kind. Distribution of this 10 | memo is unlimited. 11 | 12 | Copyright Notice 13 | 14 | Copyright (c) 2013 Daniel Wirtz 15 | 16 | Abstract 17 | 18 | Protocol JSON (PSON) is a lightweight, binary, language-independent 19 | data interchange format. PSON defines a set of encoding rules for 20 | the portable representation of structured data. 21 | 22 | 1. Introduction 23 | 24 | Protocol JSON (PSON) is a binary format for the serialization of 25 | structured data. It is derived from JavaScript Object Notation 26 | (JSON), as defined in [RFC4627]. 27 | 28 | PSON can represent five primitive types (strings, numbers, booleans, 29 | null and raw bytes) and two structured types (objects and arrays). 30 | 31 | A string is a sequence of zero or more UTF-8 characters [UNICODE]. 32 | 33 | An object is an unordered collection of zero or more name/value 34 | pairs, where a name is a string and a value is a string, number, 35 | boolean, null, object or array. 36 | 37 | An array is an ordered sequence of zero or more values. 38 | 39 | The terms "object" and "array" come from the conventions of 40 | JavaScript. 41 | 42 | A variable length integer (varint) is a base 128 variable length 43 | integer as described in the Encoding section of the Protocol Buffers 44 | (protobuf) developer guide. 45 | 46 | PSON's design goals were for it to be small, portable, binary and 47 | a superset of JSON. 48 | 49 | 1.1. Conventions Used in This Document 50 | 51 | The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 52 | "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this 53 | document are to be interpreted as described in [RFC2119]. 54 | 55 | The grammatical rules in this document are to be interpreted as 56 | described in [RFC4234]. 57 | 58 | 2. PSON Grammar 59 | 60 | PSON data is a sequence of tokens, varints and arbitrary bytes. 61 | 62 | 2.1 Values 63 | 64 | A PSON value MUST start with a token and MAY be followed by 65 | 66 | a zig-zag encoded varint 67 | 68 | or 69 | 70 | an unsigned varint determining the length of arbitrary byte data 71 | following 72 | 73 | or 74 | 75 | an unsigned varint determining the number of PSON values 76 | following. 77 | 78 | No other combinations are allowed. 79 | 80 | There are 256 tokens: 81 | 82 | ZERO = %x00 ; 0 83 | NEGONE = %x01 ; -1 84 | ONE = %x02 ; +1 85 | ... 86 | MAX = %xEF ; -120 87 | 88 | NULL = %xF0 89 | TRUE = %xF1 90 | FALSE = %xF2 91 | EOBJECT = %xF3 92 | EARRAY = %xF4 93 | ESTRING = %xF5 94 | OBJECT = %xF6 ; + varint32 + key/values 95 | ARRAY = %xF7 ; + varint32 + values 96 | INTEGER = %xF8 ; + varint32 97 | LONG = %xF9 ; + varint64 98 | FLOAT = %xFA ; + float32 99 | DOUBLE = %xFB ; + float64 100 | STRING = %xFC ; + varint32 + bytes 101 | STRING_ADD = %xFD ; + varint32 + bytes 102 | STRING_GET = %xFE ; + varint32 103 | BINARY = %xFF ; + varint32 + bytes 104 | 105 | 2.2. Boolean 106 | 107 | A boolean value evaluating to true MUST be encoded as the token: 108 | 109 | TRUE = %xF1 110 | 111 | A boolean value evaluating to false MUST be encoded as the token: 112 | 113 | FALSE = %xF2 114 | 115 | 2.2. Numbers 116 | 117 | 2.2.1. Integer 118 | 119 | Integer values greater than or equal -120 and less than or equal 120 | 119 SHOULD be encoded as a token / single byte beginning at 121 | 122 | ZERO = %x00 ; 0 123 | 124 | and ending at 125 | 126 | MAX = %0xEF ; -120 127 | 128 | corresponding to the value's zig-zag encoded varint representation. 129 | 130 | Otherwise and values less than -120 or greater than 119 MUST be 131 | encoded as the token 132 | 133 | INTEGER = %xF8 134 | 135 | followed by its value as a zig-zag encoded 32 bit varint. 136 | 137 | If an integer value exceeds 32 bits of information and thus does not 138 | fit into a zig-zag encoded 32 bit varint, it SHOULD be encoded as 139 | the token 140 | 141 | LONG = %xF9 142 | 143 | followed by its value as a zig-zag encoded 64 bit varint or MUST be 144 | reduced to 32 bits otherwise which MAY rise a warning. 145 | 146 | 2.2.2. Floating point 147 | 148 | A 32 bit float SHOULD be encoded as the token 149 | 150 | FLOAT = %xFA 151 | 152 | followed by the little endian 32 bit float value. 153 | 154 | Otherwise and a 64 bit double precision float MUST be encoded as 155 | the token 156 | 157 | DOUBLE = %xFB 158 | 159 | followed by the little endian 64 bit float value. 160 | 161 | If a 64 bit float can be converted to a 32 bit float without losing 162 | any information, it SHOULD be encoded as a 32 bit float instead. 163 | 164 | If a float can be converted to an integer without losing any 165 | information, it SHOULD be encoded as an integer. 166 | 167 | 2.2. Arrays 168 | 169 | An array with zero elements SHOULD be encoded as the token 170 | 171 | EARRAY = %xF4 172 | 173 | Otherwise and arrays with one or more elements MUST be encoded as 174 | the token 175 | 176 | ARRAY = %xF7 177 | 178 | followed by the number of elements as an unsigned 32 bit varint 179 | followed by all elements as a PSON encoded value. 180 | 181 | If a value evaluates to the JavaScript constant 182 | 183 | undefined 184 | 185 | it must instead be encoded as the token: 186 | 187 | NULL = %xF0 188 | 189 | 2.3. Objects 190 | 191 | An object evaluating to the JavaScript constant 192 | 193 | null 194 | 195 | MUST be encoded as the token: 196 | 197 | NULL = %xF0 198 | 199 | An object with zero key/value pairs SHOULD be encoded as the token: 200 | 201 | EOBJECT = %xF3 202 | 203 | Otherwise it and objects with one or more key/value pairs MUST be 204 | encoded as the token 205 | 206 | OBJECT = %xF6 207 | 208 | followed by the number of key/value pairs as an unsigned 32 bit 209 | varint followed by the alternating keys and values as PSON encoded 210 | values. 211 | 212 | If a value inside of an object evaluates to the JavaScript constant 213 | 214 | undefined 215 | 216 | the corresponding key/value pair MUST be omitted. 217 | 218 | Order of key/value pairs SHOULD be preserved if supported by the 219 | language runtime. 220 | 221 | 2.4. Strings 222 | 223 | A string with zero characters SHOULD be encoded as the token: 224 | 225 | ESTRING = %xF5 226 | 227 | Otherwise it and strings with one or more characters MUST be encoded 228 | as the token 229 | 230 | STRING = %xFC 231 | 232 | followed by the number of raw bytes as an unsigned 32 bit varint 233 | followed by the UTF-8 encoded raw bytes. 234 | 235 | 2.5. Binary data 236 | 237 | Binary data MUST be encoded as the token 238 | 239 | BINARY = %xFF 240 | 241 | followed by the number of raw bytes as an unsigned 32 bit varint 242 | followed by the raw bytes. 243 | 244 | 2.6. undefined 245 | 246 | In PSON there is no token for a value that equals the JavaScript 247 | constant 248 | 249 | undefined 250 | 251 | and a value evaluating to undefined MUST either be skipped if it is 252 | a value inside of an object or, otherwise, be encoded as if it would 253 | equal the JavaScript constant: 254 | 255 | null 256 | 257 | 3. Dictionaries 258 | 259 | 3.1. Progressive substitution 260 | 261 | In addition to encoding strings as defined in 2.4, strings SHOULD 262 | also be stored in a dictionary if requested by the application on 263 | the encoding side. 264 | 265 | A string that is not yet present in the dictionary SHOULD be added 266 | to the dictionary on the encoding side. If a key is added to the 267 | dictionary on the encoding side, it MUST be assigned the value of 268 | the number of elements contained in the dictionary before the value 269 | has been added (index) and, instead of being encoded like in 2.4, 270 | be encoded as the token 271 | 272 | STRING_ADD = %0xFD 273 | 274 | followed by the number of raw bytes as an unsigned 32 bit varint 275 | followed by the UTF-8 encoded raw bytes. 276 | 277 | When the decoding side decodes a string that has been encoded in 278 | this way, it MUST add the value to its dictionary and assign it 279 | the value of the number of elements contained in the dictionary 280 | before the value has been added (index). 281 | 282 | A string that has previously been added to the dictionary SHOULD, 283 | instead of being encoded as in 2.4, be encoded as the token 284 | 285 | STRING_GET = %0xFE 286 | 287 | followed by the previously assigned index as an unsigned 32 bit 288 | varint. 289 | 290 | When the decoding side decodes a string that has been encoded 291 | in this way, it MUST look up the index in the dictionary and 292 | return the remembered string value instead. 293 | 294 | 3.2. Static substitution 295 | 296 | In addition to adding string values to the dictionary as defined in 297 | 3.1, the initial dictionary MAY be negotiated between the encoding 298 | and the decoding side prior to encoding/decoding any values. 299 | 300 | If the encoding side uses static substitution, the decoding side MUST 301 | use the same dictionary entries in the same order. 302 | 303 | 4. Encoding 304 | 305 | All floating point values MUST be encoded in little endian byte 306 | order. 307 | 308 | All string values MUST be encoded as UTF-8. 309 | 310 | 5. Decoding 311 | 312 | A decoder MUST be able to process all data types defined in this 313 | document. It SHOULD return the corresponding values if available in 314 | the language runtime and MAY rise a warning otherwise. 315 | 316 | 6. MIME media type 317 | 318 | The MIME media type for PSON is application/octet-stream. 319 | 320 | Author's Address 321 | 322 | Daniel Wirtz 323 | dcode.io 324 | EMail: dcode@dcode.io 325 | 326 | Full Copyright Statement 327 | 328 | Copyright 2013 Daniel Wirtz 329 | 330 | Licensed under the Apache License, Version 2.0 (the "License"); 331 | you may not use this file except in compliance with the License. 332 | You may obtain a copy of the License at 333 | 334 | http://www.apache.org/licenses/LICENSE-2.0 335 | 336 | Unless required by applicable law or agreed to in writing, software 337 | distributed under the License is distributed on an "AS IS" BASIS, 338 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 339 | See the License for the specific language governing permissions and 340 | limitations under the License. 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![PSON](https://raw.github.com/dcodeIO/PSON/master/PSON.png) 2 | ==== 3 | **PSON** is a super efficient binary serialization format for JSON focused on minimal encoding size. 4 | 5 | How does it work? 6 | ----------------- 7 | PSON combines the best of JSON, BJSON, ProtoBuf and a bit of ZIP to achieve a superior small footprint on the network 8 | level. Basic constants and small integer values are efficiently encoded as a single byte. Other integer values are always 9 | encoded as variable length integers. Additionally it comes with progressive and static dictionaries to reduce data 10 | redundancy to a minimum. In a nutshell: 11 | 12 | * 246 single byte values 13 | * Base 128 variable length integers (varints) as in protobuf 14 | * 32 bit floats instead of 64 bit doubles if possible without information loss 15 | * Progressive and static dictionaries 16 | * Raw binary data support 17 | * Long support 18 | 19 | Reference implementation 20 | ------------------------ 21 | This repository contains a plain **node.js/CommonJS, RequireJS/AMD and Browser compatible** JavaScript implementation 22 | of the [PSON specification](https://github.com/dcodeIO/PSON/blob/master/PSONspec.txt) on top of 23 | [ByteBuffer.js](https://github.com/dcodeIO/ByteBuffer.js) and optionally [Long.js](https://github.com/dcodeIO/Long.js): 24 | 25 | A **PSON.StaticPair** contains the PSON encoder and decoder for a static (or empty) dictionary and can be shared between 26 | all connections. It's recommended for production. 27 | 28 | A **PSON.ProgressivePair** contains the PSON encoder and decoder for a progressive (automatically filling) dictionary. 29 | On the one hand this requires no dictionary work from the developer but on the other requires one pair per connection. 30 | 31 | tl;dr Numbers, please! 32 | ---------------------- 33 | The test suite contains the following basic example message: 34 | 35 | ```json 36 | { 37 | "hello": "world!", 38 | "time": 1234567890, 39 | "float": 0.01234, 40 | "boolean": true, 41 | "otherbool": false, 42 | "null": null, 43 | "obj": { 44 | "what": "that" 45 | }, 46 | "arr": [1,2,3] 47 | } 48 | ``` 49 | 50 | * **JSON** stringify: 133 bytes 51 | * **PSON** without a dictionary: 103 bytes (about **22% smaller** than JSON) 52 | * **PSON** with a progressive dictionary: 103 bytes for the first and 59 bytes for each subsequent message (about 53 | **22% smaller** for the first and about **55% smaller** for each subsequent message than JSON. 54 | * **PSON** with the same but static dictionary: 59 bytes for each message (about **55% smaller** than JSON) 55 | 56 | ```text 57 | F6 08 FE 00 FC 06 77 6F 72 6C 64 21 FE 01 F8 A4 ......world!.... 58 | 8B B0 99 79 FE 02 FB F6 0B 76 C3 B6 45 89 3F FE ...y.....v..E.?. 59 | 03 F1 FE 04 F2 FE 05 F0 FE 06 F6 01 FE 07 FC 04 ................ 60 | 74 68 61 74 FE 08 F7 03 02 04 06 that....... 61 | ``` 62 | 63 | Another example that's also contained in the test suite is encoding our package.json, which is of course a string value 64 | centered file, to PSON using a general purpose static dictionary: 65 | 66 | * **JSON** stringify: 813 bytes 67 | * **PSON** with empty dict: 760 bytes (about **6% smaller** than JSON) 68 | * **PSON** with [static dict](https://github.com/dcodeIO/PSON/blob/master/dicts/package.json): 613 bytes (about **24% smaller** than JSON) 69 | 70 | Usage 71 | ----- 72 | 73 | #### node.js/CommonJS 74 | 75 | `npm install pson` 76 | 77 | ```js 78 | var PSON = require("pson"); 79 | ... 80 | ``` 81 | 82 | #### RequireJS/AMD 83 | 84 | ```js 85 | require.config({ 86 | ... 87 | "paths": { 88 | "Long": "/path/to/Long.js", // optional 89 | "ByteBuffer": "/path/to/ByteBufferAB.js", 90 | "PSON": "/path/to/PSON.js" 91 | }, 92 | ... 93 | }); 94 | require(["PSON"], function(PSON) { 95 | ... 96 | }); 97 | ``` 98 | 99 | #### Browser 100 | 101 | ```html 102 | 103 | 104 | 105 | ``` 106 | 107 | ```js 108 | var PSON = dcodeIO.PSON; 109 | ... 110 | ``` 111 | 112 | Example 113 | ------- 114 | ```js 115 | // Sender 116 | var initialDictionary = ["hello"]; 117 | var pson = new PSON.ProgressivePair(initialDictionary); 118 | var data = { "hello": "world!" }; 119 | var buffer = pson.encode(data); 120 | someSocket.send(buffer); 121 | ``` 122 | 123 | ```js 124 | // Receiver 125 | var initialDictionary = ["hello"]; 126 | var pson = new PSON.ProgressivePair(initialDictionary); 127 | someSocket.on("data", function(data) { 128 | data = pson.decode(data); 129 | ... 130 | }); 131 | ``` 132 | 133 | API 134 | --- 135 | The API is pretty much straight forward: 136 | 137 | * `PSON.Pair#encode(json: *): ByteBuffer` encodes JSON to PSON data 138 | * `PSON.Pair#toBuffer(json: *): Buffer` encodes straight to a node.js Buffer 139 | * `PSON.Pair#toArrayBuffer(json: *): ArrayBuffer` encodes straight to an ArrayBuffer 140 | * `PSON.Pair#decode(pson: ByteBuffer|Buffer|ArrayBuffer): *` decodes PSON data to JSON 141 | 142 | #### Progressive 143 | * `new PSON.ProgressivePair([initialDictionary: Array.])` constructs a new progressive encoder and decoder pair 144 | with an automatically filling keyword dictionary 145 | * `PSON.ProgressivePair#exclude(obj: Object)` Excludes an object's and its children's keywords from being added to the progressive 146 | dictionary 147 | * `PSON.ProgressivePair#include(obj: Object)` Undoes the former 148 | 149 | #### Static 150 | * `new PSON.StaticPair([dictionary: Array.])` constructs a new static encoder and decoder pair 151 | with a static (or empty) dictionary 152 | 153 | Downloads 154 | --------- 155 | * [Distributions](https://github.com/dcodeIO/PSON/tree/master/dist) 156 | * [ZIP-Archive](https://github.com/dcodeIO/PSON/archive/master.zip) 157 | * [Tarball](https://github.com/dcodeIO/PSON/tarball/master) 158 | 159 | Documentation 160 | ------------- 161 | * [PSON specification](https://github.com/dcodeIO/PSON/blob/master/PSONspec.txt) 162 | * [API documentation](http://htmlpreview.github.io/?http://raw.github.com/dcodeIO/PSON/master/docs/PSON.html) 163 | 164 | **License:** [Apache License, Version 2.0](http://opensource.org/licenses/Apache-2.0) 165 | -------------------------------------------------------------------------------- /dicts/README.md: -------------------------------------------------------------------------------- 1 | Dictionaries 2 | ------------ 3 | This directory contains a set of dictionaries for common use cases. 4 | -------------------------------------------------------------------------------- /dicts/empty.json: -------------------------------------------------------------------------------- 1 | [] -------------------------------------------------------------------------------- /dicts/package.json: -------------------------------------------------------------------------------- 1 | [ 2 | "name", 3 | "version", 4 | "description", 5 | "keywords", 6 | "homepage", 7 | "bugs", 8 | "url", 9 | "email", 10 | "license", 11 | "licenses", 12 | "author", 13 | "email", 14 | "contributors", 15 | "maintainers", 16 | "files", 17 | "main", 18 | "bin", 19 | "man", 20 | "directories", 21 | "directories.lib", 22 | "directories.bin", 23 | "directories.man", 24 | "directories.doc", 25 | "directories.example", 26 | "repository", 27 | "type", 28 | "git", 29 | "svn", 30 | "scripts", 31 | "prepublish", 32 | "publish", 33 | "postpublish", 34 | "preinstall", 35 | "install", 36 | "postinstall", 37 | "preuninstall", 38 | "uninstall", 39 | "postuninstall", 40 | "preupdate", 41 | "update", 42 | "postupdate", 43 | "pretest", 44 | "test", 45 | "posttest", 46 | "prestop", 47 | "stop", 48 | "poststop", 49 | "prestart", 50 | "start", 51 | "poststart", 52 | "prerestart", 53 | "restart", 54 | "postrestart", 55 | "config", 56 | "dependencies", 57 | "latest", 58 | "devDependencies", 59 | "bundledDependencies", 60 | "optionalDependencies", 61 | "engines", 62 | "node", 63 | "npm", 64 | "os", 65 | "darwin", 66 | "freebsd", 67 | "linux", 68 | "sunos", 69 | "win32", 70 | "!darwin", 71 | "!freebsd", 72 | "!linux", 73 | "!sunos", 74 | "!win32", 75 | "cpu", 76 | "arm", 77 | "ia32", 78 | "mips", 79 | "x64", 80 | "!arm", 81 | "!ia32", 82 | "!mips", 83 | "!x64", 84 | "preferGlobal", 85 | "private", 86 | "publishConfig" 87 | ] -------------------------------------------------------------------------------- /dist/PSON.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | /** 18 | * @license PSON (c) 2013 Daniel Wirtz 19 | * Released under the Apache License, Version 2.0 20 | * see: https://github.com/dcodeIO/PSON for details 21 | */ 22 | (function(global) { 23 | "use strict"; 24 | 25 | function loadPSON(ByteBuffer) { 26 | if (!ByteBuffer) { 27 | throw(new Error("PSON requires ByteBuffer.js: Get it at https://github.com/dcodeIO/ByteBuffer.js")); 28 | } 29 | 30 | /** 31 | * PSON namespace. 32 | * @exports PSON 33 | * @namespace 34 | */ 35 | var PSON = {}; 36 | 37 | /** 38 | * @alias PSON.T 39 | */ 40 | PSON.T = (function() { 41 | 42 | /** 43 | * PSON byte types. 44 | * @exports PSON.T 45 | * @namespace 46 | */ 47 | var T = {}; 48 | 49 | T.ZERO = 0x00; // 0 50 | // 0x01; // -1 51 | // 0x02; // 1 52 | // ... // zig-zag encoded varints 53 | T.MAX = 0xEF; // -120, max. zig-zag encoded varint 54 | 55 | T.NULL = 0xF0; // null 56 | T.TRUE = 0xF1; // true 57 | T.FALSE = 0xF2; // false 58 | T.EOBJECT = 0xF3; // {} 59 | T.EARRAY = 0xF4; // [] 60 | T.ESTRING = 0xF5; // "" 61 | T.OBJECT = 0xF6; // {...} 62 | T.ARRAY = 0xF7; // [...] 63 | T.INTEGER = 0xF8; // number (zig-zag encoded varint32) 64 | T.LONG = 0xF9; // Long (zig-zag encoded varint64) 65 | T.FLOAT = 0xFA; // number (float32) 66 | T.DOUBLE = 0xFB; // number (float64) 67 | T.STRING = 0xFC; // string (varint length + data) 68 | T.STRING_ADD = 0xFD; // string (varint length + data, add to dictionary) 69 | T.STRING_GET = 0xFE; // string (varint index to get from dictionary) 70 | T.BINARY = 0xFF; // bytes (varint length + data) 71 | 72 | return T; 73 | 74 | })(); 75 | 76 | /** 77 | * @alias PSON.Encoder 78 | */ 79 | PSON.Encoder = (function(ByteBuffer, T) { 80 | 81 | /** 82 | * Float conversion test buffer. 83 | * @type {!ByteBuffer} 84 | */ 85 | var fbuf = new ByteBuffer(4); 86 | fbuf.length = 4; 87 | 88 | /** 89 | * Long.js. 90 | * @type {?Long} 91 | */ 92 | var Long = ByteBuffer.Long; 93 | 94 | /** 95 | * Constructs a new PSON Encoder. 96 | * @exports PSON.Encoder 97 | * @class A PSON Encoder. 98 | * @param {Array.=} dict Initial dictionary 99 | * @param {boolean} progressive Whether this is a progressive or a static encoder 100 | * @param {Object.=} options Options 101 | * @constructor 102 | */ 103 | var Encoder = function(dict, progressive, options) { 104 | 105 | /** 106 | * Dictionary hash. 107 | * @type {Object.} 108 | */ 109 | this.dict = {}; 110 | 111 | /** 112 | * Next dictionary index. 113 | * @type {number} 114 | */ 115 | this.next = 0; 116 | if (dict && Array.isArray(dict)) { 117 | while (this.next < dict.length) { 118 | this.dict[dict[this.next]] = this.next++; 119 | } 120 | } 121 | 122 | /** 123 | * Whether the encoder is progressive or static. 124 | * @type {boolean} 125 | */ 126 | this.progressive = !!progressive; 127 | 128 | /** 129 | * Options. 130 | * @type {Object.} 131 | */ 132 | this.options = options || {}; 133 | }; 134 | 135 | /** 136 | * Encodes JSON to PSON. 137 | * @param {*} json JSON 138 | * @param {(!ByteBuffer)=} buf Buffer to encode to. When omitted, the resulting ByteBuffer will be flipped. When 139 | * specified, it will not be flipped. 140 | * @returns {!ByteBuffer} PSON 141 | */ 142 | Encoder.prototype.encode = function(json, buf) { 143 | var doFlip = false; 144 | if (!buf) { 145 | buf = new ByteBuffer(); 146 | doFlip = true; 147 | } 148 | var le = buf.littleEndian; 149 | try { 150 | this._encodeValue(json, buf.LE()); 151 | buf.littleEndian = le; 152 | return doFlip ? buf.flip() : buf; 153 | } catch (e) { 154 | buf.littleEndian = le; 155 | throw(e); 156 | } 157 | }; 158 | 159 | /** 160 | * Encodes a single JSON value to PSON. 161 | * @param {*} val JSON value 162 | * @param {!ByteBuffer} buf Target buffer 163 | * @param {boolean=} excluded Whether keywords are to be excluded or not 164 | * @private 165 | */ 166 | Encoder.prototype._encodeValue = function(val, buf, excluded) { 167 | if (val === null) { 168 | buf.writeUint8(T.NULL); 169 | } else { 170 | switch (typeof val) { 171 | case 'function': 172 | val = val.toString(); 173 | // fall through 174 | case 'string': 175 | if (val.length === 0) { 176 | buf.writeUint8(T.ESTRING); 177 | } else { 178 | if (this.dict.hasOwnProperty(val)) { 179 | buf.writeUint8(T.STRING_GET); 180 | buf.writeVarint32(this.dict[val]); 181 | } else { 182 | buf.writeUint8(T.STRING); 183 | buf.writeVString(val); 184 | } 185 | } 186 | break; 187 | case 'number': 188 | var intVal = parseInt(val); 189 | if (val === intVal) { 190 | var zzval = ByteBuffer.zigZagEncode32(val); // unsigned 191 | if (zzval <= T.MAX) { 192 | buf.writeUint8(zzval); 193 | } else { 194 | buf.writeUint8(T.INTEGER); 195 | buf.writeVarint32ZigZag(val); 196 | } 197 | } else { 198 | fbuf.writeFloat32(val, 0); 199 | if (val === fbuf.readFloat32(0)) { 200 | buf.writeUint8(T.FLOAT); 201 | buf.writeFloat32(val); 202 | } else { 203 | buf.writeUint8(T.DOUBLE); 204 | buf.writeFloat64(val); 205 | } 206 | } 207 | break; 208 | case 'boolean': 209 | buf.writeUint8(val ? T.TRUE : T.FALSE); 210 | break; 211 | case 'object': 212 | var i; 213 | if (Array.isArray(val)) { 214 | if (val.length === 0) { 215 | buf.writeUint8(T.EARRAY); 216 | } else { 217 | buf.writeUint8(T.ARRAY); 218 | buf.writeVarint32(val.length); 219 | for (i=0; i} dict Initial dictionary values 294 | * @param {boolean} progressive Whether this is a progressive or a static decoder 295 | * @param {Object.=} options Options 296 | * @constructor 297 | */ 298 | var Decoder = function(dict, progressive, options) { 299 | 300 | /** 301 | * Dictionary array. 302 | * @type {Array.} 303 | */ 304 | this.dict = (dict && Array.isArray(dict)) ? dict : []; 305 | 306 | /** 307 | * Whether this is a progressive or a static decoder. 308 | * @type {boolean} 309 | */ 310 | this.progressive = !!progressive; 311 | 312 | /** 313 | * Options. 314 | * @type {Object.} 315 | */ 316 | this.options = options || {}; 317 | }; 318 | 319 | /** 320 | * Decodes PSON to JSON. 321 | * @param {!(ByteBuffer|ArrayBuffer|Buffer)} buf PSON 322 | * @returns {?} JSON 323 | */ 324 | Decoder.prototype.decode = function(buf) { 325 | if (!(buf instanceof ByteBuffer)) { 326 | buf = ByteBuffer.wrap(buf); 327 | } 328 | var le = buf.littleEndian; 329 | try { 330 | var val = this._decodeValue(buf.LE()); 331 | buf.littleEndian = le; 332 | return val; 333 | } catch (e) { 334 | buf.littleEndian = le; 335 | throw(e); 336 | } 337 | }; 338 | 339 | /** 340 | * Decodes a single PSON value to JSON. 341 | * @param {!ByteBuffer} buf Buffer to decode from 342 | * @returns {?} JSON 343 | * @private 344 | */ 345 | Decoder.prototype._decodeValue = function(buf) { 346 | var t = buf.readUint8(); 347 | if (t <= T.MAX) { 348 | return ByteBuffer.zigZagDecode32(t); 349 | } else { 350 | switch (t) { 351 | case T.NULL: return null; 352 | case T.TRUE: return true; 353 | case T.FALSE: return false; 354 | case T.EOBJECT: return {}; 355 | case T.EARRAY: return []; 356 | case T.ESTRING: return ""; 357 | case T.OBJECT: 358 | t = buf.readVarint32(); // #keys 359 | var obj = {}; 360 | while (--t>=0) { 361 | obj[this._decodeValue(buf)] = this._decodeValue(buf); 362 | } 363 | return obj; 364 | case T.ARRAY: 365 | t = buf.readVarint32(); // #items 366 | var arr = []; 367 | while (--t>=0) { 368 | arr.push(this._decodeValue(buf)); 369 | } 370 | return arr; 371 | case T.INTEGER: return buf.readVarint32ZigZag(); 372 | case T.LONG: // must not crash 373 | if (Long) return buf.readVarint64ZigZag(); 374 | return buf.readVarint32ZigZag(); 375 | case T.FLOAT: return buf.readFloat32(); 376 | case T.DOUBLE: return buf.readFloat64(); 377 | case T.STRING: return buf.readVString(); 378 | case T.STRING_ADD: 379 | var str = buf.readVString(); 380 | this.dict.push(str); 381 | return str; 382 | case T.STRING_GET: 383 | return this.dict[buf.readVarint32()]; 384 | case T.BINARY: 385 | t = buf.readVarint32(); 386 | var ret = buf.slice(buf.offset, buf.offset+t); 387 | buf.offset += t; 388 | return ret; 389 | default: 390 | throw(new Error("Illegal type at "+buf.offset+": "+t)); 391 | } 392 | } 393 | }; 394 | 395 | return Decoder; 396 | 397 | })(ByteBuffer, PSON.T); 398 | 399 | /** 400 | * @alias PSON.Pair 401 | */ 402 | PSON.Pair = (function() { 403 | 404 | /** 405 | * Constructs a new abstract PSON encoder and decoder pair. 406 | * @exports PSON.Pair 407 | * @class An abstract PSON encoder and decoder pair. 408 | * @constructor 409 | * @abstract 410 | */ 411 | var Pair = function() { 412 | 413 | /** 414 | * Encoder. 415 | * @type {!PSON.Encoder} 416 | * @expose 417 | */ 418 | this.encoder; 419 | 420 | /** 421 | * Decoder. 422 | * @type {!PSON.Decoder} 423 | * @expose 424 | */ 425 | this.decoder; 426 | }; 427 | 428 | /** 429 | * Encodes JSON to PSON. 430 | * @param {*} json JSON 431 | * @returns {!ByteBuffer} PSON 432 | * @expose 433 | */ 434 | Pair.prototype.encode = function(json) { 435 | return this.encoder.encode(json); 436 | }; 437 | 438 | /** 439 | * Encodes JSON straight to an ArrayBuffer of PSON. 440 | * @param {*} json JSON 441 | * @returns {!ArrayBuffer} PSON as ArrayBuffer 442 | * @expose 443 | */ 444 | Pair.prototype.toArrayBuffer = function(json) { 445 | return this.encoder.encode(json).toArrayBuffer(); 446 | }; 447 | 448 | /** 449 | * Encodes JSON straight to a node Buffer of PSON. 450 | * @param {*} json JSON 451 | * @returns {!Buffer} PSON as node Buffer 452 | * @expose 453 | */ 454 | Pair.prototype.toBuffer = function(json) { 455 | return this.encoder.encode(json).toBuffer(); 456 | }; 457 | 458 | /** 459 | * Decodes PSON to JSON. 460 | * @param {ByteBuffer|ArrayBuffer|Buffer} pson PSON 461 | * @returns {*} JSON 462 | * @expose 463 | */ 464 | Pair.prototype.decode = function(pson) { 465 | return this.decoder.decode(pson); 466 | }; 467 | 468 | return Pair; 469 | })(); 470 | 471 | /** 472 | * @alias PSON.StaticPair 473 | */ 474 | PSON.StaticPair = (function(Pair, Encoder, Decoder) { 475 | 476 | /** 477 | * Constructs a new static PSON encoder and decoder pair. 478 | * @exports PSON.StaticPair 479 | * @class A static PSON encoder and decoder pair. 480 | * @param {Array.=} dict Static dictionary 481 | * @param {Object.=} options Options 482 | * @constructor 483 | * @extends PSON.Pair 484 | */ 485 | var StaticPair = function(dict, options) { 486 | Pair.call(this); 487 | 488 | this.encoder = new Encoder(dict, false, options); 489 | this.decoder = new Decoder(dict, false, options); 490 | }; 491 | 492 | // Extends PSON.Pair 493 | StaticPair.prototype = Object.create(Pair.prototype); 494 | 495 | return StaticPair; 496 | 497 | })(PSON.Pair, PSON.Encoder, PSON.Decoder); 498 | 499 | /** 500 | * @alias PSON.ProgressivePair 501 | */ 502 | PSON.ProgressivePair = (function(Pair, Encoder, Decoder) { 503 | 504 | /** 505 | * Constructs a new progressive PSON encoder and decoder pair. 506 | * @exports PSON.ProgressivePair 507 | * @class A progressive PSON encoder and decoder pair. 508 | * @param {Array.=} dict Initial dictionary 509 | * @param {Object.=} options Options 510 | * @constructor 511 | * @extends PSON.Pair 512 | */ 513 | var ProgressivePair = function(dict, options) { 514 | Pair.call(this); 515 | 516 | this.encoder = new Encoder(dict, true, options); 517 | this.decoder = new Decoder(dict, true, options); 518 | }; 519 | 520 | // Extends PSON.Pair 521 | ProgressivePair.prototype = Object.create(Pair.prototype); 522 | 523 | /** 524 | * Alias for {@link PSON.exclude}. 525 | * @param {Object} obj Now excluded object 526 | */ 527 | ProgressivePair.prototype.exclude = function(obj) { 528 | PSON.exclude(obj); 529 | }; 530 | 531 | /** 532 | * Alias for {@link PSON.include}. 533 | * @param {Object} obj New included object 534 | */ 535 | ProgressivePair.prototype.include = function(obj) { 536 | PSON.include(obj); 537 | }; 538 | 539 | return ProgressivePair; 540 | 541 | })(PSON.Pair, PSON.Encoder, PSON.Decoder); 542 | 543 | /** 544 | * Excluces an object's and its children's keys from being added to a progressive dictionary. 545 | * @param {Object} obj Now excluded object 546 | */ 547 | PSON.exclude = function(obj) { 548 | if (typeof obj === 'object') { 549 | Object.defineProperty(obj, "_PSON_EXCL_", { 550 | value: true, 551 | enumerable: false, 552 | configurable: true 553 | }); 554 | } 555 | }; 556 | 557 | /** 558 | * Undoes exclusion of an object's and its children's keys from being added to a progressive dictionary. 559 | * @param {Object} obj Now included object 560 | */ 561 | PSON.include = function(obj) { 562 | if (typeof obj === 'object') { 563 | delete obj["_PSON_EXCL_"]; 564 | } 565 | }; 566 | 567 | return PSON; 568 | } 569 | 570 | // Enable module loading if available 571 | if (typeof module != 'undefined' && module["exports"]) { // CommonJS 572 | module["exports"] = loadPSON(require("bytebuffer")); 573 | } else if (typeof define != 'undefined' && define["amd"]) { // AMD 574 | define("PSON", ["ByteBuffer"], loadPSON); 575 | } else { 576 | if (!global["dcodeIO"]) { 577 | global["dcodeIO"] = {}; 578 | } 579 | global["dcodeIO"]["PSON"] = loadPSON(global["dcodeIO"]["ByteBuffer"]); 580 | } 581 | 582 | })(this); 583 | -------------------------------------------------------------------------------- /dist/PSON.min.js: -------------------------------------------------------------------------------- 1 | /* 2 | PSON (c) 2013 Daniel Wirtz 3 | Released under the Apache License, Version 2.0 4 | see: https://github.com/dcodeIO/PSON for details 5 | */ 6 | (function(k){function n(k){if(!k)throw Error("PSON requires ByteBuffer.js: Get it at https://github.com/dcodeIO/ByteBuffer.js");var e={T:{ZERO:0,MAX:239,NULL:240,TRUE:241,FALSE:242,EOBJECT:243,EARRAY:244,ESTRING:245,OBJECT:246,ARRAY:247,INTEGER:248,LONG:249,FLOAT:250,DOUBLE:251,STRING:252,STRING_ADD:253,STRING_GET:254,BINARY:255}};e.Encoder=function(f,c){var e=new f(4);e.length=4;var g=f.Long,b=function(a,d,c){this.dict={};this.next=0;if(a&&Array.isArray(a))for(;this.next 2 | 3 | 4 | 5 | Class: Decoder 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Class: Decoder

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | PSON. 31 | 32 | Decoder 33 |

34 | 35 |

A PSON Decoder.

36 | 37 |
38 | 39 |
40 |
41 | 42 | 43 | 44 | 45 |
46 |

new Decoder(dict, progressive, options)

47 | 48 | 49 |
50 |
51 | 52 | 53 |
54 |

Constructs a new PSON Decoder.

55 |
56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
Parameters:
64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 101 | 102 | 103 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 130 | 131 | 132 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 159 | 160 | 161 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 |
NameTypeArgumentDescription
dict 94 | 95 | 96 | Array.<string> 97 | 98 | 99 | 100 | 104 | 105 | 106 | 107 |

Initial dictionary values

progressive 123 | 124 | 125 | boolean 126 | 127 | 128 | 129 | 133 | 134 | 135 | 136 |

Whether this is a progressive or a static decoder

options 152 | 153 | 154 | Object.<string,*> 155 | 156 | 157 | 158 | 162 | 163 | <optional>
164 | 165 | 166 | 167 |

Options

178 | 179 | 180 | 181 |
182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 |
206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 |
216 | 217 | 218 |
219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 |

Members

232 | 233 |
234 | 235 |
236 |

dict :Array.<string>

237 | 238 | 239 |
240 |
241 | 242 |
243 |

Dictionary array.

244 |
245 | 246 | 247 | 248 |
249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 |
273 | 274 | 275 | 276 |
277 | 278 | 279 | 280 |
281 |

options :Object.<string,*>

282 | 283 | 284 |
285 |
286 | 287 |
288 |

Options.

289 |
290 | 291 | 292 | 293 |
294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 |
318 | 319 | 320 | 321 |
322 | 323 | 324 | 325 |
326 |

progressive :boolean

327 | 328 | 329 |
330 |
331 | 332 |
333 |

Whether this is a progressive or a static decoder.

334 |
335 | 336 | 337 | 338 |
339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 |
363 | 364 | 365 | 366 |
367 | 368 |
369 | 370 | 371 | 372 |

Methods

373 | 374 |
375 | 376 |
377 |

decode(buf) → {?}

378 | 379 | 380 |
381 |
382 | 383 | 384 |
385 |

Decodes PSON to JSON.

386 |
387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 |
Parameters:
395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 |
NameTypeDescription
buf 423 | 424 | 425 | ByteBuffer 426 | | 427 | 428 | ArrayBuffer 429 | | 430 | 431 | Buffer 432 | 433 | 434 | 435 |

PSON

447 | 448 | 449 | 450 |
451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 |
475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 |
Returns:
483 | 484 | 485 |
486 |

JSON

487 |
488 | 489 | 490 | 491 |
492 |
493 | Type 494 |
495 |
496 | 497 | ? 498 | 499 | 500 |
501 |
502 | 503 | 504 | 505 | 506 |
507 | 508 |
509 | 510 | 511 | 512 | 513 | 514 |
515 | 516 |
517 | 518 | 519 | 520 | 521 |
522 | 523 | 528 | 529 |
530 | 531 | 532 | 533 | 534 | -------------------------------------------------------------------------------- /docs/PSON.Encoder.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Class: Encoder 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Class: Encoder

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | PSON. 31 | 32 | Encoder 33 |

34 | 35 |

A PSON Encoder.

36 | 37 |
38 | 39 |
40 |
41 | 42 | 43 | 44 | 45 |
46 |

new Encoder(dict, progressive, options)

47 | 48 | 49 |
50 |
51 | 52 | 53 |
54 |

Constructs a new PSON Encoder.

55 |
56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
Parameters:
64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 101 | 102 | 103 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 132 | 133 | 134 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 161 | 162 | 163 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 |
NameTypeArgumentDescription
dict 94 | 95 | 96 | Array.<string> 97 | 98 | 99 | 100 | 104 | 105 | <optional>
106 | 107 | 108 | 109 |

Initial dictionary

progressive 125 | 126 | 127 | boolean 128 | 129 | 130 | 131 | 135 | 136 | 137 | 138 |

Whether this is a progressive or a static encoder

options 154 | 155 | 156 | Object.<string,*> 157 | 158 | 159 | 160 | 164 | 165 | <optional>
166 | 167 | 168 | 169 |

Options

180 | 181 | 182 | 183 |
184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 |
208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 |
218 | 219 | 220 |
221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 |

Members

234 | 235 |
236 | 237 |
238 |

dict :Object.<string,number>

239 | 240 | 241 |
242 |
243 | 244 |
245 |

Dictionary hash.

246 |
247 | 248 | 249 | 250 |
251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 |
275 | 276 | 277 | 278 |
279 | 280 | 281 | 282 |
283 |

next :number

284 | 285 | 286 |
287 |
288 | 289 |
290 |

Next dictionary index.

291 |
292 | 293 | 294 | 295 |
296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 |
320 | 321 | 322 | 323 |
324 | 325 | 326 | 327 |
328 |

options :Object.<string,*>

329 | 330 | 331 |
332 |
333 | 334 |
335 |

Options.

336 |
337 | 338 | 339 | 340 |
341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 |
365 | 366 | 367 | 368 |
369 | 370 | 371 | 372 |
373 |

progressive :boolean

374 | 375 | 376 |
377 |
378 | 379 |
380 |

Whether the encoder is progressive or static.

381 |
382 | 383 | 384 | 385 |
386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 |
410 | 411 | 412 | 413 |
414 | 415 |
416 | 417 | 418 | 419 |

Methods

420 | 421 |
422 | 423 |
424 |

encode(json, buf) → {ByteBuffer}

425 | 426 | 427 |
428 |
429 | 430 | 431 |
432 |

Encodes JSON to PSON.

433 |
434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 |
Parameters:
442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 479 | 480 | 481 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 508 | 509 | 510 | 517 | 518 | 519 | 520 | 521 | 523 | 524 | 525 | 526 | 527 |
NameTypeArgumentDescription
json 472 | 473 | 474 | * 475 | 476 | 477 | 478 | 482 | 483 | 484 | 485 |

JSON

buf 501 | 502 | 503 | (!ByteBuffer) 504 | 505 | 506 | 507 | 511 | 512 | <optional>
513 | 514 | 515 | 516 |

Buffer to encode to. When omitted, the resulting ByteBuffer will be flipped. When 522 | specified, it will not be flipped.

528 | 529 | 530 | 531 |
532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 |
556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 |
Returns:
564 | 565 | 566 |
567 |

PSON

568 |
569 | 570 | 571 | 572 |
573 |
574 | Type 575 |
576 |
577 | 578 | ByteBuffer 579 | 580 | 581 |
582 |
583 | 584 | 585 | 586 | 587 |
588 | 589 |
590 | 591 | 592 | 593 | 594 | 595 |
596 | 597 |
598 | 599 | 600 | 601 | 602 |
603 | 604 | 609 | 610 |
611 | 612 | 613 | 614 | 615 | -------------------------------------------------------------------------------- /docs/PSON.Pair.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Class: Pair 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Class: Pair

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | PSON. 31 | 32 | Pair 33 |

34 | 35 |

An abstract PSON encoder and decoder pair.

36 | 37 |
38 | 39 |
40 |
41 | 42 | 43 | 44 | 45 |
46 |

<virtual> new Pair()

47 | 48 | 49 |
50 |
51 | 52 | 53 |
54 |

Constructs a new abstract PSON encoder and decoder pair.

55 |
56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 |
66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 |
90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 |
100 | 101 | 102 |
103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 |

Methods

118 | 119 |
120 | 121 |
122 |

decode(pson) → {*}

123 | 124 | 125 |
126 |
127 | 128 | 129 |
130 |

Decodes PSON to JSON.

131 |
132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 |
Parameters:
140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 |
NameTypeDescription
pson 168 | 169 | 170 | ByteBuffer 171 | | 172 | 173 | ArrayBuffer 174 | | 175 | 176 | Buffer 177 | 178 | 179 | 180 |

PSON

192 | 193 | 194 | 195 |
196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 |
220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 |
Returns:
228 | 229 | 230 |
231 |

JSON

232 |
233 | 234 | 235 | 236 |
237 |
238 | Type 239 |
240 |
241 | 242 | * 243 | 244 | 245 |
246 |
247 | 248 | 249 | 250 | 251 |
252 | 253 | 254 | 255 |
256 |

encode(json) → {ByteBuffer}

257 | 258 | 259 |
260 |
261 | 262 | 263 |
264 |

Encodes JSON to PSON.

265 |
266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 |
Parameters:
274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 |
NameTypeDescription
json 302 | 303 | 304 | * 305 | 306 | 307 | 308 |

JSON

320 | 321 | 322 | 323 |
324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 |
348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 |
Returns:
356 | 357 | 358 |
359 |

PSON

360 |
361 | 362 | 363 | 364 |
365 |
366 | Type 367 |
368 |
369 | 370 | ByteBuffer 371 | 372 | 373 |
374 |
375 | 376 | 377 | 378 | 379 |
380 | 381 | 382 | 383 |
384 |

toArrayBuffer(json) → {ArrayBuffer}

385 | 386 | 387 |
388 |
389 | 390 | 391 |
392 |

Encodes JSON straight to an ArrayBuffer of PSON.

393 |
394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 |
Parameters:
402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 |
NameTypeDescription
json 430 | 431 | 432 | * 433 | 434 | 435 | 436 |

JSON

448 | 449 | 450 | 451 |
452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 |
476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 |
Returns:
484 | 485 | 486 |
487 |

PSON as ArrayBuffer

488 |
489 | 490 | 491 | 492 |
493 |
494 | Type 495 |
496 |
497 | 498 | ArrayBuffer 499 | 500 | 501 |
502 |
503 | 504 | 505 | 506 | 507 |
508 | 509 | 510 | 511 |
512 |

toBuffer(json) → {Buffer}

513 | 514 | 515 |
516 |
517 | 518 | 519 |
520 |

Encodes JSON straight to a node Buffer of PSON.

521 |
522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 |
Parameters:
530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 |
NameTypeDescription
json 558 | 559 | 560 | * 561 | 562 | 563 | 564 |

JSON

576 | 577 | 578 | 579 |
580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 |
604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 |
Returns:
612 | 613 | 614 |
615 |

PSON as node Buffer

616 |
617 | 618 | 619 | 620 |
621 |
622 | Type 623 |
624 |
625 | 626 | Buffer 627 | 628 | 629 |
630 |
631 | 632 | 633 | 634 | 635 |
636 | 637 |
638 | 639 | 640 | 641 | 642 | 643 |
644 | 645 |
646 | 647 | 648 | 649 | 650 |
651 | 652 | 657 | 658 |
659 | 660 | 661 | 662 | 663 | -------------------------------------------------------------------------------- /docs/PSON.ProgressivePair.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Class: ProgressivePair 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Class: ProgressivePair

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | PSON. 31 | 32 | ProgressivePair 33 |

34 | 35 |

A progressive PSON encoder and decoder pair.

36 | 37 |
38 | 39 |
40 |
41 | 42 | 43 | 44 | 45 |
46 |

new ProgressivePair(dict, options)

47 | 48 | 49 |
50 |
51 | 52 | 53 |
54 |

Constructs a new progressive PSON encoder and decoder pair.

55 |
56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
Parameters:
64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 101 | 102 | 103 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 132 | 133 | 134 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 |
NameTypeArgumentDescription
dict 94 | 95 | 96 | Array.<string> 97 | 98 | 99 | 100 | 104 | 105 | <optional>
106 | 107 | 108 | 109 |

Initial dictionary

options 125 | 126 | 127 | Object.<string,*> 128 | 129 | 130 | 131 | 135 | 136 | <optional>
137 | 138 | 139 | 140 |

Options

151 | 152 | 153 | 154 |
155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 |
179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 |
189 | 190 | 191 |
192 | 193 | 194 |

Extends

195 | 196 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 |

Methods

213 | 214 |
215 | 216 |
217 |

decode(pson) → {*}

218 | 219 | 220 |
221 |
222 | 223 | 224 |
225 |

Decodes PSON to JSON.

226 |
227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 |
Parameters:
235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 |
NameTypeDescription
pson 263 | 264 | 265 | ByteBuffer 266 | | 267 | 268 | ArrayBuffer 269 | | 270 | 271 | Buffer 272 | 273 | 274 | 275 |

PSON

287 | 288 | 289 | 290 |
291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 |
Inherited From:
299 |
302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 |
320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 |
Returns:
328 | 329 | 330 |
331 |

JSON

332 |
333 | 334 | 335 | 336 |
337 |
338 | Type 339 |
340 |
341 | 342 | * 343 | 344 | 345 |
346 |
347 | 348 | 349 | 350 | 351 |
352 | 353 | 354 | 355 |
356 |

encode(json) → {ByteBuffer}

357 | 358 | 359 |
360 |
361 | 362 | 363 |
364 |

Encodes JSON to PSON.

365 |
366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 |
Parameters:
374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 |
NameTypeDescription
json 402 | 403 | 404 | * 405 | 406 | 407 | 408 |

JSON

420 | 421 | 422 | 423 |
424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 |
Inherited From:
432 |
435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 |
453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 |
Returns:
461 | 462 | 463 |
464 |

PSON

465 |
466 | 467 | 468 | 469 |
470 |
471 | Type 472 |
473 |
474 | 475 | ByteBuffer 476 | 477 | 478 |
479 |
480 | 481 | 482 | 483 | 484 |
485 | 486 | 487 | 488 |
489 |

exclude(obj)

490 | 491 | 492 |
493 |
494 | 495 | 496 |
497 |

Alias for PSON.exclude.

498 |
499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 |
Parameters:
507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 |
NameTypeDescription
obj 535 | 536 | 537 | Object 538 | 539 | 540 | 541 |

Now excluded object

553 | 554 | 555 | 556 |
557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 |
581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 |
591 | 592 | 593 | 594 |
595 |

include(obj)

596 | 597 | 598 |
599 |
600 | 601 | 602 |
603 |

Alias for PSON.include.

604 |
605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 |
Parameters:
613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 |
NameTypeDescription
obj 641 | 642 | 643 | Object 644 | 645 | 646 | 647 |

New included object

659 | 660 | 661 | 662 |
663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 |
687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 |
697 | 698 | 699 | 700 |
701 |

toArrayBuffer(json) → {ArrayBuffer}

702 | 703 | 704 |
705 |
706 | 707 | 708 |
709 |

Encodes JSON straight to an ArrayBuffer of PSON.

710 |
711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 |
Parameters:
719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 |
NameTypeDescription
json 747 | 748 | 749 | * 750 | 751 | 752 | 753 |

JSON

765 | 766 | 767 | 768 |
769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 |
Inherited From:
777 |
780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 |
798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 |
Returns:
806 | 807 | 808 |
809 |

PSON as ArrayBuffer

810 |
811 | 812 | 813 | 814 |
815 |
816 | Type 817 |
818 |
819 | 820 | ArrayBuffer 821 | 822 | 823 |
824 |
825 | 826 | 827 | 828 | 829 |
830 | 831 | 832 | 833 |
834 |

toBuffer(json) → {Buffer}

835 | 836 | 837 |
838 |
839 | 840 | 841 |
842 |

Encodes JSON straight to a node Buffer of PSON.

843 |
844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 |
Parameters:
852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 |
NameTypeDescription
json 880 | 881 | 882 | * 883 | 884 | 885 | 886 |

JSON

898 | 899 | 900 | 901 |
902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 |
Inherited From:
910 |
913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 |
931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 |
Returns:
939 | 940 | 941 |
942 |

PSON as node Buffer

943 |
944 | 945 | 946 | 947 |
948 |
949 | Type 950 |
951 |
952 | 953 | Buffer 954 | 955 | 956 |
957 |
958 | 959 | 960 | 961 | 962 |
963 | 964 |
965 | 966 | 967 | 968 | 969 | 970 |
971 | 972 |
973 | 974 | 975 | 976 | 977 |
978 | 979 | 984 | 985 |
986 | 987 | 988 | 989 | 990 | -------------------------------------------------------------------------------- /docs/PSON.StaticPair.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Class: StaticPair 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Class: StaticPair

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | PSON. 31 | 32 | StaticPair 33 |

34 | 35 |

A static PSON encoder and decoder pair.

36 | 37 |
38 | 39 |
40 |
41 | 42 | 43 | 44 | 45 |
46 |

new StaticPair(dict, options)

47 | 48 | 49 |
50 |
51 | 52 | 53 |
54 |

Constructs a new static PSON encoder and decoder pair.

55 |
56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
Parameters:
64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 101 | 102 | 103 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 132 | 133 | 134 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 |
NameTypeArgumentDescription
dict 94 | 95 | 96 | Array.<string> 97 | 98 | 99 | 100 | 104 | 105 | <optional>
106 | 107 | 108 | 109 |

Static dictionary

options 125 | 126 | 127 | Object.<string,*> 128 | 129 | 130 | 131 | 135 | 136 | <optional>
137 | 138 | 139 | 140 |

Options

151 | 152 | 153 | 154 |
155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 |
179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 |
189 | 190 | 191 |
192 | 193 | 194 |

Extends

195 | 196 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 |

Methods

213 | 214 |
215 | 216 |
217 |

decode(pson) → {*}

218 | 219 | 220 |
221 |
222 | 223 | 224 |
225 |

Decodes PSON to JSON.

226 |
227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 |
Parameters:
235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 |
NameTypeDescription
pson 263 | 264 | 265 | ByteBuffer 266 | | 267 | 268 | ArrayBuffer 269 | | 270 | 271 | Buffer 272 | 273 | 274 | 275 |

PSON

287 | 288 | 289 | 290 |
291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 |
Inherited From:
299 |
302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 |
320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 |
Returns:
328 | 329 | 330 |
331 |

JSON

332 |
333 | 334 | 335 | 336 |
337 |
338 | Type 339 |
340 |
341 | 342 | * 343 | 344 | 345 |
346 |
347 | 348 | 349 | 350 | 351 |
352 | 353 | 354 | 355 |
356 |

encode(json) → {ByteBuffer}

357 | 358 | 359 |
360 |
361 | 362 | 363 |
364 |

Encodes JSON to PSON.

365 |
366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 |
Parameters:
374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 |
NameTypeDescription
json 402 | 403 | 404 | * 405 | 406 | 407 | 408 |

JSON

420 | 421 | 422 | 423 |
424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 |
Inherited From:
432 |
435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 |
453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 |
Returns:
461 | 462 | 463 |
464 |

PSON

465 |
466 | 467 | 468 | 469 |
470 |
471 | Type 472 |
473 |
474 | 475 | ByteBuffer 476 | 477 | 478 |
479 |
480 | 481 | 482 | 483 | 484 |
485 | 486 | 487 | 488 |
489 |

toArrayBuffer(json) → {ArrayBuffer}

490 | 491 | 492 |
493 |
494 | 495 | 496 |
497 |

Encodes JSON straight to an ArrayBuffer of PSON.

498 |
499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 |
Parameters:
507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 |
NameTypeDescription
json 535 | 536 | 537 | * 538 | 539 | 540 | 541 |

JSON

553 | 554 | 555 | 556 |
557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 |
Inherited From:
565 |
568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 |
586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 |
Returns:
594 | 595 | 596 |
597 |

PSON as ArrayBuffer

598 |
599 | 600 | 601 | 602 |
603 |
604 | Type 605 |
606 |
607 | 608 | ArrayBuffer 609 | 610 | 611 |
612 |
613 | 614 | 615 | 616 | 617 |
618 | 619 | 620 | 621 |
622 |

toBuffer(json) → {Buffer}

623 | 624 | 625 |
626 |
627 | 628 | 629 |
630 |

Encodes JSON straight to a node Buffer of PSON.

631 |
632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 |
Parameters:
640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 |
NameTypeDescription
json 668 | 669 | 670 | * 671 | 672 | 673 | 674 |

JSON

686 | 687 | 688 | 689 |
690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 |
Inherited From:
698 |
701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 |
719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 |
Returns:
727 | 728 | 729 |
730 |

PSON as node Buffer

731 |
732 | 733 | 734 | 735 |
736 |
737 | Type 738 |
739 |
740 | 741 | Buffer 742 | 743 | 744 |
745 |
746 | 747 | 748 | 749 | 750 |
751 | 752 |
753 | 754 | 755 | 756 | 757 | 758 |
759 | 760 |
761 | 762 | 763 | 764 | 765 |
766 | 767 | 772 | 773 |
774 | 775 | 776 | 777 | 778 | -------------------------------------------------------------------------------- /docs/PSON.T.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Namespace: T 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Namespace: T

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | PSON. 31 | 32 | T 33 |

34 | 35 |
36 | 37 |
38 |
39 | 40 | 41 | 42 | 43 |

PSON byte types.

44 | 45 | 46 | 47 |
48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 |
72 | 73 | 74 | 75 | 76 |
77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
96 | 97 |
98 | 99 | 100 | 101 | 102 |
103 | 104 | 109 | 110 |
111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /docs/PSON.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Namespace: PSON 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Namespace: PSON

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | PSON 31 |

32 | 33 |
34 | 35 |
36 |
37 | 38 | 39 | 40 | 41 |

PSON namespace.

42 | 43 | 44 | 45 |
46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 |
70 | 71 | 72 | 73 | 74 |
75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 |

Classes

84 | 85 |
86 |
Decoder
87 |
88 | 89 |
Encoder
90 |
91 | 92 |
Pair
93 |
94 | 95 |
ProgressivePair
96 |
97 | 98 |
StaticPair
99 |
100 |
101 | 102 | 103 | 104 |

Namespaces

105 | 106 |
107 |
T
108 |
109 |
110 | 111 | 112 | 113 | 114 | 115 |

Methods

116 | 117 |
118 | 119 |
120 |

<static> exclude(obj)

121 | 122 | 123 |
124 |
125 | 126 | 127 |
128 |

Excluces an object's and its children's keys from being added to a progressive dictionary.

129 |
130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 |
Parameters:
138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 |
NameTypeDescription
obj 166 | 167 | 168 | Object 169 | 170 | 171 | 172 |

Now excluded object

184 | 185 | 186 | 187 |
188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 |
212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 |
222 | 223 | 224 | 225 |
226 |

<static> include(obj)

227 | 228 | 229 |
230 |
231 | 232 | 233 |
234 |

Undoes exclusion of an object's and its children's keys from being added to a progressive dictionary.

235 |
236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 |
Parameters:
244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 |
NameTypeDescription
obj 272 | 273 | 274 | Object 275 | 276 | 277 | 278 |

Now included object

290 | 291 | 292 | 293 |
294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 |
318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 |
328 | 329 |
330 | 331 | 332 | 333 | 334 | 335 |
336 | 337 |
338 | 339 | 340 | 341 | 342 |
343 | 344 | 349 | 350 |
351 | 352 | 353 | 354 | 355 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Index 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Index

21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |

29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 |
49 | 50 | 55 | 56 |
57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /docs/module-PSON.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Module: PSON 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Module: PSON

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | PSON 31 |

32 | 33 |
34 | 35 |
36 |
37 | 38 | 39 | 40 | 41 |

PSON namespace.

42 | 43 | 44 | 45 |
46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 |
70 | 71 | 72 | 73 | 74 |
75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 |

Methods

90 | 91 |
92 | 93 |
94 |

<static> exclude(obj)

95 | 96 | 97 |
98 |
99 | 100 | 101 |
102 |

Excluces an object's and its children's keys from being added to a progressive dictionary.

103 |
104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 |
Parameters:
112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 |
NameTypeDescription
obj 140 | 141 | 142 | Object 143 | 144 | 145 | 146 |

Now excluded object

158 | 159 | 160 | 161 |
162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 |
186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 |
196 | 197 | 198 | 199 |
200 |

<static> include(obj)

201 | 202 | 203 |
204 |
205 | 206 | 207 |
208 |

Undoes exclusion of an object's and its children's keys from being added to a progressive dictionary.

209 |
210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 |
Parameters:
218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 |
NameTypeDescription
obj 246 | 247 | 248 | Object 249 | 250 | 251 | 252 |

Now included object

264 | 265 | 266 | 267 |
268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 |
292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 |
302 | 303 |
304 | 305 | 306 | 307 | 308 | 309 |
310 | 311 |
312 | 313 | 314 | 315 | 316 |
317 | 318 | 323 | 324 |
325 | 326 | 327 | 328 | 329 | -------------------------------------------------------------------------------- /docs/scripts/html5.js: -------------------------------------------------------------------------------- 1 | /* 2 | HTML5 Shiv v3.6.2pre | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed 3 | Uncompressed source: https://github.com/aFarkas/html5shiv 4 | */ 5 | (function(l,f){function m(){var a=e.elements;return"string"==typeof a?a.split(" "):a}function i(a){var b=n[a[o]];b||(b={},h++,a[o]=h,n[h]=b);return b}function p(a,b,c){b||(b=f);if(g)return b.createElement(a);c||(c=i(b));b=c.cache[a]?c.cache[a].cloneNode():r.test(a)?(c.cache[a]=c.createElem(a)).cloneNode():c.createElem(a);return b.canHaveChildren&&!s.test(a)?c.frag.appendChild(b):b}function t(a,b){if(!b.cache)b.cache={},b.createElem=a.createElement,b.createFrag=a.createDocumentFragment,b.frag=b.createFrag(); 6 | a.createElement=function(c){return!e.shivMethods?b.createElem(c):p(c,a,b)};a.createDocumentFragment=Function("h,f","return function(){var n=f.cloneNode(),c=n.createElement;h.shivMethods&&("+m().join().replace(/\w+/g,function(a){b.createElem(a);b.frag.createElement(a);return'c("'+a+'")'})+");return n}")(e,b.frag)}function q(a){a||(a=f);var b=i(a);if(e.shivCSS&&!j&&!b.hasCSS){var c,d=a;c=d.createElement("p");d=d.getElementsByTagName("head")[0]||d.documentElement;c.innerHTML="x"; 7 | c=d.insertBefore(c.lastChild,d.firstChild);b.hasCSS=!!c}g||t(a,b);return a}var k=l.html5||{},s=/^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i,r=/^(?:a|b|code|div|fieldset|h1|h2|h3|h4|h5|h6|i|label|li|ol|p|q|span|strong|style|table|tbody|td|th|tr|ul)$/i,j,o="_html5shiv",h=0,n={},g;(function(){try{var a=f.createElement("a");a.innerHTML="";j="hidden"in a;var b;if(!(b=1==a.childNodes.length)){f.createElement("a");var c=f.createDocumentFragment();b="undefined"==typeof c.cloneNode|| 8 | "undefined"==typeof c.createDocumentFragment||"undefined"==typeof c.createElement}g=b}catch(d){g=j=!0}})();var e={elements:k.elements||"abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup main mark meter nav output progress section summary time video",version:"3.6.2pre",shivCSS:!1!==k.shivCSS,supportsUnknownElements:g,shivMethods:!1!==k.shivMethods,type:"default",shivDocument:q,createElement:p,createDocumentFragment:function(a,b){a||(a=f);if(g)return a.createDocumentFragment(); 9 | for(var b=b||i(a),c=b.frag.cloneNode(),d=0,e=m(),h=e.length;d)/],["lit",/^(?:\d+|\d*\.\d+)(?:%|[a-z]+)?/i],["lit",/^#[\da-f]{3,6}/i],["pln",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i],["pun",/^[^\s\w"']+/]]),["css"]);PR.registerLangHandler(PR.createSimpleLexer([],[["kwd",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i]]),["css-kw"]);PR.registerLangHandler(PR.createSimpleLexer([],[["str",/^[^"')]+/]]),["css-str"]); 3 | -------------------------------------------------------------------------------- /docs/scripts/prettify/prettify.js: -------------------------------------------------------------------------------- 1 | var q=null;window.PR_SHOULD_USE_CONTINUATION=!0; 2 | (function(){function L(a){function m(a){var f=a.charCodeAt(0);if(f!==92)return f;var b=a.charAt(1);return(f=r[b])?f:"0"<=b&&b<="7"?parseInt(a.substring(1),8):b==="u"||b==="x"?parseInt(a.substring(2),16):a.charCodeAt(1)}function e(a){if(a<32)return(a<16?"\\x0":"\\x")+a.toString(16);a=String.fromCharCode(a);if(a==="\\"||a==="-"||a==="["||a==="]")a="\\"+a;return a}function h(a){for(var f=a.substring(1,a.length-1).match(/\\u[\dA-Fa-f]{4}|\\x[\dA-Fa-f]{2}|\\[0-3][0-7]{0,2}|\\[0-7]{1,2}|\\[\S\s]|[^\\]/g),a= 3 | [],b=[],o=f[0]==="^",c=o?1:0,i=f.length;c122||(d<65||j>90||b.push([Math.max(65,j)|32,Math.min(d,90)|32]),d<97||j>122||b.push([Math.max(97,j)&-33,Math.min(d,122)&-33]))}}b.sort(function(a,f){return a[0]-f[0]||f[1]-a[1]});f=[];j=[NaN,NaN];for(c=0;ci[0]&&(i[1]+1>i[0]&&b.push("-"),b.push(e(i[1])));b.push("]");return b.join("")}function y(a){for(var f=a.source.match(/\[(?:[^\\\]]|\\[\S\s])*]|\\u[\dA-Fa-f]{4}|\\x[\dA-Fa-f]{2}|\\\d+|\\[^\dux]|\(\?[!:=]|[()^]|[^()[\\^]+/g),b=f.length,d=[],c=0,i=0;c=2&&a==="["?f[c]=h(j):a!=="\\"&&(f[c]=j.replace(/[A-Za-z]/g,function(a){a=a.charCodeAt(0);return"["+String.fromCharCode(a&-33,a|32)+"]"}));return f.join("")}for(var t=0,s=!1,l=!1,p=0,d=a.length;p=5&&"lang-"===b.substring(0,5))&&!(o&&typeof o[1]==="string"))c=!1,b="src";c||(r[f]=b)}i=d;d+=f.length;if(c){c=o[1];var j=f.indexOf(c),k=j+c.length;o[2]&&(k=f.length-o[2].length,j=k-c.length);b=b.substring(5);B(l+i,f.substring(0,j),e,p);B(l+i+j,c,C(b,c),p);B(l+i+k,f.substring(k),e,p)}else p.push(l+i,b)}a.e=p}var h={},y;(function(){for(var e=a.concat(m), 9 | l=[],p={},d=0,g=e.length;d=0;)h[n.charAt(k)]=r;r=r[1];n=""+r;p.hasOwnProperty(n)||(l.push(r),p[n]=q)}l.push(/[\S\s]/);y=L(l)})();var t=m.length;return e}function u(a){var m=[],e=[];a.tripleQuotedStrings?m.push(["str",/^(?:'''(?:[^'\\]|\\[\S\s]|''?(?=[^']))*(?:'''|$)|"""(?:[^"\\]|\\[\S\s]|""?(?=[^"]))*(?:"""|$)|'(?:[^'\\]|\\[\S\s])*(?:'|$)|"(?:[^"\\]|\\[\S\s])*(?:"|$))/,q,"'\""]):a.multiLineStrings?m.push(["str",/^(?:'(?:[^'\\]|\\[\S\s])*(?:'|$)|"(?:[^"\\]|\\[\S\s])*(?:"|$)|`(?:[^\\`]|\\[\S\s])*(?:`|$))/, 10 | q,"'\"`"]):m.push(["str",/^(?:'(?:[^\n\r'\\]|\\.)*(?:'|$)|"(?:[^\n\r"\\]|\\.)*(?:"|$))/,q,"\"'"]);a.verbatimStrings&&e.push(["str",/^@"(?:[^"]|"")*(?:"|$)/,q]);var h=a.hashComments;h&&(a.cStyleComments?(h>1?m.push(["com",/^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/,q,"#"]):m.push(["com",/^#(?:(?:define|elif|else|endif|error|ifdef|include|ifndef|line|pragma|undef|warning)\b|[^\n\r]*)/,q,"#"]),e.push(["str",/^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h|[a-z]\w*)>/,q])):m.push(["com",/^#[^\n\r]*/, 11 | q,"#"]));a.cStyleComments&&(e.push(["com",/^\/\/[^\n\r]*/,q]),e.push(["com",/^\/\*[\S\s]*?(?:\*\/|$)/,q]));a.regexLiterals&&e.push(["lang-regex",/^(?:^^\.?|[!+-]|!=|!==|#|%|%=|&|&&|&&=|&=|\(|\*|\*=|\+=|,|-=|->|\/|\/=|:|::|;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|[?@[^]|\^=|\^\^|\^\^=|{|\||\|=|\|\||\|\|=|~|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\s*(\/(?=[^*/])(?:[^/[\\]|\\[\S\s]|\[(?:[^\\\]]|\\[\S\s])*(?:]|$))+\/)/]);(h=a.types)&&e.push(["typ",h]);a=(""+a.keywords).replace(/^ | $/g, 12 | "");a.length&&e.push(["kwd",RegExp("^(?:"+a.replace(/[\s,]+/g,"|")+")\\b"),q]);m.push(["pln",/^\s+/,q," \r\n\t\xa0"]);e.push(["lit",/^@[$_a-z][\w$@]*/i,q],["typ",/^(?:[@_]?[A-Z]+[a-z][\w$@]*|\w+_t\b)/,q],["pln",/^[$_a-z][\w$@]*/i,q],["lit",/^(?:0x[\da-f]+|(?:\d(?:_\d+)*\d*(?:\.\d*)?|\.\d\+)(?:e[+-]?\d+)?)[a-z]*/i,q,"0123456789"],["pln",/^\\[\S\s]?/,q],["pun",/^.[^\s\w"-$'./@\\`]*/,q]);return x(m,e)}function D(a,m){function e(a){switch(a.nodeType){case 1:if(k.test(a.className))break;if("BR"===a.nodeName)h(a), 13 | a.parentNode&&a.parentNode.removeChild(a);else for(a=a.firstChild;a;a=a.nextSibling)e(a);break;case 3:case 4:if(p){var b=a.nodeValue,d=b.match(t);if(d){var c=b.substring(0,d.index);a.nodeValue=c;(b=b.substring(d.index+d[0].length))&&a.parentNode.insertBefore(s.createTextNode(b),a.nextSibling);h(a);c||a.parentNode.removeChild(a)}}}}function h(a){function b(a,d){var e=d?a.cloneNode(!1):a,f=a.parentNode;if(f){var f=b(f,1),g=a.nextSibling;f.appendChild(e);for(var h=g;h;h=g)g=h.nextSibling,f.appendChild(h)}return e} 14 | for(;!a.nextSibling;)if(a=a.parentNode,!a)return;for(var a=b(a.nextSibling,0),e;(e=a.parentNode)&&e.nodeType===1;)a=e;d.push(a)}var k=/(?:^|\s)nocode(?:\s|$)/,t=/\r\n?|\n/,s=a.ownerDocument,l;a.currentStyle?l=a.currentStyle.whiteSpace:window.getComputedStyle&&(l=s.defaultView.getComputedStyle(a,q).getPropertyValue("white-space"));var p=l&&"pre"===l.substring(0,3);for(l=s.createElement("LI");a.firstChild;)l.appendChild(a.firstChild);for(var d=[l],g=0;g=0;){var h=m[e];A.hasOwnProperty(h)?window.console&&console.warn("cannot override language handler %s",h):A[h]=a}}function C(a,m){if(!a||!A.hasOwnProperty(a))a=/^\s*=o&&(h+=2);e>=c&&(a+=2)}}catch(w){"console"in window&&console.log(w&&w.stack?w.stack:w)}}var v=["break,continue,do,else,for,if,return,while"],w=[[v,"auto,case,char,const,default,double,enum,extern,float,goto,int,long,register,short,signed,sizeof,static,struct,switch,typedef,union,unsigned,void,volatile"], 18 | "catch,class,delete,false,import,new,operator,private,protected,public,this,throw,true,try,typeof"],F=[w,"alignof,align_union,asm,axiom,bool,concept,concept_map,const_cast,constexpr,decltype,dynamic_cast,explicit,export,friend,inline,late_check,mutable,namespace,nullptr,reinterpret_cast,static_assert,static_cast,template,typeid,typename,using,virtual,where"],G=[w,"abstract,boolean,byte,extends,final,finally,implements,import,instanceof,null,native,package,strictfp,super,synchronized,throws,transient"], 19 | H=[G,"as,base,by,checked,decimal,delegate,descending,dynamic,event,fixed,foreach,from,group,implicit,in,interface,internal,into,is,lock,object,out,override,orderby,params,partial,readonly,ref,sbyte,sealed,stackalloc,string,select,uint,ulong,unchecked,unsafe,ushort,var"],w=[w,"debugger,eval,export,function,get,null,set,undefined,var,with,Infinity,NaN"],I=[v,"and,as,assert,class,def,del,elif,except,exec,finally,from,global,import,in,is,lambda,nonlocal,not,or,pass,print,raise,try,with,yield,False,True,None"], 20 | J=[v,"alias,and,begin,case,class,def,defined,elsif,end,ensure,false,in,module,next,nil,not,or,redo,rescue,retry,self,super,then,true,undef,unless,until,when,yield,BEGIN,END"],v=[v,"case,done,elif,esac,eval,fi,function,in,local,set,then,until"],K=/^(DIR|FILE|vector|(de|priority_)?queue|list|stack|(const_)?iterator|(multi)?(set|map)|bitset|u?(int|float)\d*)/,N=/\S/,O=u({keywords:[F,H,w,"caller,delete,die,do,dump,elsif,eval,exit,foreach,for,goto,if,import,last,local,my,next,no,our,print,package,redo,require,sub,undef,unless,until,use,wantarray,while,BEGIN,END"+ 21 | I,J,v],hashComments:!0,cStyleComments:!0,multiLineStrings:!0,regexLiterals:!0}),A={};k(O,["default-code"]);k(x([],[["pln",/^[^]*(?:>|$)/],["com",/^<\!--[\S\s]*?(?:--\>|$)/],["lang-",/^<\?([\S\s]+?)(?:\?>|$)/],["lang-",/^<%([\S\s]+?)(?:%>|$)/],["pun",/^(?:<[%?]|[%?]>)/],["lang-",/^]*>([\S\s]+?)<\/xmp\b[^>]*>/i],["lang-js",/^]*>([\S\s]*?)(<\/script\b[^>]*>)/i],["lang-css",/^]*>([\S\s]*?)(<\/style\b[^>]*>)/i],["lang-in.tag",/^(<\/?[a-z][^<>]*>)/i]]), 22 | ["default-markup","htm","html","mxml","xhtml","xml","xsl"]);k(x([["pln",/^\s+/,q," \t\r\n"],["atv",/^(?:"[^"]*"?|'[^']*'?)/,q,"\"'"]],[["tag",/^^<\/?[a-z](?:[\w-.:]*\w)?|\/?>$/i],["atn",/^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],["lang-uq.val",/^=\s*([^\s"'>]*(?:[^\s"'/>]|\/(?=\s)))/],["pun",/^[/<->]+/],["lang-js",/^on\w+\s*=\s*"([^"]+)"/i],["lang-js",/^on\w+\s*=\s*'([^']+)'/i],["lang-js",/^on\w+\s*=\s*([^\s"'>]+)/i],["lang-css",/^style\s*=\s*"([^"]+)"/i],["lang-css",/^style\s*=\s*'([^']+)'/i],["lang-css", 23 | /^style\s*=\s*([^\s"'>]+)/i]]),["in.tag"]);k(x([],[["atv",/^[\S\s]+/]]),["uq.val"]);k(u({keywords:F,hashComments:!0,cStyleComments:!0,types:K}),["c","cc","cpp","cxx","cyc","m"]);k(u({keywords:"null,true,false"}),["json"]);k(u({keywords:H,hashComments:!0,cStyleComments:!0,verbatimStrings:!0,types:K}),["cs"]);k(u({keywords:G,cStyleComments:!0}),["java"]);k(u({keywords:v,hashComments:!0,multiLineStrings:!0}),["bsh","csh","sh"]);k(u({keywords:I,hashComments:!0,multiLineStrings:!0,tripleQuotedStrings:!0}), 24 | ["cv","py"]);k(u({keywords:"caller,delete,die,do,dump,elsif,eval,exit,foreach,for,goto,if,import,last,local,my,next,no,our,print,package,redo,require,sub,undef,unless,until,use,wantarray,while,BEGIN,END",hashComments:!0,multiLineStrings:!0,regexLiterals:!0}),["perl","pl","pm"]);k(u({keywords:J,hashComments:!0,multiLineStrings:!0,regexLiterals:!0}),["rb"]);k(u({keywords:w,cStyleComments:!0,regexLiterals:!0}),["js"]);k(u({keywords:"all,and,by,catch,class,else,extends,false,finally,for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then,true,try,unless,until,when,while,yes", 25 | hashComments:3,cStyleComments:!0,multilineStrings:!0,tripleQuotedStrings:!0,regexLiterals:!0}),["coffee"]);k(x([],[["str",/^[\S\s]+/]]),["regex"]);window.prettyPrintOne=function(a,m,e){var h=document.createElement("PRE");h.innerHTML=a;e&&D(h,e);E({g:m,i:e,h:h});return h.innerHTML};window.prettyPrint=function(a){function m(){for(var e=window.PR_SHOULD_USE_CONTINUATION?l.now()+250:Infinity;p=0){var k=k.match(g),f,b;if(b= 26 | !k){b=n;for(var o=void 0,c=b.firstChild;c;c=c.nextSibling)var i=c.nodeType,o=i===1?o?b:c:i===3?N.test(c.nodeValue)?b:o:o;b=(f=o===b?void 0:o)&&"CODE"===f.tagName}b&&(k=f.className.match(g));k&&(k=k[1]);b=!1;for(o=n.parentNode;o;o=o.parentNode)if((o.tagName==="pre"||o.tagName==="code"||o.tagName==="xmp")&&o.className&&o.className.indexOf("prettyprint")>=0){b=!0;break}b||((b=(b=n.className.match(/\blinenums\b(?::(\d+))?/))?b[1]&&b[1].length?+b[1]:!0:!1)&&D(n,b),d={g:k,h:n,i:b},E(d))}}p 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var path = require("path"), 17 | PSON = require(path.join(__dirname, "dist", "PSON.js")); 18 | 19 | module.exports = PSON; 20 | -------------------------------------------------------------------------------- /jsdoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "tags": { 3 | "allowUnknownTags": true 4 | }, 5 | "source": { 6 | "include": ["dist/PSON.js"] 7 | }, 8 | "opts": { 9 | "template": "templates/esoccer", 10 | "destination": "docs" 11 | }, 12 | "plugins": ["plugins/markdown"] 13 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pson", 3 | "description": "A super efficient binary serialization format for JSON.", 4 | "version": "2.0.0", 5 | "author": "Daniel Wirtz ", 6 | "main": "index.js", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/dcodeIO/PSON.git" 10 | }, 11 | "bugs": { 12 | "url": "https://github.com/dcodeIO/PSON/issues" 13 | }, 14 | "keywords": ["net", "buffer", "serialization", "bytebuffer", "websocket", "webrtc", "json", "protobuf", "thrift"], 15 | "license": "Apache-2.0", 16 | "engines": { 17 | "node": ">=0.6" 18 | }, 19 | "dependencies": { 20 | "bytebuffer": "~3" 21 | }, 22 | "devDependencies": { 23 | "metascript": "~0", 24 | "closurecompiler": "~1", 25 | "testjs": "~1" 26 | }, 27 | "scripts": { 28 | "test": "testjs", 29 | "build": "metascript src/PSON.js src/ > dist/PSON.js", 30 | "compile": "ccjs dist/PSON.js > dist/PSON.min.js", 31 | "jsdoc": "jsdoc -c jsdoc.json", 32 | "make": "npm run-script build && npm run-script compile && npm test && npm run-script jsdoc" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/PSON.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | /** 18 | * @license PSON (c) 2013 Daniel Wirtz 19 | * Released under the Apache License, Version 2.0 20 | * see: https://github.com/dcodeIO/PSON for details 21 | */ 22 | (function(global) { 23 | "use strict"; 24 | 25 | function loadPSON(ByteBuffer) { 26 | if (!ByteBuffer) { 27 | throw(new Error("PSON requires ByteBuffer.js: Get it at https://github.com/dcodeIO/ByteBuffer.js")); 28 | } 29 | 30 | /** 31 | * PSON namespace. 32 | * @exports PSON 33 | * @namespace 34 | */ 35 | var PSON = {}; 36 | 37 | //? include("PSON/T.js"); 38 | 39 | //? include("PSON/Encoder.js"); 40 | 41 | //? include("PSON/Decoder.js"); 42 | 43 | //? include("PSON/Pair.js"); 44 | 45 | //? include("PSON/StaticPair.js"); 46 | 47 | //? include("PSON/ProgressivePair.js"); 48 | 49 | /** 50 | * Excluces an object's and its children's keys from being added to a progressive dictionary. 51 | * @param {Object} obj Now excluded object 52 | */ 53 | PSON.exclude = function(obj) { 54 | if (typeof obj === 'object') { 55 | Object.defineProperty(obj, "_PSON_EXCL_", { 56 | value: true, 57 | enumerable: false, 58 | configurable: true 59 | }); 60 | } 61 | }; 62 | 63 | /** 64 | * Undoes exclusion of an object's and its children's keys from being added to a progressive dictionary. 65 | * @param {Object} obj Now included object 66 | */ 67 | PSON.include = function(obj) { 68 | if (typeof obj === 'object') { 69 | delete obj["_PSON_EXCL_"]; 70 | } 71 | }; 72 | 73 | return PSON; 74 | } 75 | 76 | // Enable module loading if available 77 | if (typeof module != 'undefined' && module["exports"]) { // CommonJS 78 | module["exports"] = loadPSON(require("bytebuffer")); 79 | } else if (typeof define != 'undefined' && define["amd"]) { // AMD 80 | define("PSON", ["ByteBuffer"], loadPSON); 81 | } else { 82 | if (!global["dcodeIO"]) { 83 | global["dcodeIO"] = {}; 84 | } 85 | global["dcodeIO"]["PSON"] = loadPSON(global["dcodeIO"]["ByteBuffer"]); 86 | } 87 | 88 | })(this); 89 | -------------------------------------------------------------------------------- /src/PSON/Decoder.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @alias PSON.Decoder 3 | */ 4 | PSON.Decoder = (function(ByteBuffer, T) { 5 | 6 | /** 7 | * Long.js. 8 | * @type {?Long} 9 | */ 10 | var Long = ByteBuffer.Long; 11 | 12 | /** 13 | * Constructs a new PSON Decoder. 14 | * @exports PSON.Decoder 15 | * @class A PSON Decoder. 16 | * @param {Array.} dict Initial dictionary values 17 | * @param {boolean} progressive Whether this is a progressive or a static decoder 18 | * @param {Object.=} options Options 19 | * @constructor 20 | */ 21 | var Decoder = function(dict, progressive, options) { 22 | 23 | /** 24 | * Dictionary array. 25 | * @type {Array.} 26 | */ 27 | this.dict = (dict && Array.isArray(dict)) ? dict : []; 28 | 29 | /** 30 | * Whether this is a progressive or a static decoder. 31 | * @type {boolean} 32 | */ 33 | this.progressive = !!progressive; 34 | 35 | /** 36 | * Options. 37 | * @type {Object.} 38 | */ 39 | this.options = options || {}; 40 | }; 41 | 42 | /** 43 | * Decodes PSON to JSON. 44 | * @param {!(ByteBuffer|ArrayBuffer|Buffer)} buf PSON 45 | * @returns {?} JSON 46 | */ 47 | Decoder.prototype.decode = function(buf) { 48 | if (!(buf instanceof ByteBuffer)) { 49 | buf = ByteBuffer.wrap(buf); 50 | } 51 | var le = buf.littleEndian; 52 | try { 53 | var val = this._decodeValue(buf.LE()); 54 | buf.littleEndian = le; 55 | return val; 56 | } catch (e) { 57 | buf.littleEndian = le; 58 | throw(e); 59 | } 60 | }; 61 | 62 | /** 63 | * Decodes a single PSON value to JSON. 64 | * @param {!ByteBuffer} buf Buffer to decode from 65 | * @returns {?} JSON 66 | * @private 67 | */ 68 | Decoder.prototype._decodeValue = function(buf) { 69 | var t = buf.readUint8(); 70 | if (t <= T.MAX) { 71 | return ByteBuffer.zigZagDecode32(t); 72 | } else { 73 | switch (t) { 74 | case T.NULL: return null; 75 | case T.TRUE: return true; 76 | case T.FALSE: return false; 77 | case T.EOBJECT: return {}; 78 | case T.EARRAY: return []; 79 | case T.ESTRING: return ""; 80 | case T.OBJECT: 81 | t = buf.readVarint32(); // #keys 82 | var obj = {}; 83 | while (--t>=0) { 84 | obj[this._decodeValue(buf)] = this._decodeValue(buf); 85 | } 86 | return obj; 87 | case T.ARRAY: 88 | t = buf.readVarint32(); // #items 89 | var arr = []; 90 | while (--t>=0) { 91 | arr.push(this._decodeValue(buf)); 92 | } 93 | return arr; 94 | case T.INTEGER: return buf.readVarint32ZigZag(); 95 | case T.LONG: // must not crash 96 | if (Long) return buf.readVarint64ZigZag(); 97 | return buf.readVarint32ZigZag(); 98 | case T.FLOAT: return buf.readFloat32(); 99 | case T.DOUBLE: return buf.readFloat64(); 100 | case T.STRING: return buf.readVString(); 101 | case T.STRING_ADD: 102 | var str = buf.readVString(); 103 | this.dict.push(str); 104 | return str; 105 | case T.STRING_GET: 106 | return this.dict[buf.readVarint32()]; 107 | case T.BINARY: 108 | t = buf.readVarint32(); 109 | var ret = buf.slice(buf.offset, buf.offset+t); 110 | buf.offset += t; 111 | return ret; 112 | default: 113 | throw(new Error("Illegal type at "+buf.offset+": "+t)); 114 | } 115 | } 116 | }; 117 | 118 | return Decoder; 119 | 120 | })(ByteBuffer, PSON.T); 121 | -------------------------------------------------------------------------------- /src/PSON/Encoder.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @alias PSON.Encoder 3 | */ 4 | PSON.Encoder = (function(ByteBuffer, T) { 5 | 6 | /** 7 | * Float conversion test buffer. 8 | * @type {!ByteBuffer} 9 | */ 10 | var fbuf = new ByteBuffer(4); 11 | fbuf.length = 4; 12 | 13 | /** 14 | * Long.js. 15 | * @type {?Long} 16 | */ 17 | var Long = ByteBuffer.Long; 18 | 19 | /** 20 | * Constructs a new PSON Encoder. 21 | * @exports PSON.Encoder 22 | * @class A PSON Encoder. 23 | * @param {Array.=} dict Initial dictionary 24 | * @param {boolean} progressive Whether this is a progressive or a static encoder 25 | * @param {Object.=} options Options 26 | * @constructor 27 | */ 28 | var Encoder = function(dict, progressive, options) { 29 | 30 | /** 31 | * Dictionary hash. 32 | * @type {Object.} 33 | */ 34 | this.dict = {}; 35 | 36 | /** 37 | * Next dictionary index. 38 | * @type {number} 39 | */ 40 | this.next = 0; 41 | if (dict && Array.isArray(dict)) { 42 | while (this.next < dict.length) { 43 | this.dict[dict[this.next]] = this.next++; 44 | } 45 | } 46 | 47 | /** 48 | * Whether the encoder is progressive or static. 49 | * @type {boolean} 50 | */ 51 | this.progressive = !!progressive; 52 | 53 | /** 54 | * Options. 55 | * @type {Object.} 56 | */ 57 | this.options = options || {}; 58 | }; 59 | 60 | /** 61 | * Encodes JSON to PSON. 62 | * @param {*} json JSON 63 | * @param {(!ByteBuffer)=} buf Buffer to encode to. When omitted, the resulting ByteBuffer will be flipped. When 64 | * specified, it will not be flipped. 65 | * @returns {!ByteBuffer} PSON 66 | */ 67 | Encoder.prototype.encode = function(json, buf) { 68 | var doFlip = false; 69 | if (!buf) { 70 | buf = new ByteBuffer(); 71 | doFlip = true; 72 | } 73 | var le = buf.littleEndian; 74 | try { 75 | this._encodeValue(json, buf.LE()); 76 | buf.littleEndian = le; 77 | return doFlip ? buf.flip() : buf; 78 | } catch (e) { 79 | buf.littleEndian = le; 80 | throw(e); 81 | } 82 | }; 83 | 84 | /** 85 | * Encodes a single JSON value to PSON. 86 | * @param {*} val JSON value 87 | * @param {!ByteBuffer} buf Target buffer 88 | * @param {boolean=} excluded Whether keywords are to be excluded or not 89 | * @private 90 | */ 91 | Encoder.prototype._encodeValue = function(val, buf, excluded) { 92 | if (val === null) { 93 | buf.writeUint8(T.NULL); 94 | } else { 95 | switch (typeof val) { 96 | case 'function': 97 | val = val.toString(); 98 | // fall through 99 | case 'string': 100 | if (val.length === 0) { 101 | buf.writeUint8(T.ESTRING); 102 | } else { 103 | if (this.dict.hasOwnProperty(val)) { 104 | buf.writeUint8(T.STRING_GET); 105 | buf.writeVarint32(this.dict[val]); 106 | } else { 107 | buf.writeUint8(T.STRING); 108 | buf.writeVString(val); 109 | } 110 | } 111 | break; 112 | case 'number': 113 | var intVal = parseInt(val); 114 | if (val === intVal) { 115 | var zzval = ByteBuffer.zigZagEncode32(val); // unsigned 116 | if (zzval <= T.MAX) { 117 | buf.writeUint8(zzval); 118 | } else { 119 | buf.writeUint8(T.INTEGER); 120 | buf.writeVarint32ZigZag(val); 121 | } 122 | } else { 123 | fbuf.writeFloat32(val, 0); 124 | if (val === fbuf.readFloat32(0)) { 125 | buf.writeUint8(T.FLOAT); 126 | buf.writeFloat32(val); 127 | } else { 128 | buf.writeUint8(T.DOUBLE); 129 | buf.writeFloat64(val); 130 | } 131 | } 132 | break; 133 | case 'boolean': 134 | buf.writeUint8(val ? T.TRUE : T.FALSE); 135 | break; 136 | case 'object': 137 | var i; 138 | if (Array.isArray(val)) { 139 | if (val.length === 0) { 140 | buf.writeUint8(T.EARRAY); 141 | } else { 142 | buf.writeUint8(T.ARRAY); 143 | buf.writeVarint32(val.length); 144 | for (i=0; i=} dict Initial dictionary 11 | * @param {Object.=} options Options 12 | * @constructor 13 | * @extends PSON.Pair 14 | */ 15 | var ProgressivePair = function(dict, options) { 16 | Pair.call(this); 17 | 18 | this.encoder = new Encoder(dict, true, options); 19 | this.decoder = new Decoder(dict, true, options); 20 | }; 21 | 22 | // Extends PSON.Pair 23 | ProgressivePair.prototype = Object.create(Pair.prototype); 24 | 25 | /** 26 | * Alias for {@link PSON.exclude}. 27 | * @param {Object} obj Now excluded object 28 | */ 29 | ProgressivePair.prototype.exclude = function(obj) { 30 | PSON.exclude(obj); 31 | }; 32 | 33 | /** 34 | * Alias for {@link PSON.include}. 35 | * @param {Object} obj New included object 36 | */ 37 | ProgressivePair.prototype.include = function(obj) { 38 | PSON.include(obj); 39 | }; 40 | 41 | return ProgressivePair; 42 | 43 | })(PSON.Pair, PSON.Encoder, PSON.Decoder); 44 | -------------------------------------------------------------------------------- /src/PSON/StaticPair.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @alias PSON.StaticPair 3 | */ 4 | PSON.StaticPair = (function(Pair, Encoder, Decoder) { 5 | 6 | /** 7 | * Constructs a new static PSON encoder and decoder pair. 8 | * @exports PSON.StaticPair 9 | * @class A static PSON encoder and decoder pair. 10 | * @param {Array.=} dict Static dictionary 11 | * @param {Object.=} options Options 12 | * @constructor 13 | * @extends PSON.Pair 14 | */ 15 | var StaticPair = function(dict, options) { 16 | Pair.call(this); 17 | 18 | this.encoder = new Encoder(dict, false, options); 19 | this.decoder = new Decoder(dict, false, options); 20 | }; 21 | 22 | // Extends PSON.Pair 23 | StaticPair.prototype = Object.create(Pair.prototype); 24 | 25 | return StaticPair; 26 | 27 | })(PSON.Pair, PSON.Encoder, PSON.Decoder); 28 | -------------------------------------------------------------------------------- /src/PSON/T.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @alias PSON.T 3 | */ 4 | PSON.T = (function() { 5 | 6 | /** 7 | * PSON byte types. 8 | * @exports PSON.T 9 | * @namespace 10 | */ 11 | var T = {}; 12 | 13 | T.ZERO = 0x00; // 0 14 | // 0x01; // -1 15 | // 0x02; // 1 16 | // ... // zig-zag encoded varints 17 | T.MAX = 0xEF; // -120, max. zig-zag encoded varint 18 | 19 | T.NULL = 0xF0; // null 20 | T.TRUE = 0xF1; // true 21 | T.FALSE = 0xF2; // false 22 | T.EOBJECT = 0xF3; // {} 23 | T.EARRAY = 0xF4; // [] 24 | T.ESTRING = 0xF5; // "" 25 | T.OBJECT = 0xF6; // {...} 26 | T.ARRAY = 0xF7; // [...] 27 | T.INTEGER = 0xF8; // number (zig-zag encoded varint32) 28 | T.LONG = 0xF9; // Long (zig-zag encoded varint64) 29 | T.FLOAT = 0xFA; // number (float32) 30 | T.DOUBLE = 0xFB; // number (float64) 31 | T.STRING = 0xFC; // string (varint length + data) 32 | T.STRING_ADD = 0xFD; // string (varint length + data, add to dictionary) 33 | T.STRING_GET = 0xFE; // string (varint index to get from dictionary) 34 | T.BINARY = 0xFF; // bytes (varint length + data) 35 | 36 | return T; 37 | 38 | })(); 39 | -------------------------------------------------------------------------------- /tests/suite.js: -------------------------------------------------------------------------------- 1 | var PSON = require("../index.js"), 2 | ByteBuffer = require("bytebuffer"), 3 | Long = ByteBuffer.Long, 4 | pkg = require("../package.json"), 5 | pkgDict = require("../dicts/package.json"); 6 | 7 | module.exports = { 8 | 9 | "T": function(test) { 10 | var pson = new PSON.StaticPair(); 11 | test.equal(pson.encode(null).compact().toString("debug"), ""); 12 | test.equal(pson.encode(true).compact().toString("debug"), ""); 13 | test.equal(pson.encode(false).compact().toString("debug"), ""); 14 | test.equal(pson.encode({}).compact().toString("debug"), ""); 15 | test.equal(pson.encode([]).compact().toString("debug"), ""); 16 | test.equal(pson.encode("").compact().toString("debug"), ""); 17 | test.done(); 18 | }, 19 | 20 | "number": function(test) { 21 | var pson = new PSON.StaticPair(), bb; 22 | test.equal(pson.encode(0).compact().toString("debug"), "<00>"); 23 | test.equal(pson.encode(-120).compact().toString("debug"), ""); 24 | test.equal((bb=pson.encode(120).compact()).toString("debug"), ""); 25 | test.strictEqual(bb.LE().readVarint32ZigZag(1).value, 120); 26 | test.equal((bb=pson.encode(0.25).compact()).toString("debug"), ""); 27 | test.strictEqual(bb.LE().readFloat32(1), 0.25); 28 | test.equal((bb=pson.encode(0.011).compact()).toString("debug"), ""); 29 | test.strictEqual(bb.LE().readFloat64(1), 0.011); 30 | var l = new Long.fromNumber(1); 31 | test.equal((bb=pson.encode(l).compact()).toString("debug"), ""); 32 | test.ok(l.equals(bb.readVarint64ZigZag(1).value)); 33 | test.done(); 34 | }, 35 | 36 | "string": function(test) { 37 | var pson = new PSON.StaticPair(); 38 | test.equal(pson.encode("abc").compact().toString("debug"), ""); 39 | test.done(); 40 | }, 41 | 42 | "object": function(test) { 43 | var pson = new PSON.StaticPair(); 44 | test.equal(pson.encode({"a":"b"}).compact().toString("debug"), ""); 45 | test.equal(pson.encode({"a":"b","c":50}).compact().toString("debug"), ""); 46 | test.done(); 47 | }, 48 | 49 | "array": function(test) { 50 | var pson = new PSON.StaticPair(); 51 | test.equal(pson.encode([1,2,3]).compact().toString("debug"), ""); 52 | test.done(); 53 | }, 54 | 55 | "undefined": function(test) { 56 | var pson = new PSON.StaticPair(); 57 | test.equal(pson.encode({"a":undefined}).compact().toString("debug"), ""); 58 | test.equal(pson.encode(undefined).compact().toString("debug"), ""); 59 | test.equal(pson.encode([0,undefined,1]).compact().toString("debug"), ""); 60 | test.done(); 61 | }, 62 | 63 | "en/decode": { 64 | 65 | "static": function(test) { 66 | var pson = new PSON.StaticPair(["hello", "time", "float", "boolean", "otherbool", "null", "obj", "what", "arr"]); 67 | var data = { 68 | "hello": "world!", 69 | "time": 1234567890, 70 | "float": 0.01234, 71 | "boolean": true, 72 | "otherbool": false, 73 | "null": null, 74 | "obj": { 75 | "what": "that" 76 | }, 77 | "arr": [1,2,3] 78 | }; 79 | var jsonLen; 80 | test.log("JSON: "+(jsonLen = JSON.stringify(data).length)); 81 | var bb = pson.encode(data).compact(); 82 | test.log("PSON static: "+bb.limit+" = "+(bb.limit*100/jsonLen - 100).toFixed(3)+"%"); 83 | var decData = pson.decode(bb); 84 | test.deepEqual(data, decData); 85 | console.log(""); bb.printDebug(); 86 | test.done(); 87 | }, 88 | 89 | "progressive": function(test) { 90 | var pson = new PSON.ProgressivePair(); 91 | var data = { 92 | "hello": "world!", 93 | "time": 1234567890, 94 | "float": 0.01234, 95 | "boolean": true, 96 | "otherbool": false, 97 | "null": null, 98 | "obj": { 99 | "what": "that" 100 | }, 101 | "arr": [1,2,3] 102 | }; 103 | var jsonLen; 104 | test.log("JSON: "+(jsonLen = JSON.stringify(data).length)); 105 | var bb = pson.encode(data).compact(); 106 | test.log("PSON first: "+bb.limit+" = "+(bb.limit*100/jsonLen - 100).toFixed(3)+"%"); 107 | var decData = pson.decode(bb); 108 | test.deepEqual(data, decData); 109 | bb = pson.encode(data).compact(); 110 | test.log("PSON again: "+bb.limit+" = "+(bb.limit*100/jsonLen - 100).toFixed(3)+"%"); 111 | decData = pson.decode(bb); 112 | console.log(""); bb.printDebug(); 113 | test.deepEqual(data, decData); 114 | test.done(); 115 | } 116 | }, 117 | 118 | "package": function(test) { 119 | var jsonLen; 120 | test.log("JSON: "+(jsonLen=JSON.stringify(pkg).length)); 121 | var pson = new PSON.StaticPair(); 122 | var bb = pson.encode(pkg).compact(); 123 | test.log("PSON w/o dict: "+bb.limit+" = "+(bb.limit*100/jsonLen - 100).toFixed(3)+"%"); 124 | pson = new PSON.StaticPair(pkgDict); 125 | bb = pson.encode(pkg).compact(); 126 | test.log("PSON w/ dict: "+bb.limit+" = "+(bb.limit*100/jsonLen - 100).toFixed(3)+"%"); 127 | console.log(""); bb.printDebug(); 128 | test.deepEqual(pkg, pson.decode(bb)); 129 | test.done(); 130 | } 131 | }; 132 | --------------------------------------------------------------------------------