项目作者: aspkg

项目描述 :
JSON serialization/deserialization for AssemblyScript
高级语言: TypeScript
项目地址: git://github.com/aspkg/as-json.git
创建时间: 2021-05-13T16:47:41Z
项目社区:https://github.com/aspkg/as-json

开源协议:MIT License

下载




  1. ██ ███████ ██████ ███ ██ █████ ███████
    ██ ██ ██ ██ ████ ██ ██ ██ ██
    ██ ███████ ██ ██ ██ ██ ██ █████ ███████ ███████
    ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
    █████ ███████ ██████ ██ ████ ██ ██ ███████

    AssemblyScript - v1.0.3

📝 About

JSON is the de-facto serialization format of modern web applications, but its serialization and deserialization remain a significant performance bottleneck, especially at scale. Traditional parsing approaches are computationally expensive, adding unnecessary overhead to both clients and servers. This library is designed to mitigate this by leveraging SIMD acceleration and highly optimized transformations.

🔭 What’s new

🔹Major performance improvements and addition of SIMD

🔹Near zero-growth allocation design and low overhead

🔹Support for custom serializer and deserializers

🔹Fixes to many, many, bugs and edge cases

🔹Support for dynamic objects, arrays, arbitrary values, and raw types

📚 Contents

💾 Installation

  1. npm install json-as

Add the --transform to your asc command (e.g. in package.json)

  1. --transform json-as/transform

Alternatively, add it to your asconfig.json

  1. {
  2. "options": {
  3. "transform": ["json-as/transform"]
  4. }
  5. }

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

🪄 Usage

  1. import { JSON } from "json-as";
  2. @json
  3. class Vec3 {
  4. x: f32 = 0.0;
  5. y: f32 = 0.0;
  6. z: f32 = 0.0;
  7. }
  8. @json
  9. class Player {
  10. @alias("first name")
  11. firstName!: string;
  12. lastName!: string;
  13. lastActive!: i32[];
  14. // Drop in a code block, function, or expression that evaluates to a boolean
  15. @omitif((self: Player) => self.age < 18)
  16. age!: i32;
  17. @omitnull()
  18. pos!: Vec3 | null;
  19. isVerified!: boolean;
  20. }
  21. const player: Player = {
  22. firstName: "Jairus",
  23. lastName: "Tanaka",
  24. lastActive: [3, 9, 2025],
  25. age: 18,
  26. pos: {
  27. x: 3.4,
  28. y: 1.2,
  29. z: 8.3,
  30. },
  31. isVerified: true,
  32. }
  33. const serialized = JSON.stringify<Player>(player);
  34. const deserialized = JSON.parse<Player>(serialized);
  35. console.log("Serialized " + serialized);
  36. console.log("Deserialized " + JSON.stringify(deserialized));

🔍 Examples

🏷️ Omitting Fields

This library allows selective omission of fields during serialization using the following decorators:

@omit

This decorator excludes a field from serialization entirely.

  1. @json
  2. class Example {
  3. name!: string;
  4. @omit
  5. SSN!: string;
  6. }
  7. const obj = new Example();
  8. obj.name = "Jairus";
  9. obj.SSN = "123-45-6789";
  10. console.log(JSON.stringify(obj)); // { "name": "Jairus" }

@omitnull

This decorator omits a field only if its value is null.

  1. @json
  2. class Example {
  3. name!: string;
  4. @omitnull()
  5. optionalField!: string | null;
  6. }
  7. const obj = new Example();
  8. obj.name = "Jairus";
  9. obj.optionalField = null;
  10. console.log(JSON.stringify(obj)); // { "name": "Jairus" }

@omitif((self: this) => condition)

