🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →

json-as

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-as - npm Package Compare versions

Comparing version

to
0.5.5

@@ -14,4 +14,6 @@ {

"transform": ["./transform"],
"bindings": "esm"
}
"bindings": "esm",
"extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
},
"extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
}
import { JSON } from "..";
import { u128, u128Safe, u256, u256Safe, i128, i128Safe, i256Safe } from "as-bignum/assembly";
function canSerde<T>(data: T): void {

@@ -42,2 +43,30 @@ const serialized = JSON.stringify<T>(data);

canSerde<i64>(-101);
canSerde<u128>(u128.from("0"))
canSerde<u128>(u128.from("100"))
canSerde<u128>(u128.from("101"))
canSerde<u128Safe>(u128Safe.from("0"))
canSerde<u128Safe>(u128Safe.from("100"))
canSerde<u128Safe>(u128Safe.from("101"))
canSerde<u256>(u256.fromU128(u128.from("0")))
canSerde<u256>(u256.fromU128(u128.from("100")))
canSerde<u256>(u256.fromU128(u128.from("101")))
canSerde<u256Safe>(u256Safe.fromU128(u128.from("0")))
canSerde<u256Safe>(u256Safe.fromU128(u128.from("100")))
canSerde<u256Safe>(u256Safe.fromU128(u128.from("101")))
canSerde<i128>(i128.from("0"))
canSerde<i128>(i128.from("100"))
canSerde<i128>(i128.from("101"))
canSerde<i128Safe>(i128Safe.from("0"))
canSerde<i128Safe>(i128Safe.from("100"))
canSerde<i128Safe>(i128Safe.from("101"))
canSerde<i128Safe>(i128Safe.from("-100"))
canSerde<i128Safe>(i128Safe.from("-101"))
canSerde<i256Safe>(new i256Safe(10, 11, 500, 501))
});

@@ -75,2 +104,17 @@

});
it("should ser/de BigInt objects", () => {
canSerde<i32>(0);
canSerde<u32>(100);
canSerde<u64>(101);
canSerde<i32>(-100);
canSerde<i64>(-101);
canSerde<u128>(u128.from("0"))
canSerde<u128>(u128.from("100"))
canSerde<u128>(u128.from("101"))
canSerde<u128>(u128.from("-100"))
canSerde<u128>(u128.from("-101"))
})
});

@@ -77,0 +121,0 @@

@@ -0,1 +1,2 @@

import { u128, u128Safe, u256, u256Safe, i128, i128Safe, i256Safe } from "as-bignum/assembly";
import { StringSink } from "as-string-sink/assembly";

@@ -24,3 +25,3 @@ import { isSpace } from "util/string";

} from "./chars";
import { unsafeCharCodeAt } from "./util";
import { isBigNum, unsafeCharCodeAt } from "./util";

