Comparing version
@@ -15,5 +15,5 @@ { | ||
"bindings": "esm", | ||
"extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json" | ||
"exportStart": "_start" | ||
}, | ||
"extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json" | ||
} |
@@ -47,3 +47,3 @@ import { JSON } from ".."; | ||
canSerde<u128>(u128.from("101")) | ||
` | ||
canSerde<u128Safe>(u128Safe.from("0")) | ||
@@ -50,0 +50,0 @@ canSerde<u128Safe>(u128Safe.from("100")) |
@@ -84,2 +84,4 @@ import { u128, u128Safe, u256, u256Safe, i128, i128Safe, i256Safe } from "as-bignum/assembly"; | ||
return data.toString(); | ||
} else if (data instanceof Date) { | ||
return data.toISOString(); | ||
} else { | ||
@@ -156,11 +158,7 @@ throw new Error(`Could not serialize data of type ${nameof<T>()}. Invalid data provided.`); | ||
} | ||
/*export class Arr extends Array<Variant> { | ||
public data: Variant[] = []; | ||
push<T>(data: T): i32 { | ||
return this.data.push(Variant.from<T>(data)); | ||
} | ||
at<T>(index: i32): T { | ||
return this.data.at(index).get<T>(); | ||
} | ||
}*/ | ||
// @ts-ignore | ||
@unsafe | ||
export function createObjectUnsafe<T>(): T { | ||
return changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())) | ||
} | ||
} | ||
@@ -167,0 +165,0 @@ |
@@ -1,7 +0,42 @@ | ||
import { wasi_console } from "@assemblyscript/wasi-shim/assembly/wasi_console" | ||
import { u128 } from "as-bignum/assembly"; | ||
import { wasi_console } from "@assemblyscript/wasi-shim/assembly/wasi_console"; | ||
import { | ||
JSON | ||
} from "."; | ||
@json | ||
class Player { | ||
firstName: string; | ||
lastName: string; | ||
lastActive: i32[]; | ||
age: i32; | ||
pos: Vec3 | null; | ||
isVerified: boolean; | ||
stats: Stats | ||
} | ||
@json | ||
class Contacts { | ||
type: string | ||
player: string | ||
} | ||
const player = JSON.createObjectUnsafe<Player>() | ||
player.firstName = "John"; | ||
player.lastName = "West"; | ||
player.age = 23; | ||
const contact: Contacts = { | ||
player: JSON.stringify(player), | ||
type: "friends" | ||
} | ||
let stringifiedContact = JSON.stringify(contact); | ||
console.log("Input (Should see backslashes logged): " + stringifiedContact); | ||
const contacts = JSON.parse<Contacts>(stringifiedContact) | ||
console.log("Player: " + contacts.player); | ||
console.log("Type: " + contacts.type); | ||
const parsedPlayer = JSON.parse<Player>(contacts.player); | ||
console.log("Final Player: " + JSON.stringify(parsedPlayer)); | ||
console.log("Final Result (Contacts): " + JSON.stringify(contacts));/* | ||
/* | ||
// @ts-ignore | ||
@@ -12,3 +47,3 @@ @json | ||
loss: u128 | ||
} | ||
}*/ | ||
// @ts-ignore | ||
@@ -21,5 +56,18 @@ @json | ||
} | ||
// @ts-ignore | ||
@json | ||
class Test { | ||
data: string | ||
} | ||
const vec: Vec3 = { | ||
x: 3.4, | ||
y: 1.2, | ||
z: 8.3 | ||
} | ||
const test: Test = { | ||
data: JSON.stringify(vec) | ||
} | ||
/* | ||
// @ts-ignore | ||
@json | ||
class Player { | ||
@@ -51,6 +99,8 @@ firstName: string; | ||
}; | ||
const serializedPlayer = JSON.stringify<Player>(player); | ||
*//* | ||
const serializedPlayer = JSON.stringify<Test>(test); | ||
wasi_console.log("Serialized Player: " + serializedPlayer); | ||
const deserializedPlayer = JSON.parse<Player>(serializedPlayer); | ||
const deserializedPlayer = JSON.parse<Test>(serializedPlayer); | ||
wasi_console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer)); | ||
wasi_console.log("Deserialize Vec3: " + JSON.stringify(JSON.parse<Vec3>(deserializedPlayer.data))) | ||
*/ |
{ | ||
"extends": "assemblyscript/std/assembly.json", | ||
"compilerOptions": { | ||
"experimentalDecorators": true | ||
}, | ||
"include": [ | ||
@@ -7,0 +4,0 @@ "./**/*.ts" |
{ | ||
"name": "json-as", | ||
"version": "0.5.7", | ||
"version": "0.5.8", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -14,3 +14,3 @@ "types": "assembly/index.ts", | ||
"bench:astral": "astral", | ||
"build:test": "asc assembly/test.ts --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --transform \"./transform\" --exportStart _start -o ./build/test.wasm", | ||
"build:test": "asc assembly/test.ts --target test", | ||
"build:transform": "tsc -p ./transform", | ||
@@ -20,10 +20,15 @@ "test:wasmtime": "wasmtime ./build/test.wasm", | ||
"test:wasm3": "wasm3 ./build/test.wasm", | ||
"prettier": "as-prettier -w ." | ||
"prettier": "as-prettier -w .", | ||
"asbuild:debug": "asc assembly/index.ts --target debug", | ||
"asbuild:release": "asc assembly/index.ts --target release", | ||
"asbuild": "yarn asbuild:debug && yarn asbuild:release", | ||
"test": "node tests", | ||
"start": "npx serve ." | ||
}, | ||
"devDependencies": { | ||
"@as-pect/cli": "^7.0.7", | ||
"@as-tral/cli": "^1.1.1", | ||
"@as-tral/cli": "^1.2.0", | ||
"@assemblyscript/loader": "^0.21.3", | ||
"@assemblyscript/wasi-shim": "^0.1.0", | ||
"@serial-as/json": "^2.0.0", | ||
"as-bignum": "^0.2.23", | ||
"assemblyscript": "^0.24.1", | ||
@@ -54,3 +59,9 @@ "assemblyscript-prettier": "^1.0.2", | ||
"homepage": "https://github.com/JairusSW/as-json#readme", | ||
"type": "module" | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"import": "./build/release.js", | ||
"types": "./build/release.d.ts" | ||
} | ||
} | ||
} |
# AS-JSON | ||
 | ||
 | ||
## Features | ||
Full | ||
## Installation | ||
@@ -93,3 +89,16 @@ | ||
# FAQ | ||
**Does it support the JSON specification?** | ||
Yes, it does. However, dynamic objects and arrays are not supported, but planned in the near future. | ||
**Is it fast?** | ||
Look below | ||
**How does it compare to other librarys?** | ||
Its pretty much the same as the other libraries out there (near/assemblyscript-json and @serial-as/json), but it focuses highly on performance | ||
**Will it catch invalid JSON?** | ||
No, it does not check for invalid JSON, but gives its best shot at parsing instead. Will probably throw an error. | ||
## Performance | ||
@@ -96,0 +105,0 @@ |
@@ -78,3 +78,3 @@ import { ClassDecorator, registerDecorator, } from "visitor-as/dist/decorator.js"; | ||
this.visit(node.members); | ||
const serializedProp = '__JSON_Serialized: string = "";'; | ||
// const serializedProp = '__JSON_Serialized: string = "";'; | ||
let serializeFunc = ""; | ||
@@ -105,4 +105,7 @@ if (this.currentClass.encodeStmts.length > 0) { | ||
//console.log(serializeFunc) | ||
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node); | ||
node.members.push(serializedProperty); | ||
//const serializedProperty = SimpleParser.parseClassMember( | ||
// serializedProp, | ||
// node | ||
//); | ||
//node.members.push(serializedProperty); | ||
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node); | ||
@@ -109,0 +112,0 @@ node.members.push(serializeMethod); |
{ | ||
"name": "@json-as/transform", | ||
"version": "0.5.7", | ||
"version": "0.5.8", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -94,3 +94,3 @@ import { | ||
const serializedProp = '__JSON_Serialized: string = "";'; | ||
// const serializedProp = '__JSON_Serialized: string = "";'; | ||
@@ -127,7 +127,7 @@ let serializeFunc = ""; | ||
//console.log(serializeFunc) | ||
const serializedProperty = SimpleParser.parseClassMember( | ||
serializedProp, | ||
node | ||
); | ||
node.members.push(serializedProperty); | ||
//const serializedProperty = SimpleParser.parseClassMember( | ||
// serializedProp, | ||
// node | ||
//); | ||
//node.members.push(serializedProperty); | ||
@@ -134,0 +134,0 @@ const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node); |
50880
3.95%24
9.09%1270
3.76%119
8.18%