Skip to content

Commit

Permalink
map support
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed May 7, 2024
1 parent 67223e3 commit 45b3282
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
17 changes: 15 additions & 2 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export namespace JSON {
F64,
Boolean,
String,
Obj,
Array
}
export class Value {
Expand All @@ -56,6 +57,8 @@ export namespace JSON {
@inline static from<T>(value: T): JSON.Value {
if (value instanceof Variant) {
// Handle
} else if (value instanceof JSON.Value) {
return value;
}
const out = changetype<JSON.Value>(__new(offsetof<JSON.Value>(), idof<JSON.Value>()));
out.set<T>(value);
Expand All @@ -82,16 +85,22 @@ export namespace JSON {
} else if (value instanceof u64 || value instanceof i64) {
this.type = JSON.Types.U64;
store<T>(changetype<usize>(this), value, STORAGE);
}else if (value instanceof f32) {
} else if (value instanceof f32) {
this.type = JSON.Types.F64;
store<T>(changetype<usize>(this), value, STORAGE);
}else if (value instanceof f64) {
} else if (value instanceof f64) {
this.type = JSON.Types.F64;
store<T>(changetype<usize>(this), value, STORAGE);
} else if (isString<T>()) {
this.type = JSON.Types.String;
this.length = String.UTF8.byteLength(value as string);
store<T>(changetype<usize>(this), value, STORAGE);
} else if (value instanceof Map) {
if (idof<T>() !== idof<Map<string, JSON.Value>>()) {
throw new Error("Maps must be of type Map<string, JSON.Value>!");
}
this.type = JSON.Types.Obj;
store<T>(changetype<usize>(this), value, STORAGE);
} else if (isArray<T>()) {
// @ts-ignore: T satisfies constraints of any[]
this.type = JSON.Types.Array + getArrayDepth<T>(0);
Expand Down Expand Up @@ -172,6 +181,10 @@ export namespace JSON {
}
} else if (data instanceof JSON.Value) {
return serializeUnknown(data as JSON.Value, out);
// @ts-ignore
} else if (isDefined(data.__JSON_Serialize)) {
// @ts-ignore
return data.__JSON_Serialize(out);
}
}
/**
Expand Down
8 changes: 7 additions & 1 deletion assembly/serialize/array/unknown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSON } from "../..";
import { commaCode, rightBracketCode } from "../../src/chars";
import { commaCode, leftBracketCode, rightBracketCode } from "../../src/chars";
import { Sink } from "../../src/sink";
import { serializeUnknown } from "../unknown";

Expand All @@ -11,6 +11,12 @@ import { serializeUnknown } from "../unknown";
} else {
out = Sink.fromString("[");
}
} else {
out.writeCodePoint(leftBracketCode);
if (!data.length) {
out.writeCodePoint(rightBracketCode);
return out;
}
}

const end = data.length - 1;
Expand Down
4 changes: 2 additions & 2 deletions assembly/serialize/unknown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JSON } from "..";
import { Sink } from "../src/sink";
import { __atoi_fast } from "../src/util";
import { serializeUnknownArray } from "./array/unknown";
import { serializeFloat } from "./float";
import { serializeInteger } from "./integer";
import { serializeString } from "./string";
Expand Down Expand Up @@ -36,6 +37,5 @@ export function serializeUnknown(data: JSON.Value, out: Sink | null = null): Sin
return serializeFloat(data.get<f64>(), out);
}
}
// handle fail. skip, dump, crash
return unreachable();
return serializeUnknownArray(data.get<JSON.Value[]>(), out);
}
50 changes: 48 additions & 2 deletions assembly/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JSON } from ".";
import { Sink } from "./src/sink";

console.log(JSON.serialize("hello world!").toString());

Expand All @@ -17,5 +18,50 @@ console.log(JSON.serialize([
JSON.Value.from("world"),
JSON.Value.from(123),
JSON.Value.from("456"),
JSON.Value.from(7.89)
]).toString());
JSON.Value.from([
JSON.Value.from(7.89),
JSON.Value.from([])
])
]).toString());

const set = JSON.Value.from([
JSON.Value.from([]),
JSON.Value.from([
JSON.Value.from([])
]),
JSON.Value.from([
JSON.Value.from([]),
JSON.Value.from([
JSON.Value.from([])
]),
]),
]);

console.log(JSON.serialize(set).toString());

@unmanaged
class Vec3 {
x: f64;
y: f64;
z: f64;
@inline __JSON_Serialize(out: Sink | null = null): Sink {
if (!out) {
out = Sink.withCapacity(25);
}

out.write(this.__JSON_Serialize_Unsafe());

return out;
}
@inline __JSON_Serialize_Unsafe(): string {
return `{"x":${JSON.serialize<f64>(this.x)},"y":${JSON.serialize<f64>(this.y)},"z":${JSON.serialize<f64>(this.z)}}`;
}
}

const vec: Vec3 = {
x: 3.4,
y: 1.2,
z: -5.6
}

console.log(JSON.serialize(vec).toString())

0 comments on commit 45b3282

Please sign in to comment.