Comparing version
@@ -8,4 +8,23 @@ import { JSON } from ".."; | ||
function canDeser<T>(data: string, toBe: T): void { | ||
const deserialized = JSON.parse<T>(data); | ||
expect(deserialized).toStrictEqual(toBe); | ||
} | ||
function canSer<T>(data: T, toBe: string): void { | ||
const serialized = JSON.stringify<T>(data); | ||
expect(serialized).toBe(toBe); | ||
} | ||
// @ts-ignore | ||
@json | ||
class Map4 { | ||
a: string; | ||
b: string; | ||
c: string; | ||
d: string; | ||
} | ||
// @ts-ignore | ||
@json | ||
class Vec3 { | ||
@@ -156,1 +175,33 @@ x: f64; | ||
}); | ||
describe("Ser externals", () => { | ||
it("should serialize valid objects", () => { | ||
canSer<Map4>({ a: '\\', b: '}', c: '][', d: '"' }, '{"a":"\\\\","b":"}","c":"][","d":"\\""}') | ||
canSer<Vec3>({ x: 0.4, y: 1.4, z: 0 }, '{"x":0.4,"y":1.4,"z":0.0}') | ||
}) | ||
}); | ||
// @ts-ignore | ||
@json | ||
class HttpResp { | ||
statusCode: number; | ||
headers: Array<Array<string>>; | ||
body: string; | ||
} | ||
describe("Deser externals", () => { | ||
it("should deserialize valid JSON strings", () => { | ||
canDeser<Map4>('\n{"a":\r"\\\\",\n\r"b":"}","c":"][","d"\t:\t"\\""}', { a: '\\', b: '}', c: '][', d: '"' }) | ||
canDeser<Vec3>('{"x":0.4,"y":1.4,"z":0.0}', { x: 0.4, y: 1.4, z: 0 }) | ||
canDeser<HttpResp>('{"statusCode":200,"headers":[["Conn\\\\ection","close"],["Content-Length","375"],["ETag","W/\\"177-/Ihew5Z+fiI8NLbTM2Wyphl/PFY\\""]],\n"body":"{\\n \\\"args\\\": {},\\n \\\"headers\\\": {\\n \\\"content-length\\\": \\\"0\\\",\\n \\\"accept\\\": \\\"*/*\\\" \\n}}"}', | ||
{ | ||
statusCode: 200, | ||
headers: [ | ||
['Conn\\ection', 'close'], | ||
['Content-Length', '375'], | ||
['ETag', 'W/\"177-/Ihew5Z+fiI8NLbTM2Wyphl/PFY\"'] | ||
], | ||
body: '{\n "args": {},\n "headers": {\n "content-length": "0",\n "accept": "*/*" \n}}' | ||
}) | ||
}) | ||
}); |
@@ -410,2 +410,3 @@ import { StringSink } from "as-string-sink/assembly"; | ||
} else if (char === quoteCode) { | ||
let escaping = false; | ||
for ( | ||
@@ -417,16 +418,20 @@ let stringValueIndex = ++outerLoopIndex; | ||
const char = unsafeCharCodeAt(data, stringValueIndex); | ||
if ( | ||
char === quoteCode && | ||
unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode | ||
) { | ||
if (isKey === false) { | ||
key.reinst(data, outerLoopIndex, stringValueIndex); | ||
isKey = true; | ||
} else { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, stringValueIndex); | ||
isKey = false; | ||
if (char === backSlashCode && !escaping) { | ||
escaping = true; | ||
} else { | ||
if ( | ||
char === quoteCode && !escaping | ||
) { | ||
if (isKey === false) { | ||
key.reinst(data, outerLoopIndex, stringValueIndex); | ||
isKey = true; | ||
} else { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, stringValueIndex); | ||
isKey = false; | ||
} | ||
outerLoopIndex = ++stringValueIndex; | ||
break; | ||
} | ||
outerLoopIndex = ++stringValueIndex; | ||
break; | ||
escaping = false; | ||
} | ||
@@ -508,11 +513,18 @@ } | ||
let instr = false; | ||
let escaping = false; | ||
for (let i = 1; i < data.length - 1; i++) { | ||
if (unsafeCharCodeAt(data, i) === quoteCode) { | ||
if (instr === false) { | ||
instr = true; | ||
lastPos = i; | ||
} else if (unsafeCharCodeAt(data, i - 1) !== backSlashCode) { | ||
instr = false; | ||
result.push(parseString(data.slice(lastPos, i))); | ||
const char = unsafeCharCodeAt(data, i); | ||
if (char === backSlashCode && !escaping) { | ||
escaping = true; | ||
} else { | ||
if (char === quoteCode && !escaping) { | ||
if (instr === false) { | ||
instr = true; | ||
lastPos = i; | ||
} else { | ||
instr = false; | ||
result.push(parseString(data.slice(lastPos, i + 1))); | ||
} | ||
} | ||
escaping = false; | ||
} | ||
@@ -519,0 +531,0 @@ } |
{ | ||
"name": "json-as", | ||
"version": "0.5.61", | ||
"version": "0.5.62", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -12,3 +12,4 @@ "types": "assembly/index.ts", | ||
"Frankk Taylor", | ||
"lekiano" | ||
"lekiano", | ||
"Florian Guitton" | ||
], | ||
@@ -37,3 +38,3 @@ "license": "MIT", | ||
"assemblyscript": "^0.27.9", | ||
"assemblyscript-prettier": "^2.0.2", | ||
"assemblyscript-prettier": "^3.0.1", | ||
"benchmark": "^2.1.4", | ||
@@ -40,0 +41,0 @@ "kati": "^0.6.2", |
@@ -1,5 +0,3 @@ | ||
# AS-JSON | ||
<h1 align=center>JSON<h4 align=center>Implementation of JSON for modern AssemblyScript</h3></h1> | ||
JSON for AssemblyScript focused on performance, low-overhead, and ease-of-use. | ||
## Installation | ||
@@ -6,0 +4,0 @@ |
{ | ||
"name": "@json-as/transform", | ||
"version": "0.5.61", | ||
"version": "0.5.62", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
112529
1.74%2265
2.58%122
-1.61%