This decorator omits a field based on a custom predicate function.

  1. @json
  2. class Example {
  3. name!: string;
  4. @omitif((self: Example) => self.age <= 18)
  5. age!: number;
  6. }
  7. const obj = new Example();
  8. obj.name = "Jairus";
  9. obj.age = 18;
  10. console.log(JSON.stringify(obj)); // { "name": "Jairus" }
  11. obj.age = 99;
  12. console.log(JSON.stringify(obj)); // { "name": "Jairus", "age": 99 }

If age were higher than 18, it would be included in the serialization.

🗳️ Using nullable primitives

AssemblyScript doesn’t support using nullable primitive types, so instead, json-as offers the JSON.Box class to remedy it.

For example, this schema won’t compile in AssemblyScript:

  1. @json
  2. class Person {
  3. name!: string;
  4. age: i32 | null = null;
  5. }

Instead, use JSON.Box to allow nullable primitives:

  1. @json
  2. class Person {
  3. name: string;
  4. age: JSON.Box<i32> | null = null;
  5. constructor(name: string) {
  6. this.name = name;
  7. }
  8. }
  9. const person = new Person("Jairus");
  10. console.log(JSON.stringify(person)); // {"name":"Jairus","age":null}
  11. person.age = new JSON.Box<i32>(18); // Set age to 18
  12. console.log(JSON.stringify(person)); // {"name":"Jairus","age":18}

📤 Working with unknown or dynamic data

Sometimes it’s necessary to work with unknown data or data with dynamic types.

Because AssemblyScript is a statically-typed language, that typically isn’t allowed, so json-as provides the JSON.Value and JSON.Obj types.

Here’s a few examples:

Working with multi-type arrays

When dealing with arrays that have multiple types within them, eg. ["string",true,["array"]], use JSON.Value[]

  1. const a = JSON.parse<JSON.Value[]>('["string",true,["array"]]');
  2. console.log(JSON.stringify(a[0])); // "string"
  3. console.log(JSON.stringify(a[1])); // true
  4. console.log(JSON.stringify(a[2])); // ["array"]

Working with unknown objects

When dealing with an object with an unknown structure, use the JSON.Obj type

  1. const obj = JSON.parse<JSON.Obj>('{"a":3.14,"b":true,"c":[1,2,3],"d":{"x":1,"y":2,"z":3}}');
  2. console.log("Keys: " + obj.keys().join(" ")); // a b c d
  3. console.log("Values: " +
  4. obj
  5. .values()
  6. .map<string>((v) => JSON.stringify(v))
  7. .join(" "),
  8. ); // 3.14 true [1,2,3] {"x":1,"y":2,"z":3}
  9. const y = obj.get("d")!.get<JSON.Obj>().get("y")!;
  10. console.log('o1["d"]["y"] = ' + y.toString()); // o1["d"]["y"] = 2

Working with dynamic types within a schema

More often, objects will be completely statically typed except for one or two values.

In such cases, JSON.Value can be used to handle fields that may hold different types at runtime.

  1. @json
  2. class DynamicObj {
  3. id: i32 = 0;
  4. name: string = "";
  5. data!: JSON.Value; // Can hold any type of value
  6. }
  7. const obj = new DynamicObj();
  8. obj.id = 1;
  9. obj.name = "Example";
  10. obj.data = JSON.parse<JSON.Value>('{"key":"value"}'); // Assigning an object
  11. console.log(JSON.stringify(obj)); // {"id":1,"name":"Example","data":{"key":"value"}}
  12. obj.data = JSON.Value.from<i32>(42); // Changing to an integer
  13. console.log(JSON.stringify(obj)); // {"id":1,"name":"Example","data":42}
  14. obj.data = JSON.Value.from("a string"); // Changing to a string
  15. console.log(JSON.stringify(obj)); // {"id":1,"name":"Example","data":"a string"}

🏗️ Using Raw JSON strings

Sometimes its necessary to simply copy a string instead of serializing it.

