json-as 0.2.6 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,29 +0,0 @@
1
- import { unknown } from "./unknown"
2
-
3
- /**
4
- * Provides functionality common to all JavaScript objects.
5
- */
6
- export class DynamicObject {
7
- [key: string]: any
8
- // Having it marked as 'any' is for intellisense only
9
- protected __data: Map<string, unknown> = new Map<string, unknown>()
10
- set(path: string, value: unknown): void {
11
- this.__data.set(path, value)
12
- }
13
- get<T = unknown>(path: string): T {
14
- let type!: T
15
- if (type instanceof unknown) {
16
- // @ts-ignore
17
- return this.__data.get(path)
18
- } else {
19
- // @ts-ignore
20
- return this.__data.get(path).get<T>()
21
- }
22
- }
23
- static keys(o: DynamicObject): string[] {
24
- return o.__data.keys()
25
- }
26
- static values(o: DynamicObject): unknown[] {
27
- return o.__data.values()
28
- }
29
- }
package/assembly/bench.ts DELETED
@@ -1,199 +0,0 @@
1
- import 'wasi'
2
-
3
- import { Date } from 'as-wasi'
4
-
5
- import { JSON } from '.'
6
-
7
- import * as asJSON from "assemblyscript-json"
8
-
9
- import { unknown } from './unknown'
10
- import { DynamicObject } from './DynamicObject'
11
-
12
- // @ts-ignore
13
- @json
14
- class JSONSchema {
15
- hello: string
16
- }
17
-
18
- const jsonData: JSONSchema = {
19
- hello: 'world'
20
- }
21
-
22
- const o = new DynamicObject()
23
-
24
- o['hello'] = unknown.wrap('world')
25
-
26
- export function bench(title: string, code: () => void): void {
27
- let ops: u32 = 100_000
28
- const start: f64 = Date.now()
29
- while (ops--) {
30
- code()
31
- }
32
- const time: f64 = Date.now() - start
33
- if (time <= 0) {
34
- console.log(`${title}: ~${100_000 * 1000}.00 ops/s | ${Math.round((time * 100)) / 100}ms`)
35
- } else {
36
- console.log(`${title}: ~${Math.round(((100_000 * 1000) / time) * 100) / 100} ops/s | ${Math.round((time * 100)) / 100}ms`)
37
- }
38
- }
39
-
40
- bench('AS-JSON Stringify String', () => {
41
- JSON.stringify<string>('Hello World')
42
- })
43
-
44
- bench('AS-JSON Parse String', () => {
45
- JSON.parse<string>('"Hello World"')
46
- })
47
-
48
- bench('AS-JSON Stringify Integer', () => {
49
- JSON.stringify<i32>(14)
50
- })
51
-
52
- bench('AS-JSON Parse Integer', () => {
53
- JSON.parse<i32>('14')
54
- })
55
-
56
- bench('AS-JSON Stringify Float', () => {
57
- JSON.stringify<f64>(7.3)
58
- })
59
-
60
- bench('AS-JSON Parse Float', () => {
61
- JSON.parse<f64>('7.3')
62
- })
63
-
64
- bench('AS-JSON Stringify Boolean', () => {
65
- JSON.stringify<boolean>(true)
66
- })
67
-
68
- bench('AS-JSON Parse Boolean', () => {
69
- JSON.parse<boolean>('true')
70
- })
71
-
72
- bench('AS-JSON Stringify Array', () => {
73
- JSON.stringify<boolean[]>([true, false, true])
74
- })
75
-
76
- bench('AS-JSON Parse Array', () => {
77
- JSON.parse<boolean[]>('[true,false,true]')
78
- })
79
-
80
- bench('AS-JSON Stringify Object', () => {
81
- JSON.stringify<JSONSchema>(jsonData)
82
- })
83
-
84
- bench('AS-JSON Parse Object', () => {
85
- JSON.parse<JSONSchema>('{"hello":"world"}')
86
- })
87
-
88
- bench('AS-JSON Stringify Dynamic Object', () => {
89
- JSON.stringify<DynamicObject>(o)
90
- })
91
-
92
- bench('AS-JSON Parse Object', () => {
93
- JSON.parse<DynamicObject>('{"hello":"world"}')
94
- })
95
-
96
- bench('AS-JSON Stringify Dynamic Array', () => {
97
- JSON.stringify<unknown[]>(["Welcome to dynamic arrays", true])
98
- })
99
-
100
- bench('AS-JSON Parse Dynamic Array', () => {
101
- JSON.parse<unknown[]>('["Welcome to dynamic arrays",true]')
102
- })
103
-
104
- bench('AS-JSON Stringify Dynamic Array', () => {
105
- JSON.stringify<string[]>(["hello", "world"])
106
- })
107
- bench('AS-JSON Parse Dynamic Array', () => {
108
- JSON.parse<string[]>('["hello","hello"]')
109
- })
110
-
111
- /*
112
- bench('AssemblyScript-JSON Stringify String', () => {
113
- const encoder = new asJSON.JSONEncoder()
114
- encoder.setString(null, "Hello World")
115
- const binary = encoder.serialize()
116
- String.UTF8.decode(binary.buffer)
117
- // Its odd. Not encoded in UTF16 Strings.
118
- // Since AS-JSON returns a string, so does assemblyscript-json in this bench.
119
- // Or else, the bench is biased.
120
- })
121
-
122
- bench('AssemblyScript-JSON Parse String', () => {
123
- const decoder: asJSON.JSON.Str = <asJSON.JSON.Str>(asJSON.JSON.parse(Uint8Array.wrap(String.UTF8.encode('"Hello World"'))))
124
- decoder.valueOf()
125
- })
126
-
127
- bench('AssemblyScript-JSON Stringify Integer', () => {
128
- const encoder = new asJSON.JSONEncoder()
129
- encoder.setInteger(null, 14)
130
- const binary = encoder.serialize()
131
- String.UTF8.decode(binary.buffer)
132
- })
133
-
134
- bench('AssemblyScript-JSON Parse Integer', () => {
135
- const decoder: asJSON.JSON.Integer = <asJSON.JSON.Integer>(asJSON.JSON.parse(Uint8Array.wrap(String.UTF8.encode('14'))))
136
- decoder.valueOf()
137
- })
138
-
139
- bench('AssemblyScript-JSON Stringify Float', () => {
140
- const encoder = new asJSON.JSONEncoder()
141
- encoder.setFloat(null, 7.3)
142
- const binary = encoder.serialize()
143
- String.UTF8.decode(binary.buffer)
144
- })
145
-
146
- bench('AssemblyScript-JSON Parse Float', () => {
147
- const decoder: asJSON.JSON.Float = <asJSON.JSON.Float>(asJSON.JSON.parse(Uint8Array.wrap(String.UTF8.encode('7.3'))))
148
- decoder.valueOf()
149
- })
150
-
151
- bench('AssemblyScript-JSON Stringify Boolean', () => {
152
- const encoder = new asJSON.JSONEncoder()
153
- encoder.setBoolean(null, true)
154
- const binary = encoder.serialize()
155
- String.UTF8.decode(binary.buffer)
156
- })
157
-
158
- bench('AssemblyScript-JSON Parse Boolean', () => {
159
- const decoder: asJSON.JSON.Bool = <asJSON.JSON.Bool>(asJSON.JSON.parse(Uint8Array.wrap(String.UTF8.encode('true'))))
160
- decoder.valueOf()
161
- })
162
-
163
- bench('AssemblyScript-JSON Stringify Array', () => {
164
- const encoder = new asJSON.JSONEncoder()
165
- encoder.pushArray(null)
166
- encoder.setBoolean(null, true)
167
- encoder.setBoolean(null, false)
168
- encoder.setBoolean(null, true)
169
- encoder.popArray()
170
- const binary = encoder.serialize()
171
- String.UTF8.decode(binary.buffer)
172
- })
173
-
174
- bench('AssemblyScript-JSON Parse Array', () => {
175
- const decoder: asJSON.JSON.Arr = <asJSON.JSON.Arr>(asJSON.JSON.parse(Uint8Array.wrap(String.UTF8.encode('[true,false,true]'))))
176
- const result = decoder.valueOf()
177
- const pos0: asJSON.JSON.Bool = <asJSON.JSON.Bool>result.at(0)
178
- pos0.valueOf()
179
- const pos1: asJSON.JSON.Bool = <asJSON.JSON.Bool>result.at(1)
180
- pos1.valueOf()
181
- const pos2: asJSON.JSON.Bool = <asJSON.JSON.Bool>result.at(2)
182
- pos2.valueOf()
183
- })
184
-
185
- bench('AssemblyScript-JSON Stringify Object', () => {
186
- const encoder = new asJSON.JSONEncoder()
187
- encoder.pushObject(null)
188
- encoder.setBoolean('bool', true)
189
- encoder.popObject()
190
- const binary = encoder.serialize()
191
- String.UTF8.decode(binary.buffer)
192
- })
193
-
194
- bench('AssemblyScript-JSON Parse Object', () => {
195
- const decoder: asJSON.JSON.Obj = <asJSON.JSON.Obj>(asJSON.JSON.parse(Uint8Array.wrap(String.UTF8.encode('{"bool":true}'))))
196
- const result = decoder.getBool('bool')
197
- if (result !== null) result.valueOf()
198
- })
199
- */
@@ -1,175 +0,0 @@
1
- export enum unknownTypes {
2
- u8,
3
- u16,
4
- u32,
5
- u64,
6
- i8,
7
- i16,
8
- i32,
9
- i64,
10
- f32,
11
- f64,
12
- infinite,
13
- boolean,
14
- null,
15
- }
16
-
17
- // @ts-ignore
18
- export class unknown {
19
- public value: usize = 0
20
- public type: usize = 0
21
- public typeName: string = ''
22
- private f64: f64 = 0
23
- private f32: f32 = 0
24
- set<T>(data: T): void {
25
- if (data instanceof usize && data === null) {
26
- this.type = unknownTypes.null
27
- } else if (isBoolean<T>()) {
28
- this.value = data ? usize(1) : usize(0)
29
- this.type = unknownTypes.boolean
30
- } else if (isFloat<T>()) {
31
- if (data instanceof f32) {
32
- // @ts-ignore
33
- this.f32 = data
34
- this.type = unknownTypes.f32
35
- } else {
36
- // @ts-ignore
37
- this.f64 = data
38
- this.type = unknownTypes.f64
39
- }
40
- } else if (isInteger<T>()) {
41
- if (isSigned<T>()) {
42
- if (data instanceof i8) {
43
- this.value = usize(data)
44
- this.type = unknownTypes.i8
45
- } else if (data instanceof i16) {
46
- this.value = usize(data)
47
- this.type = unknownTypes.i16
48
- } else if (data instanceof i32) {
49
- this.value = usize(data)
50
- this.type = unknownTypes.i32
51
- } else if (data instanceof i64) {
52
- this.value = usize(data)
53
- this.type = unknownTypes.i64
54
- }
55
- } else {
56
- if (data instanceof u8) {
57
- this.value = usize(data)
58
- this.type = unknownTypes.u8
59
- } else if (data instanceof u16) {
60
- this.value = usize(data)
61
- this.type = unknownTypes.u16
62
- } else if (data instanceof u32) {
63
- this.value = usize(data)
64
- this.type = unknownTypes.u32
65
- } else if (data instanceof u64) {
66
- this.value = usize(data)
67
- this.type = unknownTypes.u64
68
- }
69
- }
70
- } else {
71
- this.value = changetype<usize>(data)
72
- this.type = idof<T>()
73
- }
74
- this.typeName = nameof<T>()
75
- }
76
- // @ts-ignore
77
- get<T>(): T {
78
- let type!: T
79
- if (isNullable<T>() && this.type === unknownTypes.null) {
80
- // @ts-ignore
81
- return null
82
- } if (isBoolean<T>()) {
83
- // @ts-ignore
84
- if (this.value === 1) return true
85
- // @ts-ignore
86
- else return false
87
- } else if (isFloat<T>()) {
88
- if (type instanceof f32) {
89
- // @ts-ignore
90
- return this.f32
91
- } else {
92
- // @ts-ignore
93
- return this.f64
94
- }
95
- } else if (isInteger<T>()) {
96
- if (isSigned<T>()) {
97
- if (type instanceof i8) {
98
- // @ts-ignore
99
- return i8(this.value)
100
- } else if (type instanceof i16) {
101
- // @ts-ignore
102
- return i16(this.value)
103
- } else if (type instanceof i32) {
104
- // @ts-ignore
105
- return i32(this.value)
106
- } else if (type instanceof i64) {
107
- // @ts-ignore
108
- return i64(this.value)
109
- }
110
- } else {
111
- if (type instanceof u8) {
112
- // @ts-ignore
113
- return u8(this.value)
114
- } else if (type instanceof u16) {
115
- // @ts-ignore
116
- return u16(this.value)
117
- } else if (type instanceof u32) {
118
- // @ts-ignore
119
- return u32(this.value)
120
- } else if (type instanceof u64) {
121
- // @ts-ignore
122
- return u64(this.value)
123
- }
124
- }
125
- } else {
126
- return changetype<T>(this.value)
127
- }
128
- }
129
- // @ts-ignore
130
- is<T>(): boolean {
131
- let type!: T
132
- if (isBoolean<T>()) {
133
- return this.type === unknownTypes.boolean
134
- } else if (isFloat<T>()) {
135
- if (type instanceof f32) {
136
- // @ts-ignore
137
- return this.type = unknownTypes.f32
138
- } else {
139
- // @ts-ignore
140
- return this.type === unknownTypes.f64
141
- }
142
- } else if (isInteger<T>()) {
143
- if (isSigned<T>()) {
144
- if (type instanceof i8) {
145
- return this.type === unknownTypes.i8
146
- } else if (type instanceof i16) {
147
- return this.type === unknownTypes.i16
148
- } else if (type instanceof i32) {
149
- return this.type === unknownTypes.i32
150
- } else if (type instanceof i64) {
151
- return this.type === unknownTypes.i64
152
- }
153
- } else {
154
- if (type instanceof u8) {
155
- return this.type === unknownTypes.u8
156
- } else if (type instanceof u16) {
157
- return this.type === unknownTypes.u16
158
- } else if (type instanceof u32) {
159
- return this.type === unknownTypes.u32
160
- } else if (type instanceof u64) {
161
- return this.type === unknownTypes.u64
162
- }
163
- }
164
- } else if (isNullable<T>() && this.type === unknownTypes.null) {
165
- return true
166
- } else {
167
- return this.type === idof<T>()
168
- }
169
- }
170
- static wrap<T>(data: T): unknown {
171
- const unk = new unknown()
172
- unk.set(data)
173
- return unk
174
- }
175
- }
package/bench/bench.js DELETED
@@ -1,102 +0,0 @@
1
- const fs = require("fs");
2
- const loader = require("@assemblyscript/loader");
3
- const { WASI } = require('wasi')
4
- const wasiOptions = {}
5
- const wasi = new WASI(wasiOptions)
6
- const imports = {
7
- wasi_snapshot_preview1: wasi.wasiImport
8
- };
9
- const wasmModule = loader.instantiateSync(fs.readFileSync(__dirname + "/output/bench.wasm"), imports);
10
- wasi.start(wasmModule)
11
-
12
-
13
- function bench(title, code) {
14
- let ops = 100_000
15
- const start = Date.now()
16
- while (ops--) {
17
- code()
18
- }
19
- const time = Date.now() - start
20
- if (time <= 0) {
21
- console.log(`${title}: ~${100_000 * 1000}.00 ops/s | ${(time * 100) / 100}ms`)
22
- } else {
23
- console.log(`${title}: ~${Math.round(((100_000 * 1000) / time) * 100) / 100} ops/s | ${(time * 100) / 100}ms`)
24
- }
25
- }
26
-
27
- const jsonData = {
28
- hello: "world"
29
- }
30
-
31
- Object
32
-
33
- bench('JSON Stringify String', () => {
34
- JSON.stringify('Hello World')
35
- })
36
-
37
- bench('JSON Parse String', () => {
38
- JSON.parse('"Hello World"')
39
- })
40
-
41
- bench('JSON Stringify Integer', () => {
42
- JSON.stringify(14)
43
- })
44
-
45
- bench('JSON Parse Integer', () => {
46
- JSON.parse('14')
47
- })
48
-
49
- bench('JSON Stringify Float', () => {
50
- JSON.stringify(7.3)
51
- })
52
-
53
- bench('JSON Parse Float', () => {
54
- JSON.parse('7.3')
55
- })
56
-
57
- bench('JSON Stringify Boolean', () => {
58
- JSON.stringify(true)
59
- })
60
-
61
- bench('JSON Parse Boolean', () => {
62
- JSON.parse('true')
63
- })
64
-
65
- bench('JSON Stringify Array', () => {
66
- JSON.stringify([true, false, true])
67
- })
68
-
69
- bench('JSON Parse Array', () => {
70
- JSON.parse('[true,false,true]')
71
- })
72
-
73
- bench('JSON Stringify Object', () => {
74
- JSON.stringify(jsonData)
75
- })
76
-
77
- bench('JSON Parse Object', () => {
78
- JSON.parse('{"hello":"world"}')
79
- })
80
-
81
- bench('JSON Stringify Dynamic Object', () => {
82
- JSON.stringify(jsonData)
83
- })
84
-
85
- bench('JSON Parse Object', () => {
86
- JSON.parse('{"hello":"world"}')
87
- })
88
-
89
- bench('JSON Stringify Dynamic Array', () => {
90
- JSON.stringify(["Welcome to dynamic arrays", true])
91
- })
92
-
93
- bench('JSON Parse Dynamic Array', () => {
94
- JSON.parse('["Welcome to dynamic arrays",true]')
95
- })
96
-
97
- bench('JSON Stringify Dynamic Array', () => {
98
- JSON.stringify(["hello", "world"])
99
- })
100
- bench('JSON Parse Dynamic Array', () => {
101
- JSON.parse('["hello","hello"]')
102
- })