Comparing version
@@ -1,2 +0,5 @@ | ||
import { JSON } from ".."; | ||
bench("1+1", () => { | ||
blackbox("1+1".split("+w")) | ||
}) | ||
/*import { JSON } from ".."; | ||
@@ -76,1 +79,2 @@ @json | ||
}); | ||
*/ |
@@ -34,3 +34,3 @@ import { JSON } from ".."; | ||
}); | ||
/* | ||
describe("Ser/de Numbers", () => { | ||
@@ -44,3 +44,3 @@ it("should ser/de integers", () => { | ||
canSerde<i64>(-101); | ||
/* | ||
canSerde<u128>(u128.from("0")) | ||
@@ -70,3 +70,3 @@ canSerde<u128>(u128.from("100")) | ||
canSerde<i128Safe>(i128Safe.from("-100")) | ||
canSerde<i128Safe>(i128Safe.from("-101"))*/ | ||
canSerde<i128Safe>(i128Safe.from("-101")) | ||
@@ -205,2 +205,2 @@ //canSerde<i256Safe>(new i256Safe(10, 11, 500, 501)) | ||
}); | ||
}); | ||
});*/ |
@@ -27,2 +27,4 @@ // Characters | ||
export const commaWord = ","; | ||
export const rightBracketWord = "]"; | ||
export const rightBracketWord = "]"; | ||
// Escape Codes | ||
export const newLineCode = "\n".charCodeAt(0); |
@@ -1,3 +0,4 @@ | ||
import { u128, u128Safe, u256, u256Safe, i128, i128Safe, i256Safe } from "as-bignum/assembly"; | ||
import { u128, u128Safe, u256, u256Safe, i128, i128Safe } from "as-bignum/assembly"; | ||
import { StringSink } from "as-string-sink/assembly"; | ||
import { itoa32, itoa64, dtoa, dtoa_buffered } from "util/number"; | ||
import { isSpace } from "util/string"; | ||
@@ -25,3 +26,3 @@ import { | ||
} from "./chars"; | ||
import { isBigNum, unsafeCharCodeAt } from "./util"; | ||
import { escapeChar, isBigNum, unsafeCharCodeAt } from "./util"; | ||
@@ -43,3 +44,48 @@ /** | ||
if (isString<T>()) { | ||
return '"' + (<string>data).replaceAll('"', '\\"') + '"'; | ||
let result = new StringSink("\""); | ||
// @ts-ignore | ||
for (let i = 0; i < data.length; i++) { | ||
// @ts-ignore | ||
switch (unsafeCharCodeAt(data, i)) { | ||
case 0x22: { | ||
result.write("\\\""); | ||
break; | ||
} | ||
case 0x5C: { | ||
result.write("\\\\"); | ||
break; | ||
} | ||
case 0x08: { | ||
result.write("\\b"); | ||
break; | ||
} | ||
case 0x0A: { | ||
result.write("\\n"); | ||
break; | ||
} | ||
case 0x0D: { | ||
result.write("\\r"); | ||
break; | ||
} | ||
case 0x09: { | ||
result.write("\\t"); | ||
break; | ||
} | ||
case 0x0C: { | ||
result.write("\\f"); | ||
break; | ||
} | ||
case 0x0B: { | ||
result.write("\\u000b"); | ||
break; | ||
} | ||
default: { | ||
// @ts-ignore | ||
result.write(data.charAt(i)); | ||
break; | ||
} | ||
} | ||
} | ||
result.write("\""); | ||
return result.toString(); | ||
} | ||
@@ -86,4 +132,2 @@ // Boolean | ||
return data.toString(); | ||
} else if (data instanceof Date) { | ||
return data.toISOString(); | ||
} else { | ||
@@ -101,4 +145,5 @@ throw new Error(`Could not serialize data of type ${nameof<T>()}. Invalid data provided.`); | ||
*/ | ||
// @ts-ignore | ||
export function parse<T>(data: string): T { | ||
let type!: T; | ||
let type: T; | ||
if (isString<T>()) { | ||
@@ -125,5 +170,2 @@ // @ts-ignore | ||
return parseBigNum<T>(data); | ||
} else if (type instanceof Date) { | ||
// @ts-ignore | ||
return Date.fromString(data); | ||
} else { | ||
@@ -134,4 +176,5 @@ // @ts-ignore | ||
} | ||
// @ts-ignore | ||
function parseObjectValue<T>(data: string): T { | ||
let type!: T; | ||
let type: T; | ||
if (isString<T>()) { | ||
@@ -154,5 +197,3 @@ // @ts-ignore | ||
} else if (isDefined(type.__JSON_Set_Key)) { | ||
// @ts-ignore | ||
//if (isNullable<T>()) return null; | ||
return parseObject<T>(data); | ||
return parseObject<T>(data.trimStart()); | ||
} else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) { | ||
@@ -167,7 +208,2 @@ // @ts-ignore | ||
} | ||
// @ts-ignore | ||
@unsafe | ||
export function createObjectUnsafe<T>(): T { | ||
return changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())) | ||
} | ||
} | ||
@@ -211,28 +247,26 @@ | ||
@inline | ||
function parseNumber<T>(data: string): T { | ||
let type: T; | ||
// @ts-ignore | ||
export function parseNumber<T>(data: string): T { | ||
// @ts-ignore | ||
if (type instanceof f64) return F64.parseFloat(data); | ||
const type: T = 0; | ||
// @ts-ignore | ||
else if (type instanceof f32) return F32.parseFloat(data); | ||
if (type instanceof f64) return f32.parse(data); | ||
// @ts-ignore | ||
else if (type instanceof u64) return U64.parseInt(data); | ||
else if (type instanceof f32) return f32.parse(data); | ||
// @ts-ignore | ||
else if (type instanceof u32) return U32.parseInt(data); | ||
else if (type instanceof u64) return u64.parse(data); | ||
// @ts-ignore | ||
else if (type instanceof u8) return U8.parseInt(data); | ||
else if (type instanceof u32) return u32.parse(data); | ||
// @ts-ignore | ||
else if (type instanceof u16) return U16.parseInt(data); | ||
else if (type instanceof u8) return u8.parse(data); | ||
// @ts-ignore | ||
else if (type instanceof i64) return I64.parseInt(data); | ||
else if (type instanceof u16) return u16.parse(data); | ||
// @ts-ignore | ||
else if (type instanceof i32) return I32.parseInt(data); | ||
else if (type instanceof i64) return i64.parse(data); | ||
// @ts-ignore | ||
else if (type instanceof i16) return I16.parseInt(data); | ||
else if (type instanceof i32) return i32.parse(data); | ||
// @ts-ignore | ||
else if (type instanceof i8) return I8.parseInt(data); | ||
throw new Error( | ||
`JSON: Cannot parse invalid data into a number. Either "${data}" is not a valid number, or <${nameof<T>()}> is an invald number type.` | ||
); | ||
else if (type instanceof i16) return i16.parse(data); | ||
// @ts-ignore | ||
else if (type instanceof i8) return i8.parse(data); | ||
} | ||
@@ -242,3 +276,3 @@ | ||
@inline | ||
export function parseObject<T>(data: string): T { | ||
function parseObject<T>(data: string): T { | ||
let schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())); | ||
@@ -360,5 +394,4 @@ let key = ""; | ||
// @ts-ignore | ||
export function parseArray<T extends unknown[]>(data: string): T { | ||
let type!: valueof<T>; | ||
if (type instanceof String) { | ||
function parseArray<T extends unknown[]>(data: string): T { | ||
if (isString<valueof<T>>()) { | ||
return <T>parseStringArray(data); | ||
@@ -375,3 +408,6 @@ } else if (isBoolean<valueof<T>>()) { | ||
// @ts-ignore | ||
} else if (isDefined(type.__JSON_Set_Key)) { | ||
} | ||
const type = instantiate<T>(); | ||
// @ts-ignore | ||
if (isDefined(type.__JSON_Set_Key)) { | ||
// @ts-ignore | ||
@@ -384,3 +420,3 @@ return parseObjectArray<T>(data); | ||
@inline | ||
export function parseStringArray(data: string): string[] { | ||
function parseStringArray(data: string): string[] { | ||
const result: string[] = []; | ||
@@ -405,3 +441,3 @@ let lastPos = 0; | ||
@inline | ||
export function parseBooleanArray<T extends boolean[]>(data: string): T { | ||
function parseBooleanArray<T extends boolean[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
@@ -434,3 +470,3 @@ let lastPos = 1; | ||
@inline | ||
export function parseNumberArray<T extends number[]>(data: string): T { | ||
function parseNumberArray<T extends number[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
@@ -461,3 +497,3 @@ let lastPos = 0; | ||
@inline | ||
export function parseArrayArray<T extends unknown[][]>(data: string): T { | ||
function parseArrayArray<T extends unknown[][]>(data: string): T { | ||
const result = instantiate<T>(); | ||
@@ -492,3 +528,3 @@ let char = 0; | ||
@inline | ||
export function parseObjectArray<T extends unknown[][]>(data: string): T { | ||
function parseObjectArray<T extends unknown[][]>(data: string): T { | ||
const result = instantiate<T>(); | ||
@@ -519,2 +555,2 @@ let char = 0; | ||
return result; | ||
} | ||
} |
@@ -8,3 +8,3 @@ import { StringSink } from "as-string-sink/assembly"; | ||
@inline | ||
export function isBigNum<T>(): boolean { | ||
export function isBigNum<T>(): boolean { | ||
if (idof<T>() == idof<u128>()) return true; | ||
@@ -47,1 +47,17 @@ if (idof<T>() == idof<u128Safe>()) return true; | ||
} | ||
// @ts-ignore | ||
@inline | ||
export function escapeChar(char: string): string { | ||
switch (unsafeCharCodeAt(char, 0)) { | ||
case 0x22: return '\\"'; | ||
case 0x5C: return "\\\\"; | ||
case 0x08: return "\\b"; | ||
case 0x0A: return "\\n"; | ||
case 0x0D: return "\\r"; | ||
case 0x09: return "\\t"; | ||
case 0x0C: return "\\f"; | ||
case 0x0B: return "\\u000b"; | ||
default: return char; | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
import { wasi_console } from "@assemblyscript/wasi-shim/assembly/wasi_console"; | ||
import { u128 } from "as-bignum/assembly"; | ||
@@ -33,3 +32,2 @@ import { | ||
lastActive!: i32[]; | ||
createdAt!: Date; | ||
age!: i32; | ||
@@ -45,3 +43,2 @@ pos!: Vec3 | null; | ||
lastActive: [8, 27, 2022], | ||
createdAt: Date.fromString("2021-12-08T00:59:26.230Z"), | ||
age: 23, | ||
@@ -61,4 +58,4 @@ pos: { | ||
const serializedPlayer = JSON.stringify<Player>(player); | ||
wasi_console.log("Serialized Player: " + serializedPlayer); | ||
console.log("Serialized Player: " + serializedPlayer); | ||
const deserializedPlayer = JSON.parse<Player>(serializedPlayer); | ||
wasi_console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer)); | ||
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer)); |
{ | ||
"name": "json-as", | ||
"version": "0.5.9", | ||
"version": "0.5.10", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -22,11 +22,11 @@ "types": "assembly/index.ts", | ||
"devDependencies": { | ||
"@as-pect/cli": "^7.0.7", | ||
"@as-pect/cli": "^8.0.0", | ||
"@as-tral/cli": "^1.2.0", | ||
"@assemblyscript/loader": "^0.21.3", | ||
"@assemblyscript/loader": "^0.25.0", | ||
"@assemblyscript/wasi-shim": "^0.1.0", | ||
"as-bignum": "^0.2.23", | ||
"assemblyscript": "^0.24.1", | ||
"assemblyscript-prettier": "^1.0.2", | ||
"prettier": "^2.7.1", | ||
"typescript": "^4.7.2" | ||
"assemblyscript": "^0.25.0", | ||
"assemblyscript-prettier": "^1.0.6", | ||
"prettier": "^2.8.1", | ||
"typescript": "^4.9.4" | ||
}, | ||
@@ -33,0 +33,0 @@ "dependencies": { |
@@ -43,5 +43,5 @@ # AS-JSON | ||
class Vec3 { | ||
x: f32; | ||
y: f32; | ||
z: f32; | ||
x!: f32; | ||
y!: f32; | ||
z!: f32; | ||
} | ||
@@ -52,8 +52,8 @@ | ||
class Player { | ||
firstName: string; | ||
lastName: string; | ||
lastActive: i32[]; | ||
age: i32; | ||
pos: Vec3 | null; | ||
isVerified: boolean; | ||
firstName!: string; | ||
lastName!: string; | ||
lastActive!: i32[]; | ||
age!: i32; | ||
pos!: Vec3 | null; | ||
isVerified!: boolean; | ||
} | ||
@@ -60,0 +60,0 @@ |
{ | ||
"name": "@json-as/transform", | ||
"version": "0.5.9", | ||
"version": "0.5.10", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -8,3 +8,5 @@ "main": "./lib/index.js", | ||
"contributors": [ | ||
"DogWhich" | ||
"DogWhich", | ||
"Josh Tenner", | ||
"Rom" | ||
], | ||
@@ -11,0 +13,0 @@ "license": "MIT", |
62356
25.42%25
4.17%1379
11.84%