For example, the following data would typically be serialized as:

  1. const map = new Map<string, string>();
  2. map.set("pos", '{"x":1.0,"y":2.0,"z":3.0}');
  3. console.log(JSON.stringify(map));
  4. // {"pos":"{\"x\":1.0,\"y\":2.0,\"z\":3.0}"}
  5. // pos's value (Vec3) is contained within a string... ideally, it should be left alone

If, instead, one wanted to insert Raw JSON into an existing schema/data structure, they could make use of the JSON.Raw type to do so:

  1. const map = new Map<string, JSON.Raw>();
  2. map.set("pos", new JSON.Raw('{"x":1.0,"y":2.0,"z":3.0}'));
  3. console.log(JSON.stringify(map));
  4. // {"pos":{"x":1.0,"y":2.0,"z":3.0}}
  5. // Now its properly formatted JSON where pos's value is of type Vec3 not string!

⚒️ Using custom serializers or deserializers

This library supports custom serialization and deserialization methods, which can be defined using the @serializer and @deserializer decorators.

Here’s an example of creating a custom data type called Point which serializes to (x,y)

  1. import { bytes } from "json-as/assembly/util";
  2. @json
  3. class Point {
  4. x: f64 = 0.0;
  5. y: f64 = 0.0;
  6. constructor(x: f64, y: f64) {
  7. this.x = x;
  8. this.y = y;
  9. }
  10. @serializer
  11. serializer(self: Point): string {
  12. return `(${self.x},${self.y})`;
  13. }
  14. @deserializer
  15. deserializer(data: string): Point {
  16. const dataSize = bytes(data);
  17. if (dataSize <= 2) throw new Error("Could not deserialize provided data as type Point");
  18. const c = data.indexOf(",");
  19. const x = data.slice(1, c);
  20. const y = data.slice(c + 1, data.length - 1);
  21. return new Point(f64.parse(x), f64.parse(y));
  22. }
  23. }
  24. const obj = new Point(3.5, -9.2);
  25. const serialized = JSON.stringify<Point>(obj);
  26. const deserialized = JSON.parse<Point>(serialized);
  27. console.log("Serialized " + serialized);
  28. console.log("Deserialized " + JSON.stringify(deserialized));

The serializer function converts a Point instance into a string format (x,y).

The deserializer function parses the string (x,y) back into a Point instance.

These functions are then wrapped before being consumed by the json-as library:

  1. @inline __SERIALIZE_CUSTOM(ptr: usize): void {
  2. const data = this.serializer(changetype<Point>(ptr));
  3. const dataSize = data.length << 1;
  4. memory.copy(bs.offset, changetype<usize>(data), dataSize);
  5. bs.offset += dataSize;
  6. }
  7. @inline __DESERIALIZE_CUSTOM(data: string): Point {
  8. return this.deserializer(data);
  9. }

This allows custom serialization while maintaining a generic interface for the library to access.

⚡ Performance

The json-as library has been optimized to achieve near-gigabyte-per-second JSON processing speeds through SIMD acceleration and highly efficient transformations. Below are detailed statistics comparing performance metrics such as build time, operations-per-second, and throughput.

🔍 Comparison to JavaScript

These benchmarks compare this library to JavaScript’s native JSON.stringify and JSON.parse functions.

Table 1 - AssemblyScript (LLVM)

Test Case Size Serialization (ops/s) Deserialization (ops/s) Serialization (MB/s) Deserialization (MB/s)
Vector3 Object 38 bytes 35,714,285 ops/s 35,435,552 ops/s 1,357 MB/s 1,348 MB/s
Alphabet String 104 bytes 13,617,021 ops/s 18,390,804 ops/s 1,416 MB/s 1,986 MB/s
Small Object 88 bytes 24,242,424 ops/s 12,307,692 ops/s 2,133 MB/s 1,083 MB/s
Medium Object 494 bytes 4,060,913 ops/s 1,396,160 ops/s 2,006 MB/s 689.7 MB/s
Large Object 3374 bytes 614,754 ops/s 132,802 ops/s 2,074 MB/s 448.0 MB/s

