All Projects → near → assemblyscript-json

near / assemblyscript-json

Licence: MIT license
JSON encoder / decoder for AssemblyScript

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to assemblyscript-json

as-sha256
AssemblyScript implementation of SHA256
Stars: ✭ 15 (-89.29%)
Mutual labels:  assemblyscript
examples
A collection of AssemblyScript examples.
Stars: ✭ 233 (+66.43%)
Mutual labels:  assemblyscript
website
AssemblyScript's website and documentation.
Stars: ✭ 47 (-66.43%)
Mutual labels:  assemblyscript
Babylon.Font
Generate text mesh for BabylonJS using WASM, written in AssemblyScript.
Stars: ✭ 22 (-84.29%)
Mutual labels:  assemblyscript
Assemblyscript
A TypeScript-like language for WebAssembly.
Stars: ✭ 13,152 (+9294.29%)
Mutual labels:  assemblyscript
easegress-assemblyscript-sdk
AssemblyScript SDK for Easegress
Stars: ✭ 15 (-89.29%)
Mutual labels:  assemblyscript
gomoku-wasm
A Gomoku game implements with WebAssembly
Stars: ✭ 30 (-78.57%)
Mutual labels:  assemblyscript
ask
Ask! is a framework to write Wasm smart contracts for Substrate Frame Pallet-Contracts in AssemblyScript
Stars: ✭ 44 (-68.57%)
Mutual labels:  assemblyscript
blurhash-as
Blurhash implementation in AssemblyScript
Stars: ✭ 26 (-81.43%)
Mutual labels:  assemblyscript
atari2600-wasm
An Atari 2600 emulator written in AssemblyScript compiled to WebAssembly
Stars: ✭ 50 (-64.29%)
Mutual labels:  assemblyscript
counter
Increment and decrement a counter with this simple smart contract via a web page.
Stars: ✭ 21 (-85%)
Mutual labels:  assemblyscript
Lunatic
Lunatic is an Erlang-inspired runtime for WebAssembly
Stars: ✭ 2,074 (+1381.43%)
Mutual labels:  assemblyscript
pallet-contracts-waterfall
Collection of simple Substrate smart contract examples written in Rust, AssemblyScript, Solang and the smart contract language ink! to test substrates pallet-contracts module
Stars: ✭ 28 (-80%)
Mutual labels:  assemblyscript
redstone-smartcontracts
An implementation of the Arweave SmartWeave smart contracts protocol.
Stars: ✭ 42 (-70%)
Mutual labels:  assemblyscript
as-string-sink
An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript
Stars: ✭ 23 (-83.57%)
Mutual labels:  assemblyscript
guest-book
Sign in with NEAR and add a message to the guest book!
Stars: ✭ 68 (-51.43%)
Mutual labels:  assemblyscript
assemblyscript-regex
A regex engine for AssemblyScript
Stars: ✭ 81 (-42.14%)
Mutual labels:  assemblyscript
rabin-wasm
Rabin fingerprinting implemented in WASM
Stars: ✭ 26 (-81.43%)
Mutual labels:  assemblyscript
as-bignum
Fixed length big numbers for AssemblyScript 🚀
Stars: ✭ 49 (-65%)
Mutual labels:  assemblyscript
wasm4
Build retro games using WebAssembly for a fantasy console.
Stars: ✭ 711 (+407.86%)
Mutual labels:  assemblyscript

assemblyscript-json

npm version npm downloads per month

JSON encoder / decoder for AssemblyScript.

Special thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing infra for AssemblyScript.

Installation

assemblyscript-json is available as a npm package. You can install assemblyscript-json in your AssemblyScript project by running:

npm install --save assemblyscript-json

Usage

Parsing JSON

import { JSON } from "assemblyscript-json"; 

// Parse an object using the JSON object
let jsonObj: JSON.Obj = <JSON.Obj>(JSON.parse('{"hello": "world", "value": 24}'));

// We can then use the .getX functions to read from the object if you know it's type
// This will return the appropriate JSON.X value if the key exists, or null if the key does not exist
let worldOrNull: JSON.Str | null = jsonObj.getString("hello"); // This will return a JSON.Str or null
if (worldOrNull != null) {
  // use .valueOf() to turn the high level JSON.Str type into a string
  let world: string = worldOrNull.valueOf();
}

let numOrNull: JSON.Num | null = jsonObj.getNum("value");
if (numOrNull != null) {
  // use .valueOf() to turn the high level JSON.Num type into a f64
  let value: f64 = numOrNull.valueOf();
}

// If you don't know the value type, get the parent JSON.Value
let valueOrNull: JSON.Value | null = jsonObj.getValue("hello");
  if (valueOrNull != null) {
  let value = <JSON.Value>valueOrNull;

  // Next we could figure out what type we are
  if(value.isString) { 
    // value.isString would be true, so we can cast to a string
    let innerString = (<JSON.Str>value).valueOf();
    let jsonString = (<JSON.Str>value).stringify();

    // Do something with string value
  }
}

Encoding JSON

import { JSONEncoder } from "assemblyscript-json";

// Create encoder
let encoder = new JSONEncoder();

// Construct necessary object
encoder.pushObject("obj");
encoder.setInteger("int", 10);
encoder.setString("str", "");
encoder.popObject();

// Get serialized data
let json: Uint8Array = encoder.serialize();

// Or get serialized data as string
let jsonString: string = encoder.stringify();

assert(jsonString, '"obj": {"int": 10, "str": ""}'); // True!

Custom JSON Deserializers

import { JSONDecoder, JSONHandler } from "assemblyscript-json";

// Events need to be received by custom object extending JSONHandler.
// NOTE: All methods are optional to implement.
class MyJSONEventsHandler extends JSONHandler {
  setString(name: string, value: string): void {
    // Handle field
  }

  setBoolean(name: string, value: bool): void {
    // Handle field
  }

  setNull(name: string): void {
    // Handle field
  }

  setInteger(name: string, value: i64): void {
    // Handle field
  }

  setFloat(name: string, value: f64): void {
    // Handle field
  }

  pushArray(name: string): bool {
    // Handle array start
    // true means that nested object needs to be traversed, false otherwise
    // Note that returning false means JSONDecoder.startIndex need to be updated by handler
    return true;
  }

  popArray(): void {
    // Handle array end
  }

  pushObject(name: string): bool {
    // Handle object start
    // true means that nested object needs to be traversed, false otherwise
    // Note that returning false means JSONDecoder.startIndex need to be updated by handler
    return true;
  }

  popObject(): void {
    // Handle object end
  }
}

// Create decoder
let decoder = new JSONDecoder<MyJSONEventsHandler>(new MyJSONEventsHandler());

// Create a byte buffer of our JSON. NOTE: Deserializers work on UTF8 string buffers.
let jsonString = '{"hello": "world"}';
let jsonBuffer = Uint8Array.wrap(String.UTF8.encode(jsonString));

// Parse JSON
decoder.deserialize(jsonBuffer); // This will send events to MyJSONEventsHandler

Feel free to look through the tests for more usage examples.

Reference Documentation

Reference API Documentation can be found in the docs directory.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].