@@ -30,3 +31,3 @@ /**

*/
export class JSON {
export namespace JSON {
/**

@@ -40,3 +41,3 @@ * Stringifies valid JSON data.

*/
static stringify<T>(data: T): string {
export function stringify<T>(data: T): string {
// String

@@ -83,2 +84,5 @@ if (isString<T>()) {

return result.toString();
} else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
// @ts-ignore
return data.toString();
} else {

@@ -96,3 +100,3 @@ throw new Error(`Could not serialize data of type ${nameof<T>()}. Invalid data provided.`);

*/
static parse<T>(data: string): T {
export function parse<T>(data: string): T {
let type!: T;

@@ -117,2 +121,5 @@ if (isString<T>()) {

return parseObject<T>(data.trimStart());
} else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
// @ts-ignore
return parseBigNum<T>(data);
} else {

@@ -123,3 +130,3 @@ // @ts-ignore

}
private static parseObjectValue<T>(data: string): T {
function parseObjectValue<T>(data: string): T {
let type!: T;

@@ -146,2 +153,5 @@ if (isString<T>()) {

return parseObject<T>(data);
} else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
// @ts-ignore
return parseBigNum<T>(data);
} else {

@@ -153,2 +163,11 @@ // @ts-ignore

}
/*export class Arr extends Array<Variant> {
public data: Variant[] = [];
push<T>(data: T): i32 {
return this.data.push(Variant.from<T>(data));
}
at<T>(index: i32): T {
return this.data.at(index).get<T>();
}
}*/
}

@@ -158,2 +177,22 @@

@inline
// @ts-ignore
function parseBigNum<T>(data: string): T {
// @ts-ignore
if (idof<T>() == idof<u128>()) return u128.fromString(data);
// @ts-ignore
if (idof<T>() == idof<u128Safe>()) return u128Safe.fromString(data);
// @ts-ignore
if (idof<T>() == idof<u256>()) return u128Safe.fromString(data);
// @ts-ignore
if (idof<T>() == idof<u256Safe>()) return u256Safe.fromString(data);
// @ts-ignore
if (idof<T>() == idof<i128>()) return i128.fromString(data);
// @ts-ignore
if (idof<T>() == idof<i128Safe>()) return i128Safe.fromString(data);
// @ts-ignore
if (idof<T>() == idof<i256Safe>()) return i256Safe.fromString(data);
}
// @ts-ignore
@inline
function parseString(data: string): string {

@@ -203,3 +242,3 @@ return data.slice(1, data.length - 1).replaceAll('\\"', '"');

@inline
export function parseObject<T>(data: string): T {
export function parseObject<T>(data: string): T {
let schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));

@@ -206,0 +245,0 @@ let key = "";

import { StringSink } from "as-string-sink/assembly";
import { isSpace } from "assemblyscript/std/assembly/util/string";
import { backSlashCode, quoteCode } from "./chars";
import { u128, u128Safe, u256, u256Safe, i128, i128Safe, i256Safe } from "as-bignum/assembly";
// @ts-ignore
@inline
export function isBigNum<T>(): boolean {
if (idof<T>() == idof<u128>()) return true;
if (idof<T>() == idof<u128Safe>()) return true;
if (idof<T>() == idof<u256>()) return true;
if (idof<T>() == idof<u256Safe>()) return true;
if (idof<T>() == idof<i128>()) return true;
if (idof<T>() == idof<i128Safe>()) return true;
if (idof<T>() == idof<i256Safe>()) return true;
return false;
}
// @ts-ignore
@inline
export function unsafeCharCodeAt(data: string, pos: i32): i32 {

@@ -7,0 +22,0 @@ return load<u16>(changetype<usize>(data) + ((<usize>pos) << 1));

import "wasi";
import { u128 } from "as-bignum/assembly";
import {

@@ -8,2 +9,8 @@ JSON

@json
class Stats {
wins: u128
loss: u128
}
// @ts-ignore
@json
class Vec3 {

@@ -24,2 +31,3 @@ x: f32;

isVerified: boolean;
stats: Stats
}

@@ -38,2 +46,6 @@

isVerified: true,
stats: {
wins: u128.fromString("443"),
loss: u128.fromString("693")
}
};

@@ -44,2 +56,2 @@

const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
{
"name": "json-as",
"version": "0.5.4",
"version": "0.5.5",
"description": "JSON encoder/decoder for AssemblyScript",

@@ -5,0 +5,0 @@ "types": "assembly/index.ts",

# AS-JSON
![AssemblyScript](https://img.shields.io/badge/AssemblyScript-blue)
![WebAssembly](https://img.shields.io/badge/WebAssemby-purple)
**JSON serializer/deserializer for AssemblyScript**
## Features
Full
## Installation

@@ -14,2 +17,8 @@

For arbitrary-length numbers, use
```bash
~ npm install as-bignum
```
Add the transform to your `asc` command

@@ -35,20 +44,31 @@

import { JSON } from "json-as/assembly";
import { u128 } from "as-bignum/assembly";
// @ts-ignore
@json
class Vec2 {
x: f32
y: f32
class Stats {
wins: u128
loss: u128
}
// @ts-ignore
@json
class Vec3 {
x: f32;
y: f32;
z: f32;
}
// @ts-ignore
@json
class Player {
firstName: string
lastName: string
lastActive: i32[]
age: i32
pos: Vec2
isVerified: boolean
firstName: string;
lastName: string;
lastActive: i32[];
age: i32;
pos: Vec3 | null;
isVerified: boolean;
stats: Stats
}
const data: Player = {
const player: Player = {
firstName: "Emmet",

@@ -59,7 +79,12 @@ lastName: "West",

pos: {
x: -3.4,
y: 1.2
x: 3.4,
y: 1.2,
z: 8.3
},
isVerified: true
}
isVerified: true,
stats: {
wins: u128.fromString("443"),
loss: u128.fromString("693")
}
};

@@ -66,0 +91,0 @@ const stringified = JSON.stringify<Player>(data);