json-as 0.2.4 → 0.2.5

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.
package/assembly/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { console, stringify } from '../node_modules/as-console/assembly/wasi'
1
2
  import { StringSink } from 'as-string-sink'
2
3
  import { DynamicObject } from './DynamicObject'
3
4
  import { unknown, unknownTypes } from './unknown'
@@ -52,6 +53,8 @@ const WS5code = '\u0009'.charCodeAt(0)
52
53
  const unknownTrue = unknown.wrap(true)
53
54
  const unknownFalse = unknown.wrap(false)
54
55
  const unknownNull = unknown.wrap(null)
56
+ const fwd_slash = "\\"
57
+ const empty_stringCode = " ".charCodeAt(0)
55
58
 
56
59
  /**
57
60
  * JSON Encoder/Decoder for AssemblyScript
@@ -667,6 +670,7 @@ export function parseArrayArray<T extends Array<unknown>>(data: string): T {
667
670
 
668
671
  export function parseObject<T>(data: string): T {
669
672
  //console.log('Data ' + data)
673
+ data = removeJSONWhitespace(data)
670
674
  const len: u32 = data.length - 1
671
675
  let schema: T
672
676
  const result = new Map<string, string>()
@@ -684,7 +688,7 @@ export function parseObject<T>(data: string): T {
684
688
  if (char === rcbracketCode || char === rbracketCode) fdepth++
685
689
  }
686
690
  if (depth !== 0 && depth === fdepth) {
687
- //console.log(`Deep: ${prependType(data.slice(lastPos + 1, i + 1))}`)
691
+ //console.log(`Deep: ${data.slice(lastPos + 1, i + 1)}`)
688
692
  result.set(key, data.slice(lastPos + 1, i + 1).trim())
689
693
  // Reset the depth
690
694
  depth = 0
@@ -694,20 +698,22 @@ export function parseObject<T>(data: string): T {
694
698
  }
695
699
  if (depth === 0) {
696
700
  if (char === colonCode) {
697
- //console.log(`Key: ${prependType(data.slice(lastPos + 1, i - 1))}`)
701
+ //console.log(`Key: ${data.slice(lastPos + 1, i - 1)}`)
698
702
  key = data.slice(lastPos + 1, i - 1).trim()
699
703
  lastPos = i
700
704
  }
701
705
  else if (char === commaCode) {
702
- //console.log(`Value: ${prependType(data.slice(lastPos + 1, i))}`)
706
+ //console.log(`Value: ${data.slice(lastPos + 1, i)}`)
703
707
  if ((i - lastPos) > 0) result.set(key, data.slice(lastPos + 1, i).trim())
704
708
  lastPos = i + 1
705
709
  }
706
710
  }
707
711
  }
708
- //console.log(`Trailing: ${prependType(data.slice(lastPos + 1, len))}\n\t\sValid: ${data.slice(lastPos + 1, len).length > 0}`)
712
+ //console.log(`Trailing: ${data.slice(lastPos + 1, len)}\n\t\sValid: ${data.slice(lastPos + 1, len).length > 0}`)
709
713
 
710
714
  if ((len - lastPos) > 0) result.set(key, data.slice(lastPos + 1, len).trim())
715
+ console.log(result.keys())
716
+ console.log(result.values())
711
717
  // @ts-ignore
712
718
  return schema.__decode(result)
713
719
  }
@@ -754,4 +760,22 @@ export function parseDynamicObject(data: string): DynamicObject {
754
760
  // @ts-ignore
755
761
  o.__data = result
756
762
  return o
763
+ }
764
+
765
+ export function removeJSONWhitespace(data: string): string {
766
+ let result = new StringSink()
767
+ let instr = false
768
+ let char = 0
769
+ for (let i = 0; i < data.length; i++) {
770
+ char = data.charCodeAt(i)
771
+ if (char === quoteCode && data.charCodeAt(i - 1) === fwd_slashCode) {
772
+ instr = !instr
773
+ }
774
+ if (instr === true) {
775
+ result.writeCodePoint(char)
776
+ } else if (instr === false && char !== empty_stringCode) {
777
+ result.writeCodePoint(char)
778
+ }
779
+ }
780
+ return result.toString()
757
781
  }
package/assembly/test.ts CHANGED
@@ -2,7 +2,7 @@ import { unknown, unknownTypes } from './unknown'
2
2
 
3
3
  import { console, stringify } from "../node_modules/as-console/assembly/wasi"
4
4
 
5
- import { JSON, parseDynamicObject, parseUnknown, parseUnknownArray } from '.'
5
+ import { JSON, parseDynamicObject, parseUnknown, parseUnknownArray, removeJSONWhitespace } from '.'
6
6
 
7
7
  import { DynamicObject } from './DynamicObject'
8
8
  /*
@@ -187,6 +187,21 @@ check<EmptySchema>('Encode/Decode object', emptyObj)
187
187
  //console.log(JSON.stringify(["Welcome to dynamic arrays", 3.14, ["Very", ["Deep", ["Array"]]], true, "It also supports nulls", null]))
188
188
 
189
189
  //console.log(JSON.stringify(JSON.parse<unknown[]>('["Welcome to dynamic arrays",3.14,["Very",["Deep",["Array"]]],true,"It also supports nulls",null]')))
190
- const foo = new Map()
190
+ //const foo = new Map()
191
191
 
192
- //console.log(JSON.stringify(parseDynamicObject('{"hello":"world"}')))
192
+ //console.log(JSON.stringify(parseDynamicObject('{"hello":"world"}')))
193
+ // @ts-ignore
194
+ @json
195
+ class Test {
196
+ stuff: string
197
+ things: i32
198
+ }
199
+
200
+ const test: Test = {
201
+ stuff: "hi",
202
+ things: 42
203
+ }
204
+
205
+ console.log(stringify(JSON.stringify(test)))
206
+ console.log(removeJSONWhitespace('{"stuff":"hi", "things":42}'))
207
+ console.log(stringify(JSON.stringify(JSON.parse<Test>('{"stuff":"hi", "things":42}'))))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "ascMain": "assembly/index.ts",
@@ -95,6 +95,7 @@ class JSONTransformer extends BaseVisitor {
95
95
  const className = this.currentClass!.name.text
96
96
  if (!this.encodeStmts.has(className)) this.encodeStmts.set(className, [])
97
97
  if (!this.decodeCode.has(className)) this.decodeCode.set(className, [])
98
+ // TODO: fix later
98
99
  // @ts-ignore
99
100
  this.encodeStmts.get(className).push(
100
101
  `this.__encoded += '' + '"' + '${name}' + '"' + ':' + JSON.stringify<${type}>(this.${name}) + ',';`