Table 2 - JavaScript (V8)

Test Case Size Serialization (ops/s) Deserialization (ops/s) Serialization (MB/s) Deserialization (MB/s)
Vector3 Object 38 bytes 8,791,209 ops/s 5,369,12 ops/s 357.4 MB/s 204.3 MB/s
Alphabet String 104 bytes 13,793,103 ops/s 14,746,544 ops/s 1,416 MB/s 1,592 MB/s
Small Object 88 bytes 8,376,963 ops/s 4,968,944 ops/s 737.1 MB/s 437.2 MB/s
Medium Object 494 bytes 2,395,210 ops/s 1,381,693 ops/s 1,183 MB/s 682.5 MB/s
Large Object 3374 bytes 222,222 ops/s 117,233 ops/s 749.7 MB/s 395.5 MB/s

📌 Insights

  • JSON-AS consistently outperforms JavaScript’s native implementation.

  • Serialization Speed:

    • JSON-AS achieves speeds up to 2,133 MB/s, significantly faster than JavaScript’s peak of 1,416 MB/s.
    • Large objects see the biggest improvement, with JSON-AS at 2,074 MB/s vs. JavaScript’s 749.7 MB/s.
  • Deserialization Speed:

    • JSON-AS reaches 1,986 MB/s, while JavaScript caps at 1,592 MB/s.
    • Small and medium objects see the most significant performance boost overall.

📈 Comparison to v0.9.x version

Table 1 - v1.0.0

Test Case Size Serialization (ops/s) Deserialization (ops/s) Serialization (MB/s) Deserialization (MB/s)
Vector3 Object 38 bytes 35,714,285 ops/s 35,435,552 ops/s 1,357 MB/s 1,348 MB/s
Alphabet String 104 bytes 13,617,021 ops/s 18,390,804 ops/s 1,416 MB/s 1,986 MB/s
Small Object 88 bytes 24,242,424 ops/s 12,307,692 ops/s 2,133 MB/s 1,083 MB/s
Medium Object 494 bytes 4,060,913 ops/s 1,396,160 ops/s 2,006 MB/s 689.7 MB/s
Large Object 3374 bytes 614,754 ops/s 132,802 ops/s 2,074 MB/s 448.0 MB/s

Table 2 - v0.9.29

Test Case Size Serialization (ops/s) Deserialization (ops/s) Serialization (MB/s) Deserialization (MB/s)
Vector3 Object 38 bytes 6,896,551 ops/s 10,958,904 ops/s 262.1 MB/s 416.4 MB/s
Alphabet String 104 bytes 5,128,205 ops/s 8,695,652 ops/s 533.3 MB/s 939.1 MB/s
Small Object 88 bytes 4,953,560 ops/s 3,678,160 ops/s 435.9 MB/s 323.7 MB/s
Medium Object 494 bytes 522,193 ops/s 508,582 ops/s 258.0 MB/s 251.2 MB/s
Large Object 3374 bytes 51,229 ops/s 65,585 ops/s 172.8 MB/s 221.3 MB/s

📌 Insights:

  • Massive performance improvements in JSON-AS v1.0.0:
  • Serialization is 2-12x faster (e.g., Large Object: 2,074 MB/s vs. 172.8 MB/s).
  • Deserialization is 2-3x faster (e.g., Large Object: 1,348 MB/s vs. 221.3 MB/s).
  • Vector3 Object serialization improved from 416 MB/s to 1,357 MB/s—a 3x benefit through new code generation techniques.

🔭 What’s Next

  • Theorize plans to keep key-order in generated schemas
  • Generate optimized deserialization methods
  • Inline specific hot code paths
  • Implement error handling implementation

📃 License

This project is distributed under an open source license. You can view the full license using the following link: License

📫 Contact

Please send all issues to GitHub Issues and to converse, please send me an email at me@jairus.dev