Git Product home page Git Product logo

as-json's Introduction

AS-JSON

JSON encoder/decoder for AssemblyScript

Installation

Install the library:

npm install json-as

Install dependency for a transformer:

npm install --save-dev visitor-as

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

--transform json-as/transform

Support

  • ✅ Objects
  • ✅ Arrays
  • ✅ Numbers
  • ✅ Integers
  • ✅ Null
  • ✅ Dynamic Arrays
  • ✅ Dynamic Types
  • ✅ Dynamic Objects
  • ✅ Whitespace

Usage

import { JSON } from 'json-as'

Object

@json
class JSONSchema {
    firstName: string
    lastName: string
    age: i32
}

const data: JSONSchema {
    firstName: 'Jairus',
    lastName: 'Tanaka',
    age: 14
}

const stringified = JSON.stringify(data)
// '{"firstName":"Jairus","lastName":"Tanaka","age":14}'

const parsed = JSON.parse<JSONSchema>(stringified)
// { firstName: "Jairus", lastName: "Tanaka", age: 14 }

Array

const stringified = JSON.stringify(['Hello', 'World'])
// '["Hello","World"]'

const parsed = JSON.parse<JSONSchema>(stringified)
// ["Hello", "World"]

Float

const stringified = JSON.stringify(3.14)
// '3.14'

const parsed = JSON.parse<f64>(stringified)
// 3.14

Integer

const stringified = JSON.stringify(14)
// '14'

const parsed = JSON.parse<i32>(stringified)
// 14

Boolean

const stringified = JSON.stringify(true)
// 'true'

const parsed = JSON.parse<boolean>(stringified)
// true

Bool

const stringified = JSON.stringify(true)
// 'true'

const parsed = JSON.parse<bool>(stringified)
// true

Null

const stringified = JSON.stringify(null)
// 'null'

const parsed = JSON.parse(stringified)
// null

Benchmarks

AS-JSON Stringify String: ~4191267.51 ops/s | 23.86ms
AS-JSON Parse String: ~6218119.99 ops/s | 16.08ms
AS-JSON Stringify Integer: ~13775012.61 ops/s | 7.26ms
AS-JSON Parse Integer: ~55061164.13 ops/s | 1.82ms
AS-JSON Stringify Float: ~7739399.89 ops/s | 12.92ms
AS-JSON Parse Float: ~37522902.16 ops/s | 2.67ms
AS-JSON Stringify Boolean: ~615015015.02 ops/s | 0.16ms
AS-JSON Parse Boolean: ~93901879.87 ops/s | 1.06ms
AS-JSON Stringify Array: ~2380329.74 ops/s | 42.01ms
AS-JSON Parse Array: ~6258786.14 ops/s | 15.98ms
AS-JSON Stringify Object: ~5245632.91 ops/s | 19.06ms
AS-JSON Parse Object: ~1328576.06 ops/s | 75.27ms

as-json's People

Contributors

jairussw avatar romdotdog avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.