-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,221 additions
and
880 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { isMap } from "../src/util"; | ||
import { deserializeArrayArray } from "./array/array"; | ||
import { deserializeBooleanArray } from "./array/bool"; | ||
import { deserializeFloatArray } from "./array/float"; | ||
import { deserializeIntegerArray } from "./array/integer"; | ||
import { deserializeMapArray } from "./array/map"; | ||
import { deserializeObjectArray } from "./array/object"; | ||
import { deserializeStringArray } from "./array/string"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeArray<T extends unknown[]>(data: string): T { | ||
if (isString<valueof<T>>()) { | ||
return <T>deserializeStringArray(data); | ||
} else if (isBoolean<valueof<T>>()) { | ||
// @ts-ignore | ||
return deserializeBooleanArray<T>(data); | ||
} else if (isInteger<valueof<T>>()) { | ||
// @ts-ignore | ||
return deserializeIntegerArray<T>(data); | ||
} else if (isFloat<valueof<T>>()) { | ||
// @ts-ignore | ||
return deserializeFloatArray<T>(data); | ||
} else if (isArrayLike<valueof<T>>()) { | ||
// @ts-ignore | ||
return deserializeArrayArray<T>(data); | ||
} else if (isMap<valueof<T>>()) { | ||
return deserializeMapArray<T>(data); | ||
} else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) { | ||
const type = changetype<nonnull<valueof<T>>>(0); | ||
// @ts-ignore | ||
if (isDefined(type.__JSON_Set_Key)) { | ||
return deserializeObjectArray<T>(data); | ||
} | ||
} | ||
|
||
throw new Error("Could not parse array of type " + nameof<T>() + "!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { leftBracketCode, rightBracketCode } from "../../src/chars"; | ||
import { JSON } from "../../src/json"; | ||
import { unsafeCharCodeAt } from "../../src/util"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeArrayArray<T extends unknown[][]>(data: string): T { | ||
const result = instantiate<T>(); | ||
let lastPos = 0; | ||
let depth = 0; | ||
let i = 1; | ||
// Find start of bracket | ||
//for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {} | ||
//i++; | ||
for (; i < data.length - 1; i++) { | ||
const char = unsafeCharCodeAt(data, i); | ||
if (char === leftBracketCode) { | ||
if (depth === 0) { | ||
lastPos = i; | ||
} | ||
// Shifting is 6% faster than incrementing | ||
depth++; | ||
} else if (char === rightBracketCode) { | ||
depth--; | ||
if (depth === 0) { | ||
i++; | ||
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i))); | ||
} | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { eCode, fCode, tCode } from "../../src/chars"; | ||
import { unsafeCharCodeAt } from "../../src/util"; | ||
import { deserializeBoolean } from "../bool"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeBooleanArray<T extends boolean[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
let lastPos = 1; | ||
for (let i = 1; i < data.length - 1; i++) { | ||
const char = unsafeCharCodeAt(data, i); | ||
if (char === tCode || char === fCode) { | ||
lastPos = i; | ||
} else if (char === eCode) { | ||
i++; | ||
result.push(deserializeBoolean(data.slice(lastPos, i))); | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { isSpace } from "util/string"; | ||
import { unsafeCharCodeAt } from "../../src/util"; | ||
import { commaCode, rightBracketCode } from "../../src/chars"; | ||
import { deserializeFloat } from "../float"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeFloatArray<T extends number[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
let lastPos = 0; | ||
let i = 1; | ||
let awaitingParse = false; | ||
for (; i < data.length; i++) { | ||
const char = unsafeCharCodeAt(data, i); | ||
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) { | ||
awaitingParse = true; | ||
lastPos = i; | ||
} else if (awaitingParse && (isSpace(char) || char == commaCode || char == rightBracketCode) && lastPos > 0) { | ||
awaitingParse = false; | ||
result.push(deserializeFloat<valueof<T>>(data.slice(lastPos, i))); | ||
lastPos = 0; | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { isSpace } from "util/string"; | ||
import { unsafeCharCodeAt } from "../../src/util"; | ||
import { commaCode, rightBracketCode } from "../../src/chars"; | ||
import { deserializeInteger } from "../integer"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeIntegerArray<T extends number[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
let lastPos = 0; | ||
let i = 1; | ||
let awaitingParse = false; | ||
for (; i < data.length; i++) { | ||
const char = unsafeCharCodeAt(data, i); | ||
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) { | ||
awaitingParse = true; | ||
lastPos = i; | ||
} else if (awaitingParse && (isSpace(char) || char == commaCode || char == rightBracketCode) && lastPos > 0) { | ||
awaitingParse = false; | ||
result.push(deserializeInteger<valueof<T>>(data.slice(lastPos, i))); | ||
lastPos = 0; | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { leftBraceCode, rightBraceCode } from "../../src/chars"; | ||
import { JSON } from "../../src/json"; | ||
import { unsafeCharCodeAt } from "../../src/util"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeMapArray<T extends unknown[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
let lastPos: u32 = 1; | ||
let depth: u32 = 0; | ||
for (let pos: u32 = 0; pos < <u32>data.length; pos++) { | ||
const char = unsafeCharCodeAt(data, pos); | ||
if (char === leftBraceCode) { | ||
if (depth === 0) { | ||
lastPos = pos; | ||
} | ||
depth++; | ||
} else if (char === rightBraceCode) { | ||
depth--; | ||
if (depth === 0) { | ||
pos++; | ||
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos))); | ||
//lastPos = pos + 2; | ||
} | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { leftBraceCode, rightBraceCode } from "../../src/chars"; | ||
import { JSON } from "../../src/json"; | ||
import { unsafeCharCodeAt } from "../../src/util"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeObjectArray<T extends unknown[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
let lastPos: u32 = 1; | ||
let depth: u32 = 0; | ||
for (let pos: u32 = 0; pos < <u32>data.length; pos++) { | ||
const char = unsafeCharCodeAt(data, pos); | ||
if (char === leftBraceCode) { | ||
if (depth === 0) { | ||
lastPos = pos; | ||
} | ||
depth++; | ||
} else if (char === rightBraceCode) { | ||
depth--; | ||
if (depth === 0) { | ||
pos++; | ||
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos))); | ||
//lastPos = pos + 2; | ||
} | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { backSlashCode, quoteCode } from "../../src/chars"; | ||
import { unsafeCharCodeAt } from "../../src/util"; | ||
import { deserializeString } from "../string"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeStringArray(data: string): string[] { | ||
const result: string[] = []; | ||
let lastPos = 0; | ||
let instr = false; | ||
let escaping = false; | ||
for (let i = 1; i < data.length - 1; 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(deserializeString(data, lastPos, i)); | ||
} | ||
} | ||
escaping = false; | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { fCode, tCode } from "../src/chars"; | ||
import { unsafeCharCodeAt } from "../src/util"; | ||
|
||
/** | ||
* Deserialize a string to type boolean | ||
* @param data data to parse | ||
* @returns boolean | ||
*/ | ||
// @ts-ignore: Decorator | ||
@inline export function deserializeBoolean(data: string, start: i32 = 0, end: i32 = 0): boolean { | ||
if (!end) end = data.length; | ||
const len = end - start; | ||
const ptr = changetype<usize>(data) + <usize>(start << 1); | ||
const firstChar = unsafeCharCodeAt(data, start); | ||
if (len === 4 && firstChar === tCode && load<u64>(ptr) === 28429475166421108) return true; | ||
else if (len === 5 && firstChar === fCode && load<u64>(ptr, 2) === 28429466576093281) return false; | ||
return false//ERROR(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { JSON } from "../src/json"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeBox<T extends Box<any>>(data: string): T { | ||
const instance = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))// as Box<usize>; | ||
const val = instance._val; | ||
instance._val = parseDirectInference(val, data); | ||
// @ts-ignore | ||
return changetype<T>(instance); | ||
} | ||
|
||
@inline function parseDirectInference<T>(type: T, data: string, initializeDefaultValues: boolean = false): T { | ||
return JSON.parse<T>(data, initializeDefaultValues) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// @ts-ignore | ||
@inline export function deserializeDate(dateTimeString: string): Date { | ||
// Use AssemblyScript's date parser | ||
const d = Date.fromString(dateTimeString); | ||
|
||
// Return a new object instead of the one that the parser returned. | ||
// This may seem redundant, but addreses the issue when Date | ||
// is globally aliased to wasi_Date (or some other superclass). | ||
return new Date(d.getTime()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @ts-ignore: Decorator | ||
@inline export function deserializeFloat<T>(data: string): T { | ||
// @ts-ignore | ||
const type: T = 0; | ||
// @ts-ignore | ||
if (type instanceof f64) return f64.parse(data); | ||
// @ts-ignore | ||
return f32.parse(data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { snip_fast } from "../src/util"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeInteger<T>(data: string): T { | ||
// @ts-ignore | ||
return snip_fast<T>(data); | ||
} |
Oops, something went wrong.