πŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more β†’
Socket
Book a DemoInstallSign in
Socket

json-as

Package Overview
Dependencies
Maintainers
1
Versions
192
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.4.6
Source
npm
Version published
Weekly downloads
2.4K
23.68%
Maintainers
1
Weekly downloads
Β 
Created
Source

AS-JSON

JSON serializer/deserializer for AssemblyScript

Installation

~ npm install json-as
~ npm install visitor-as

Add the transform to your asc command

--transform json-as/transform

Or, add it to asconfig.json

{
  "options": {
    "transform": "json-as/transform"
  }
}

Support

  • βœ… Objects (Supported)
  • βœ… Arrays (Supported)
  • βœ… Numbers (Supported)
  • βœ… Integers (Supported)
  • βœ… Null (Supported)
  • ❌ Dynamic Variants (Not supported)

Usage

import { JSON } from "json-as";

@json
class Vec2 {
  x: f32
  y: f32
}
@json
class Player {
  firstName: string
  lastName: string
  lastActive: i32[]
  age: i32
  pos: Vec2
  isVerified: boolean
}

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

const stringified = JSON.stringify<Player>(data);
// {
//  "firstName": "Emmet",
//  "lastName": "West",
//  "lastActive": [8, 27, 2022],
//  "age": 23,
//  "pos": {
//    "x": -3.4000000953674318,
//    "y": 1.2000000476837159
//  },
//  "isVerified": true
// }
console.log(`Stringified: ${stringified}`);

const parsed = JSON.parse<Player>(stringified);
// Player {
//  firstName: "Emmet",
//  lastName: "West",
//  lastActive: [8, 27, 2022],
//  age: 23,
//  pos: {
//    x: -3.4000000953674318,
//    y: 1.2000000476837159
//  },
//  isVerified: true
// }
console.log(`Parsed: ${JSON.stringify(parsed)}`);

FAQ

  • Does it support the full JSON spec? Yes, as-json supports the full JSON specification and trys to mimic the JSON API as much as possible
  • What are the differences between as-json and the JSON API? The main difference between as-json and the JSON API is the ability to create new fields in objects during runtime. Since classes are static, Map ser/de support will be added soon allowing the user to parse and stringify dynamic objects and their properties
  • Does this support nested structures? Yes, as-json supports nested structures
  • Does this support whitespace? Yes, as-json supports whitespace!
  • How fast is it? Really fast. For example, here are some benchmarks for ser/de a Vec2 with as-json

Issues

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

Keywords

assemblyscript

FAQs

Package last updated on 12 Sep 2022

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