2019-01-06 18:51:59 -08:00
|
|
|
# assemblyscript-json
|
2019-01-05 20:54:13 -08:00
|
|
|
|
2019-01-06 18:42:42 -08:00
|
|
|
JSON encoder / decoder for AssemblyScript.
|
2019-01-05 20:54:13 -08:00
|
|
|
|
|
|
|
Special thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing infra for AssemblyScript.
|
|
|
|
|
|
|
|
# Limitations
|
|
|
|
|
|
|
|
This is developed for use in smart contracts written in AssemblyScript for https://github.com/nearprotocol/nearcore.
|
|
|
|
This imposes such limitations:
|
2019-01-06 18:42:42 -08:00
|
|
|
- Float numbers not supported
|
2019-01-05 20:54:13 -08:00
|
|
|
- We assume that memory never needs to be deallocated (cause these contracts are short-lived).
|
|
|
|
|
|
|
|
Note that this mostly just defines the way it's currently implemented. Contributors are welcome to fix limitations.
|
|
|
|
|
|
|
|
|
|
|
|
# Usage
|
|
|
|
|
2019-01-06 18:42:42 -08:00
|
|
|
## Encoding JSON
|
2019-01-05 20:54:13 -08:00
|
|
|
|
|
|
|
```ts
|
|
|
|
// Make sure memory allocator is available
|
|
|
|
import "allocator/arena";
|
|
|
|
// Import encoder
|
2019-01-06 18:42:42 -08:00
|
|
|
import { JSONEncoder } from "path/to/module";
|
2019-01-05 20:54:13 -08:00
|
|
|
|
|
|
|
// Create encoder
|
2019-01-06 18:42:42 -08:00
|
|
|
let encoder = new JSONEncoder();
|
2019-01-05 20:54:13 -08:00
|
|
|
|
|
|
|
// Construct necessary object
|
|
|
|
encoder.pushObject("obj");
|
|
|
|
encoder.setInteger("int", 10);
|
|
|
|
encoder.setString("str", "");
|
|
|
|
encoder.popObject();
|
|
|
|
|
|
|
|
// Get serialized data
|
2019-01-11 13:06:14 -08:00
|
|
|
let json: Uint8Array = encoder.serialize();
|
2019-01-05 20:54:13 -08:00
|
|
|
|
2019-01-11 13:30:36 -08:00
|
|
|
// Or get serialized data as string
|
|
|
|
let jsonString: String = encoder.toString();
|
|
|
|
|
2019-01-05 20:54:13 -08:00
|
|
|
```
|
|
|
|
|
2019-01-06 18:42:42 -08:00
|
|
|
## Parsing JSON
|
2019-01-05 20:54:13 -08:00
|
|
|
|
|
|
|
```ts
|
|
|
|
// Make sure memory allocator is available
|
|
|
|
import "allocator/arena";
|
|
|
|
// Import decoder
|
2019-01-06 18:42:42 -08:00
|
|
|
import { JSONDecoder, JSONHandler } from "path/to/module";
|
2019-01-05 20:54:13 -08:00
|
|
|
|
2019-01-06 18:42:42 -08:00
|
|
|
// Events need to be received by custom object extending JSONHandler.
|
2019-01-05 20:54:13 -08:00
|
|
|
// NOTE: All methods are optional to implement.
|
2019-01-06 18:42:42 -08:00
|
|
|
class MyJSONEventsHandler extends JSONHandler {
|
2019-01-05 20:54:13 -08:00
|
|
|
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: i32): void {
|
|
|
|
// Handle field
|
|
|
|
}
|
|
|
|
|
|
|
|
pushArray(name: string): bool {
|
|
|
|
// Handle array start
|
2019-01-06 18:51:59 -08:00
|
|
|
// 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;
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
popArray(): void {
|
|
|
|
// Handle array end
|
|
|
|
}
|
|
|
|
|
|
|
|
pushObject(name: string): bool {
|
|
|
|
// Handle object start
|
2019-01-06 18:51:59 -08:00
|
|
|
// 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;
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
popObject(): void {
|
|
|
|
// Handle object end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create decoder
|
2019-01-06 18:42:42 -08:00
|
|
|
let decoder = new JSONDecoder<MyJSONEventsHandler>(new MyJSONEventsHandler());
|
2019-01-05 20:54:13 -08:00
|
|
|
|
2019-01-06 18:42:42 -08:00
|
|
|
// Let's assume JSON data is available in this variable
|
|
|
|
let json: Uint8Array = ...;
|
2019-01-05 20:54:13 -08:00
|
|
|
|
2019-01-06 18:42:42 -08:00
|
|
|
// Parse JSON
|
|
|
|
decoder.deserialize(json); // This will send events to MyJSONEventsHandler
|
2019-01-05 20:54:13 -08:00
|
|
|
|
|
|
|
```
|
|
|
|
|