Skip to content

Commit

Permalink
release: v0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jun 15, 2024
1 parent 60333bb commit 3265671
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 191 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ v0.8.2 - Properties starting with `static` or `private` would be ignored
v0.8.3 - Dirty fix to issue #68. Add __JSON_Stringify callable to global scope.
v0.8.4 - Fix #71. Classes with the extending class overriding a property cause the property to be serialized twice.
v0.8.5 - Fix #73. Support for nullable primatives with Box<T> from as-container
v0.8.6 - Fix. Forgot to stash before publishing. Stash and push what should have been v0.8.5
v0.8.6 - Fix. Forgot to stash before publishing. Stash and push what should have been v0.8.5

v0.9.0 - Large update. Refactor all the code, nullable primitives, rewrite the transform, allow extensibility with @omit keywords, and fix a plethora of bugs
[UNRELEASED] v0.9.1 - Port JSON.Value from the `develop` branch to allow for union types, parsing of arbitrary data, and whatever the hell you want.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
██║ ██║███████║ ╚█████╔╝███████║╚██████╔╝██║ ╚████║
╚═╝ ╚═╝╚══════╝ ╚════╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═══╝

v0.8.6
v0.9.0
</pre>
</h3>

Expand All @@ -34,6 +34,8 @@ Alternatively, add it to your `asconfig.json`
}
```

If you'd like to see the code that the transform generates, run with `JSON_DEBUG=true`

## Usage

```js
Expand All @@ -53,6 +55,8 @@ class Player {
firstName!: string;
lastName!: string;
lastActive!: i32[];
// Drop in a code block, function, or expression that evaluates to a boolean
@omitif("this.age < 18")
age!: i32;
@omitnull()
pos!: Vec3 | null;
Expand All @@ -79,6 +83,10 @@ const parsed = JSON.parse<Player>(stringified);

If you use this project in your codebase, consider dropping a [star](https://github.com/JairusSW/as-json). I would really appreciate it!

## Notes

If you want a feature, drop an issue (and again, maybe a star). I'll likely add it in less than 7 days.

## Performance

Run or view the benchmarks [here](https://github.com/JairusSW/as-json/tree/master/bench)
Expand Down
18 changes: 5 additions & 13 deletions assembly/deserialize/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { aCode, backSlashCode, commaCode, eCode, fCode, lCode, leftBraceCode, le
import { isSpace } from "util/string";

// @ts-ignore: Decorator
@inline export function deserializeObject<T>(data: string, initializeDefaultValues: boolean): T {
@inline export function deserializeObject<T>(data: string): T {
const schema: nonnull<T> = changetype<nonnull<T>>(
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
);

// @ts-ignore
if (initializeDefaultValues) schema.__INITIALIZE();
schema.__INITIALIZE();

let key_start: i32 = 0;
let key_end: i32 = 0;
Expand Down Expand Up @@ -73,20 +73,12 @@ import { isSpace } from "util/string";
} else {
if (char === quoteCode && !escaping) {
if (isKey === false) {
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
if (containsCodePoint(data, backSlashCode, outerLoopIndex, stringValueIndex)) {
key_start = outerLoopIndex - 1;
key_end = stringValueIndex;
//console.log(`[KEY-00]: ${data.slice(outerLoopIndex - 1, stringValueIndex)}`)
} else {
key_start = outerLoopIndex;
key_end = stringValueIndex;
//console.log(`[KEY-01]: ${data.slice(outerLoopIndex, stringValueIndex)}`)
}
key_start = outerLoopIndex;
key_end = stringValueIndex;
isKey = true;
} else {
// @ts-ignore
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex - 1, stringValueIndex);
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex - 1, stringValueIndex + 1);
isKey = false;
}
outerLoopIndex = ++stringValueIndex;
Expand Down
6 changes: 3 additions & 3 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export namespace JSON {
*/

// @ts-ignore: Decorator
@inline export function parse<T>(data: string, initializeDefaultValues: boolean = false): T {
@inline export function parse<T>(data: string): T {
if (isString<T>()) {
// @ts-ignore
return deserializeString(data);
Expand All @@ -91,15 +91,15 @@ export namespace JSON {
// @ts-ignore
return deserializeArray<T>(data);
}
let type: nonnull<T> = changetype<nonnull<T>>(0);
let type: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
if (type instanceof Box) {
return deserializeBox<T>(data);
} else if (isNullable<T>() && data == nullWord) {
// @ts-ignore
return null;
// @ts-ignore
} else if (isDefined(type.__DESERIALIZE)) {
return deserializeObject<T>(data.trimStart(), initializeDefaultValues);
return deserializeObject<T>(data.trimStart());
} else if (type instanceof Map) {
return deserializeMap<T>(data.trimStart());
} else if (type instanceof Date) {
Expand Down
47 changes: 36 additions & 11 deletions assembly/test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
import { JSON } from ".";

@json
class BaseObject {
a: string;
constructor(a: string) {
this.a = a;
}
class Vec3 {
x: f32 = 0.0;
y: f32 = 0.0;
z: f32 = 0.0;
}

@json
class DerivedObject extends BaseObject {
b: string;
constructor(a: string, b: string) {
super(a);
this.b = b;
}
class Player {
@alias("first name")
firstName!: string;
lastName!: string;
lastActive!: i32[];
@omitif("this.age < 18")
age!: i32;
@omitnull()
pos!: Vec3 | null;
isVerified!: boolean;
}

const player: Player = {
firstName: "Emmet",
lastName: "West",
lastActive: [8, 27, 2022],
age: 13,
pos: {
x: 3.4,
y: 1.2,
z: 8.3
},
isVerified: true
};

const stringified = JSON.stringify<Player>(player);

const parsed = JSON.parse<Player>(stringified);

console.log("Stringified: " + stringified);
console.log("Parsed: " + JSON.stringify(parsed));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-as",
"version": "0.8.6",
"version": "0.8.7",
"description": "JSON encoder/decoder for AssemblyScript",
"types": "assembly/index.ts",
"author": "Jairus Tanaka",
Expand Down
Loading

0 comments on commit 3265671

Please sign in to comment.