New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

json-as

Package Overview
Dependencies
Maintainers
1
Versions
137
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-as

JSON encoder/decoder for AssemblyScript

  • 0.8.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.7K
increased by84.26%
Maintainers
1
Weekly downloads
 
Created
Source

 █████╗ ███████╗           ██╗███████╗ ██████╗ ███╗   ██╗
██╔══██╗██╔════╝           ██║██╔════╝██╔═══██╗████╗  ██║
███████║███████╗█████╗     ██║███████╗██║   ██║██╔██╗ ██║
██╔══██║╚════██║╚════╝██   ██║╚════██║██║   ██║██║╚██╗██║
██║  ██║███████║      ╚█████╔╝███████║╚██████╔╝██║ ╚████║
╚═╝  ╚═╝╚══════╝       ╚════╝ ╚══════╝ ╚═════╝ ╚═╝  ╚═══╝

v0.8.6

Installation

npm install json-as

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

--transform json-as/transform

Alternatively, add it to your asconfig.json

{
  // ...
  "options": {
    "transform": ["json-as/transform"]
  }
}

Usage

import { JSON } from "json-as/assembly";

// @json or @serializable work here
@json
class Vec3 {
  x: f32 = 0.0;
  y: f32 = 0.0;
  z: f32 = 0.0;
}

@json
class Player {
  @alias("first name")
  firstName!: string;
  lastName!: string;
  lastActive!: i32[];
  age!: i32;
  pos!: Vec3 | null;
  isVerified!: boolean;
}

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

const stringified = JSON.stringify<Player>(player, true);
// You can toggle on setting default values with the 2nd parameter
// Alternative: use JSON.serializeTo(player, out);

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

If you use this project in your codebase, consider dropping a star. I would really appreciate it!

Performance

Run or view the benchmarks here

Below are benchmark results comparing JavaScript's built-in JSON implementation and JSON-AS

My library beats JSON (written in C++) on all counts and, I see many places where I can pull at least a 60% uplift in performance if I implement it.

Serialization Benchmarks:

ValueJavaScript (ops/s)JSON-as (ops/s)JSON-AS with Pages
"hello world"28,629,59864,210,666+ 124%
1234531,562,43156,329,066321,783,941 ops/s
1.234515,977,27820,322,93930,307,616 ops/s
[[],[[]],[[],[[]]]]8,998,62434,453,102+ 283%

Deserialization Benchmarks: (WIP)

ValueJavaScript (ops/s)JSON-AS (ops/s)% Diff
"12345"34,647,886254,640,930+ 635%

And my PC specs:

ComponentSpecification
Wasmer Versionv4.3.0
CPUAMD Ryzen 7 7800x3D @ 6.00 GHz
MemoryT-Force DDR5 6000 MHz
OSUbuntu WSL2
GraphicsAMD Radeon RX 6750XT

Issues

Please submit an issue to https://github.com/JairusSW/as-json/issues if you find anything wrong with this library

Keywords

FAQs

Package last updated on 17 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc