Comparing version
@@ -13,2 +13,3 @@ import { StringSink } from "as-string-sink/assembly"; | ||
} from "./chars"; | ||
import { removeWhitespace } from "./util"; | ||
@@ -81,2 +82,3 @@ /** | ||
export function parse<T = Variant>(data: string): T { | ||
data = removeWhitespace(data); | ||
let type!: T; | ||
@@ -83,0 +85,0 @@ if (isString<T>()) { |
import "wasi"; | ||
import { JSON } from "."; | ||
import { removeWhitespace } from "./util"; | ||
@@ -44,5 +45,22 @@ // @ts-ignore | ||
console.log(`Stringified: ${stringified}`); | ||
data.age = 16 | ||
console.log(`Stringified2: ${JSON.stringify<Player>(data)}`); | ||
const parsed = JSON.parse<Player>(stringified); | ||
console.log(`Whitespace: ${removeWhitespace(`{ | ||
"firstName": "Emmet", | ||
"lastName": "West", | ||
"lastActive": [8, 27, 2022], | ||
"age": 23, | ||
"pos": { | ||
"x": -3.4000000953674318, | ||
"y": 1.2000000476837159 | ||
} | ||
}`)}`) | ||
const parsed = JSON.parse<Player>(`{ | ||
"firstName": "Emmet", | ||
"lastName": "West", | ||
"lastActive": [8, 27, 2022], | ||
"age": 23, | ||
"pos": { | ||
"x": -3.4000000953674318, | ||
"y": 1.2000000476837159 | ||
} | ||
}`); | ||
// Player { | ||
@@ -49,0 +67,0 @@ // firstName: "Emmet", |
@@ -1,14 +0,21 @@ | ||
export function isNumberCode(data: i32): boolean { | ||
if (data == "0".charCodeAt(0)) return true; | ||
else if (data == "1".charCodeAt(0)) return true; | ||
else if (data == "2".charCodeAt(0)) return true; | ||
else if (data == "3".charCodeAt(0)) return true; | ||
else if (data == "4".charCodeAt(0)) return true; | ||
else if (data == "5".charCodeAt(0)) return true; | ||
else if (data == "6".charCodeAt(0)) return true; | ||
else if (data == "7".charCodeAt(0)) return true; | ||
else if (data == "8".charCodeAt(0)) return true; | ||
else if (data == "9".charCodeAt(0)) return true; | ||
else if (data == "-".charCodeAt(0)) return true; | ||
else return false; | ||
import { StringSink } from "as-string-sink/assembly"; | ||
import { isSpace } from "assemblyscript/std/assembly/util/string"; | ||
import { backSlashCode, quoteCode } from "./chars"; | ||
export function removeWhitespace(data: string): string { | ||
const result = new StringSink() | ||
let instr = false; | ||
for (let i = 0; i < data.length; i++) { | ||
const char = data.charCodeAt(i); | ||
if (instr === false && char === quoteCode) instr = true | ||
else if (instr === true && char === quoteCode && data.charCodeAt(i - 1) !== backSlashCode) instr = false; | ||
if (instr === false) { | ||
if (!isSpace(char)) result.write(data.charAt(i)) | ||
} else { | ||
result.write(data.charAt(i)) | ||
} | ||
} | ||
return result.toString() | ||
} |
{ | ||
"name": "json-as", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -5,0 +5,0 @@ "types": "assembly/index.ts", |
@@ -105,3 +105,3 @@ # AS-JSON | ||
- Does this support whitespace? | ||
No, as-json does not support whitespace yet. That will come once we find a performant way to work around whitespace. | ||
Yes, as-json supports whitespace although the current implementation is deathly slow | ||
- How fast is it? | ||
@@ -108,0 +108,0 @@ Really fast. For example, here are some benchmarks for ser/de a Vec2 with as-json |
@@ -61,3 +61,3 @@ import { ClassDecorator, registerDecorator } from "visitor-as/dist/decorator.js"; | ||
this.decodeStmts = []; | ||
console.log(serializeFunc, deserializeFunc); | ||
//console.log(serializeFunc, deserializeFunc) | ||
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node); | ||
@@ -64,0 +64,0 @@ node.members.push(serializedProperty); |
{ | ||
"name": "@json-as/transform", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -80,3 +80,3 @@ import { | ||
this.decodeStmts = []; | ||
console.log(serializeFunc, deserializeFunc) | ||
//console.log(serializeFunc, deserializeFunc) | ||
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node); | ||
@@ -83,0 +83,0 @@ node.members.push(serializedProperty); |
48561
1.12%1246
1.96%