mirror of
https://github.com/fluencelabs/assemblyscript-json
synced 2025-04-24 22:32:20 +00:00
Update README and license
This commit is contained in:
parent
41571e1c98
commit
41ae669077
1
LICENSE
1
LICENSE
@ -1,6 +1,5 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2016 Marco Paland
|
|
||||||
Copyright (c) 2018 NEAR Protocol
|
Copyright (c) 2018 NEAR Protocol
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
39
README.md
39
README.md
@ -1,6 +1,6 @@
|
|||||||
# assemblyscript-bson
|
# assemblyscript-bson
|
||||||
|
|
||||||
BSON encoder / decoder for AssemblyScript somewhat based on https://github.com/mpaland/bsonfy.
|
JSON encoder / decoder for AssemblyScript.
|
||||||
|
|
||||||
Special thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing infra for AssemblyScript.
|
Special thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing infra for AssemblyScript.
|
||||||
|
|
||||||
@ -8,14 +8,7 @@ Special thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing
|
|||||||
|
|
||||||
This is developed for use in smart contracts written in AssemblyScript for https://github.com/nearprotocol/nearcore.
|
This is developed for use in smart contracts written in AssemblyScript for https://github.com/nearprotocol/nearcore.
|
||||||
This imposes such limitations:
|
This imposes such limitations:
|
||||||
- Only limited data types are supported:
|
- Float numbers not supported
|
||||||
- arrays
|
|
||||||
- objects
|
|
||||||
- 32-bit integers
|
|
||||||
- strings
|
|
||||||
- booleans
|
|
||||||
- null
|
|
||||||
- `Uint8Array`
|
|
||||||
- We assume that memory never needs to be deallocated (cause these contracts are short-lived).
|
- 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.
|
Note that this mostly just defines the way it's currently implemented. Contributors are welcome to fix limitations.
|
||||||
@ -23,16 +16,16 @@ Note that this mostly just defines the way it's currently implemented. Contribut
|
|||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
## Encoding BSON
|
## Encoding JSON
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
// Make sure memory allocator is available
|
// Make sure memory allocator is available
|
||||||
import "allocator/arena";
|
import "allocator/arena";
|
||||||
// Import encoder
|
// Import encoder
|
||||||
import { BSONEncoder } from "path/to/module";
|
import { JSONEncoder } from "path/to/module";
|
||||||
|
|
||||||
// Create encoder
|
// Create encoder
|
||||||
let encoder = new BSONEncoder();
|
let encoder = new JSONEncoder();
|
||||||
|
|
||||||
// Construct necessary object
|
// Construct necessary object
|
||||||
encoder.pushObject("obj");
|
encoder.pushObject("obj");
|
||||||
@ -45,17 +38,17 @@ let bson: Uint8Array = encoder.serialize();
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Parsing BSON
|
## Parsing JSON
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
// Make sure memory allocator is available
|
// Make sure memory allocator is available
|
||||||
import "allocator/arena";
|
import "allocator/arena";
|
||||||
// Import decoder
|
// Import decoder
|
||||||
import { BSONDecoder, BSONHandler } from "path/to/module";
|
import { JSONDecoder, JSONHandler } from "path/to/module";
|
||||||
|
|
||||||
// Events need to be received by custom object extending BSONHandler.
|
// Events need to be received by custom object extending JSONHandler.
|
||||||
// NOTE: All methods are optional to implement.
|
// NOTE: All methods are optional to implement.
|
||||||
class MyBSONEventsHandler extends BSONHandler {
|
class MyJSONEventsHandler extends JSONHandler {
|
||||||
setString(name: string, value: string): void {
|
setString(name: string, value: string): void {
|
||||||
// Handle field
|
// Handle field
|
||||||
}
|
}
|
||||||
@ -72,10 +65,6 @@ class MyBSONEventsHandler extends BSONHandler {
|
|||||||
// Handle field
|
// Handle field
|
||||||
}
|
}
|
||||||
|
|
||||||
setUint8Array(name: string, value: Uint8Array): void {
|
|
||||||
// Handle field
|
|
||||||
}
|
|
||||||
|
|
||||||
pushArray(name: string): bool {
|
pushArray(name: string): bool {
|
||||||
// Handle array start
|
// Handle array start
|
||||||
return true; // true means that nested object needs to be traversed, false otherwise
|
return true; // true means that nested object needs to be traversed, false otherwise
|
||||||
@ -96,13 +85,13 @@ class MyBSONEventsHandler extends BSONHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create decoder
|
// Create decoder
|
||||||
let decoder = new BSONDecoder<MyBSONEventsHandler>(new MyBSONEventsHandler());
|
let decoder = new JSONDecoder<MyJSONEventsHandler>(new MyJSONEventsHandler());
|
||||||
|
|
||||||
// Let's assume BSON data is available in this variable
|
// Let's assume JSON data is available in this variable
|
||||||
let bson: Uint8Array = ...;
|
let json: Uint8Array = ...;
|
||||||
|
|
||||||
// Parse BSON
|
// Parse JSON
|
||||||
decoder.deserialize(bson); // This will send events to MyBSONEventsHandler
|
decoder.deserialize(json); // This will send events to MyJSONEventsHandler
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user