Comparing version
@@ -15,4 +15,6 @@ { | ||
"./transform" | ||
] | ||
} | ||
], | ||
"bindings": "esm" | ||
}, | ||
"extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json" | ||
} |
@@ -43,2 +43,3 @@ import { u128, u128Safe, u256, u256Safe, i128, i128Safe } from "as-bignum/assembly"; | ||
if (isString<T>() && data != null) { | ||
// @ts-ignore | ||
return serializeString(data); | ||
@@ -68,2 +69,5 @@ } | ||
} | ||
else if (data instanceof Date) { | ||
return data.toISOString(); | ||
} | ||
// ArrayLike | ||
@@ -74,4 +78,6 @@ else if (isArrayLike<T>()) { | ||
return emptyArrayWord; | ||
// @ts-ignore | ||
} else if (isString<valueof<T>>()) { | ||
let result = "["; | ||
// @ts-ignore | ||
for (let i = 0; i < data.length - 1; i++) { | ||
@@ -82,5 +88,7 @@ // @ts-ignore | ||
} | ||
// @ts-ignore | ||
result += serializeString(unchecked(data[data.length - 1])); | ||
result += rightBracketWord; | ||
return result; | ||
// @ts-ignore | ||
} else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) { | ||
@@ -147,2 +155,5 @@ let result = new StringSink(leftBracketWord); | ||
return parseObject<T>(data.trimStart()); | ||
} else if (idof<nonnull<T>>() == idof<Date>()) { | ||
// @ts-ignore | ||
return Date.fromString(data); | ||
} else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) { | ||
@@ -177,2 +188,5 @@ // @ts-ignore | ||
return parseObject<T>(data.trimStart()); | ||
} else if (idof<nonnull<T>>() == idof<Date>()) { | ||
// @ts-ignore | ||
return Date.fromString(data); | ||
} else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) { | ||
@@ -179,0 +193,0 @@ // @ts-ignore |
@@ -1,108 +0,19 @@ | ||
import { u128 } from "as-bignum/assembly"; | ||
import { | ||
JSON | ||
} from "."; | ||
import { unsafeCharCodeAt } from "./src/util"; | ||
import { backSlashCode, quoteCode } from "./src/chars"; | ||
import { JSON } from "./src/json"; | ||
const exp = `["abcdefg","st\\"ring\\" w\\"\\"ith quotes\\"","string \\t\\r\\"with ran\\tdom spa\\nces and \\nnewlines\\n\\n\\n","string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\""]`; | ||
/*console.log(JSON.stringify([ | ||
"abcdefg", | ||
'st"ring" w""ith quotes"', | ||
'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n', | ||
'string with colon : comma , brace [ ] bracket { } and quote " and other quote "', | ||
]));*/ | ||
const str = "\\\"" | ||
function parseString(data: string): string { | ||
let result = ""; | ||
let last = 1; | ||
let char = 0; | ||
for (let i = 1; i < data.length - 1; i++) { | ||
// \\" | ||
if (unsafeCharCodeAt(data, i) === backSlashCode) { | ||
char = unsafeCharCodeAt(data, ++i); | ||
result += data.slice(last, i - 1) | ||
if (char === 34) { | ||
result += "\""; | ||
last = ++i; | ||
} else if (char === 110) { | ||
result += "\n"; | ||
last = ++i; | ||
// 92 98 114 116 102 117 | ||
} else if (char >= 92 && char <= 117) { | ||
if (char === 92) { | ||
result += "\\"; | ||
last = ++i; | ||
} else if (char === 98) { | ||
result += "\b"; | ||
last = ++i; | ||
} else if (char === 102) { | ||
result += "\f"; | ||
last = ++i; | ||
} else if (char === 114) { | ||
result += "\r"; | ||
last = ++i; | ||
} else if (char === 116) { | ||
result += "\t"; | ||
last = ++i; | ||
} else if (char === 117 && load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) === 27584753879220272) { | ||
result += "\u000b"; | ||
i += 4; | ||
last = ++i; | ||
} | ||
} | ||
} | ||
} | ||
result += data.slice(last, data.length - 1); | ||
return result; | ||
} | ||
console.log(parseString("\"Hello W\\\"orld!\"")); | ||
console.log(parseString("\"Hello New \\nWorld!\"")); | ||
console.log(parseString("\"\\t TAB Hello World!\"")); | ||
console.log(JSON.stringify(parseString("\"U000B \\u000b Hello World!\""))); | ||
console.log(load<u32>(changetype<usize>(str)).toString()); | ||
console.log(load<u8>(changetype<usize>(str)).toString()); | ||
console.log(load<u8>(changetype<usize>(str) + <usize>2).toString()); | ||
console.log("abcdefg"); | ||
console.log('st"ring" w""ith quotes"'); | ||
console.log('string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n'); | ||
console.log('string with colon : comma , brace [ ] bracket { } and quote " and other quote "'); | ||
console.log(JSON.stringify("abcdefg")); | ||
console.log(JSON.stringify('st"ring" w""ith quotes"')); | ||
console.log(JSON.stringify('string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n')); | ||
console.log(JSON.stringify('string with colon : comma , brace [ ] bracket { } and quote " and other quote "')); | ||
console.log(JSON.stringify("Hello W\"orld!")); | ||
console.log("U000B: \u000b") | ||
// @ts-ignore | ||
@JSON | ||
// @json or @serializable work here | ||
@json | ||
class Vec3 { | ||
x: f32 = 3.4; | ||
y: f32 = 1.2; | ||
z: f32 = 8.3; | ||
x!: f32; | ||
y!: f32; | ||
z!: f32; | ||
} | ||
// @ts-ignore | ||
@JSON | ||
class Stats extends Vec3 { | ||
wins: u128 | ||
loss: u128 | ||
} | ||
// @ts-ignore | ||
@json | ||
class Player { | ||
firstName: string; | ||
lastName: string; | ||
lastActive: i32[]; | ||
age: i32; | ||
isVerified: boolean; | ||
stats: Stats | ||
firstName!: string; | ||
lastName!: string; | ||
lastActive!: i32[]; | ||
age!: i32; | ||
pos!: Vec3 | null; | ||
isVerified!: boolean; | ||
} | ||
@@ -113,18 +24,22 @@ | ||
lastName: "West", | ||
lastActive: [ | ||
8, | ||
27, | ||
2022 | ||
], | ||
lastActive: [8, 27, 2022], | ||
age: 23, | ||
isVerified: true, | ||
stats: { | ||
wins: u128.fromString("443"), | ||
loss: u128.fromString("693") | ||
} | ||
pos: { | ||
x: 3.4, | ||
y: 1.2, | ||
z: 8.3 | ||
}, | ||
isVerified: true | ||
}; | ||
const serializedPlayer = JSON.stringify<Player>(player); | ||
console.log("Serialized Player: " + serializedPlayer); | ||
const deserializedPlayer = JSON.parse<Player>(serializedPlayer); | ||
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer)); | ||
const stringified = JSON.stringify<Player>(player); | ||
console.log(stringified); | ||
const parsed = JSON.parse<Player>(stringified); | ||
console.log(JSON.stringify(parsed)); | ||
const stringifiedDate = JSON.stringify(new Date(1677961186339)); | ||
console.log(stringifiedDate); | ||
const parsedDate = JSON.parse<Date>(stringifiedDate); | ||
console.log(JSON.stringify(parsedDate)); |
{ | ||
"name": "json-as", | ||
"version": "0.5.29", | ||
"version": "0.5.30", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -5,0 +5,0 @@ "types": "assembly/index.ts", |
@@ -5,10 +5,10 @@ # AS-JSON | ||
Probably the fastest JSON parser for AssemblyScript with many more optimizations coming down the pipeline. | ||
Probably the fastest JSON implementation for AssemblyScript with many more optimizations coming down the pipeline. | ||
## Installation | ||
```bash | ||
~ npm install json-as | ||
npm install json-as | ||
``` | ||
```bash | ||
~ npm install visitor-as | ||
npm install visitor-as --save-dev | ||
``` | ||
@@ -19,3 +19,3 @@ | ||
```bash | ||
~ npm install as-bignum | ||
npm install as-bignum | ||
``` | ||
@@ -45,3 +45,3 @@ | ||
// @json or @serializable work here | ||
@JSON | ||
@json | ||
class Vec3 { | ||
@@ -53,3 +53,3 @@ x!: f32; | ||
@JSON | ||
@json | ||
class Player { | ||
@@ -82,6 +82,26 @@ firstName!: string; | ||
# Notes | ||
## Notes | ||
Performance exceeds JavaScript JSON implementation by an average of 230% but this decreases with larger data packets. | ||
## Planned Features | ||
- [x] Serialize | ||
- [x] Objects | ||
- [x] Other Types | ||
- [ ] Dynamic Types | ||
- [x] Deserialize | ||
- [x] Objects | ||
- [x] Other Types | ||
- [ ] Dynamic Types | ||
- [ ] Streaming API | ||
- [ ] Whitespace support | ||
- [ ] Integrate features from SIMDJson | ||
- [x] Optimize | ||
- [x] Strings | ||
- [x] Int/Float | ||
- [x] Bool | ||
- [x] Object Serialization | ||
- [ ] Object Parsing | ||
- [ ] Arrays | ||
## Performance | ||
@@ -88,0 +108,0 @@ |
{ | ||
"name": "@json-as/transform", | ||
"version": "0.5.29", | ||
"version": "0.5.30", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -151,4 +151,4 @@ import { | ||
console.log(serializeFunc); | ||
console.log(setKeyFunc); | ||
//console.log(serializeFunc); | ||
//console.log(setKeyFunc); | ||
} | ||
@@ -155,0 +155,0 @@ visitSource(node: Source): void { |
118
20.41%69972
-2.58%1595
-3.8%