JSON in AssemblyScript

Finally, AssemblyScript has a fully-functional JSON implementation. as-json implements full JSON compatibility that enables AssemblyScript to use JSON to communicate with APIs, store data, and more. In this article, I will introduce you to as-json and …


This content originally appeared on DEV Community and was authored by Jairus Tanaka

Finally, AssemblyScript has a fully-functional JSON implementation. as-json implements full JSON compatibility that enables AssemblyScript to use JSON to communicate with APIs, store data, and more. In this article, I will introduce you to as-json and help you include it into your project.

Getting Started

as-json uses the same general API as the JavaScript JSON object. We can then use it just like JS. However, we must install and prep it first.

npm install json-as
--transform json-as/transform

Installation complete. Lets use some JSON! ?

json-test.ts

import { JSON } from 'json-as'

// Create the Schemas
// @ts-ignore
@json
class JSONSchema {
  firstName: string;
  lastName: string;
  human: boolean;
  age: i32;
  meta: Meta;
  language: string;
  location: f64[];
}

// @ts-ignore
@json
class Meta {
  country: string;
  awesome: boolean;
}

// Create the JSON object
const data: JSONSchema = {
  firstName: 'Jairus',
  lastName: 'Tanaka',
  age: 14,
  human: true,
  meta: {
    country: 'US',
    awesome: true
  },
  language: 'english',
  location: [-43.130850291, 32.926401705]
};

// Now, encode and decode
const encoded: string = JSON.stringify(data)
console.log(`Encoded: ${encoded}`)
const decoded = JSON.parse<JSONSchema>(encoded)
console.log(`Decoded:`)
console.log(`{`);
console.log(` firstName: ${decoded.firstName},`);
console.log(` lastName: ${decoded.lastName},`);
console.log(` age: ${decoded.age},`);
console.log(` human: ${decoded.human},`);
console.log(` meta: {`);
console.log(`   country: ${decoded.meta.country},`);
console.log(`   awesome: ${decoded.meta.awesome}`);
console.log(` },`);
console.log(` language: ${decoded.language}`);
console.log(` location: [${decoded.location[0]}, ${decoded.location[1]}]`);
console.log(`}`);

Note: If you are not running in WASI, use as-console instead.

So, json-as serialized and deserialized JSON. Lets see if it was correct.

json-test.ts

import { JSON } from 'json-as'

// Create the Schemas
// @ts-ignore
@json
class JSONSchema {
  firstName: string;
  lastName: string;
  human: boolean;
  age: i32;
  meta: Meta;
  language: string;
  location: f64[];
}

// @ts-ignore
@json
class Meta {
  country: string;
  awesome: boolean;
}

// Create the JSON object
const data: JSONSchema = {
  firstName: 'Jairus',
  lastName: 'Tanaka',
  age: 14,
  human: true,
  meta: {
    country: 'US',
    awesome: true
  },
  language: 'english',
  location: [-43.130850291, 32.926401705]
};

// Now, encode and decode
const encoded: string = JSON.stringify(data)
const decoded = JSON.parse<JSONSchema>(encoded)
// We perform an equality check
if (encoded == JSON.stringify(decoded)) {
  console.log('Yay! JSON-AS works! ?')
} else {
  console.log('Oof. JSON-AS died.?')
}

Yay! JSON is now working for AssemblyScript. Go ahead and mess around with it and let me know what you make.
NPM: https://www.npmjs.com/package/json-as
GitHub: https://github.com/aspkg/as-json
Thanks for reading ?


This content originally appeared on DEV Community and was authored by Jairus Tanaka


Print Share Comment Cite Upload Translate Updates
APA

Jairus Tanaka | Sciencx (2021-07-20T14:44:53+00:00) JSON in AssemblyScript. Retrieved from https://www.scien.cx/2021/07/20/json-in-assemblyscript/

MLA
" » JSON in AssemblyScript." Jairus Tanaka | Sciencx - Tuesday July 20, 2021, https://www.scien.cx/2021/07/20/json-in-assemblyscript/
HARVARD
Jairus Tanaka | Sciencx Tuesday July 20, 2021 » JSON in AssemblyScript., viewed ,<https://www.scien.cx/2021/07/20/json-in-assemblyscript/>
VANCOUVER
Jairus Tanaka | Sciencx - » JSON in AssemblyScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/20/json-in-assemblyscript/
CHICAGO
" » JSON in AssemblyScript." Jairus Tanaka | Sciencx - Accessed . https://www.scien.cx/2021/07/20/json-in-assemblyscript/
IEEE
" » JSON in AssemblyScript." Jairus Tanaka | Sciencx [Online]. Available: https://www.scien.cx/2021/07/20/json-in-assemblyscript/. [Accessed: ]
rf:citation
» JSON in AssemblyScript | Jairus Tanaka | Sciencx | https://www.scien.cx/2021/07/20/json-in-assemblyscript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.