You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
API Fixes, README cleanup, and generated Reference Documentation (#121)
* Got most of the JSON API cleaned up
* Added support for generating docs
* Finished deploying docs
* Added the docs to the README
* Removed the quotes on Str.toString()
* One last comment on getX functions
* Added a test for toString all the values

4
+
3
5
JSON encoder / decoder for AssemblyScript.
4
6
5
7
Special thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing infra for AssemblyScript.
6
8
7
-
# Limitations
9
+
## Installation
10
+
11
+
`assemblyscript-json` is available as a [npm package](https://www.npmjs.com/package/assemblyscript-json). You can install `assemblyscript-json` in your AssemblyScript project by running:
8
12
9
-
This is developed for use in smart contracts written in AssemblyScript for https://github.com/nearprotocol/nearcore.
10
-
This imposes such limitations:
13
+
`npm install --save assemblyscript-json`
11
14
12
-
- Float numbers not supported
13
-
- We assume that memory never needs to be deallocated (cause these contracts are short-lived).
15
+
## Usage
14
16
15
-
Note that this mostly just defines the way it's currently implemented. Contributors are welcome to fix limitations.
17
+
### Parsing JSON
16
18
17
-
# Usage
19
+
```typescript
20
+
import { JSON } from"assemblyscript-json";
18
21
19
-
## Encoding JSON
22
+
// Parse an object using the JSON object
23
+
let jsonObj:JSON.Obj= <JSON.Obj>(JSON.parse('{"hello": "world", "value": 24}'));
20
24
21
-
```ts
22
-
// Make sure memory allocator is available
23
-
import"allocator/arena";
24
-
// Import encoder
25
-
import { JSONEncoder } from"path/to/module";
25
+
// We can then use the .getX functions to read from the object if you know it's type
26
+
// This will return the appropriate JSON.X value if the key exists, or null if the key does not exist
27
+
let worldOrNull:JSON.Str|null=jsonObj.getString("hello"); // This will return a JSON.Str or null
28
+
if (worldOrNull!=null) {
29
+
// use .valueOf() to turn the high level JSON.Str type into a string
30
+
let world:string=worldOrNull.valueOf();
31
+
}
32
+
33
+
let numOrNull:JSON.Num|null=jsonObj.getNum("value");
34
+
if (numOrNull!=null) {
35
+
// use .valueOf() to turn the high level JSON.Num type into a f64
36
+
let value:f64=numOrNull.valueOf();
37
+
}
38
+
39
+
// If you don't know the value type, get the parent JSON.Value
40
+
let valueOrNull:JSON.Value|null=jsonObj.getValue("hello");
41
+
if (valueOrNull!=null) {
42
+
let value:JSON.Value=changetype<JSON.Value>(valueOrNull);
43
+
44
+
// Next we could figure out what type we are
45
+
if(value.isString) {
46
+
// value.isString would be true, so we can cast to a string
47
+
let stringValue:string=changetype<JSON.Str>(value).toString();
0 commit comments