JSON encoder / decoder for AssemblyScript.
Special thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing infra for AssemblyScript.
assemblyscript-json is available as a npm package. You can install assemblyscript-json in your AssemblyScript project by running:
npm install --save assemblyscript-json
import{JSON}from"@devcycle/assemblyscript-json";// Parse an object using the JSON objectletjsonObj: 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 existletworldOrNull: JSON.Str|null=jsonObj.getString("hello");// This will return a JSON.Str or nullif(worldOrNull!=null){// use .valueOf() to turn the high level JSON.Str type into a stringletworld: string=worldOrNull.valueOf();}letnumOrNull: JSON.Num|null=jsonObj.getNum("value");if(numOrNull!=null){// use .valueOf() to turn the high level JSON.Num type into a f64letvalue: f64=numOrNull.valueOf();}// If you don't know the value type, get the parent JSON.ValueletvalueOrNull: JSON.Value|null=jsonObj.getValue("hello");if(valueOrNull!=null){letvalue=<JSON.Value>valueOrNull;// Next we could figure out what type we areif(value.isString){// value.isString would be true, so we can cast to a stringletinnerString=(<JSON.Str>value).valueOf();letjsonString=(<JSON.Str>value).stringify();// Do something with string value}}import{JSONEncoder}from"@devcycle/assemblyscript-json";// Create encoderletencoder=newJSONEncoder();// Construct necessary objectencoder.pushObject("obj");encoder.setInteger("int",10);encoder.setString("str","");encoder.popObject();// Get serialized dataletjson: Uint8Array=encoder.serialize();// Or get serialized data as stringletjsonString: string=encoder.stringify();assert(jsonString,'"obj": {"int": 10, "str": ""}');// True!import{JSONDecoder,JSONHandler}from"@devcycle/assemblyscript-json";// Events need to be received by custom object extending JSONHandler.// NOTE: All methods are optional to implement.classMyJSONEventsHandlerextendsJSONHandler{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 handlerreturntrue;}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 handlerreturntrue;}popObject(): void{// Handle object end}}// Create decoderletdecoder=newJSONDecoder<MyJSONEventsHandler>(newMyJSONEventsHandler());// Create a byte buffer of our JSON. NOTE: Deserializers work on UTF8 string buffers.letjsonString='{"hello": "world"}';letjsonBuffer=Uint8Array.wrap(String.UTF8.encode(jsonString));// Parse JSONdecoder.deserialize(jsonBuffer);// This will send events to MyJSONEventsHandlerFeel free to look through the tests for more usage examples.
Reference API Documentation can be found in the docs directory.