mirror of
https://github.com/fluencelabs/fluid
synced 2025-06-22 01:21:32 +00:00
json parsing
This commit is contained in:
@ -0,0 +1,85 @@
|
||||
import {JSONDecoder, JSONHandler} from "../node_modules/assemblyscript-json/assembly/decoder";
|
||||
|
||||
export enum Action {
|
||||
Post,
|
||||
Fetch,
|
||||
Unknown
|
||||
// Error
|
||||
}
|
||||
|
||||
export abstract class Request {
|
||||
public action: Action = Action.Unknown;
|
||||
}
|
||||
|
||||
export class PostRequest extends Request {
|
||||
|
||||
public readonly msg: string;
|
||||
public readonly handle: string;
|
||||
|
||||
constructor(msg: string, handle: string) {
|
||||
super();
|
||||
this.msg = msg;
|
||||
this.handle = handle;
|
||||
this.action = Action.Post;
|
||||
}
|
||||
}
|
||||
|
||||
export class FetchRequest extends Request {
|
||||
constructor() {
|
||||
super();
|
||||
this.action = Action.Fetch;
|
||||
}
|
||||
}
|
||||
|
||||
export class UnknownRequest extends Request {
|
||||
constructor() {
|
||||
super();
|
||||
this.action = Action.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
export function string2Bytes(str: string): Uint8Array {
|
||||
return Uint8Array.wrap(String.UTF8.encode(str));
|
||||
}
|
||||
|
||||
export function decode(input: string): Request {
|
||||
let jsonHandler = new RequestJSONEventsHandler();
|
||||
let decoder = new JSONDecoder<RequestJSONEventsHandler>(jsonHandler);
|
||||
|
||||
let bytes = string2Bytes(input);
|
||||
|
||||
decoder.deserialize(bytes);
|
||||
|
||||
let action = jsonHandler.action;
|
||||
|
||||
let request: Request;
|
||||
|
||||
if (action == "Fetch") {
|
||||
request = new FetchRequest();
|
||||
} else if (action == "Post") {
|
||||
request = new PostRequest(jsonHandler.msg, jsonHandler.handle)
|
||||
} else {
|
||||
request = new UnknownRequest()
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
class RequestJSONEventsHandler extends JSONHandler {
|
||||
|
||||
public action: string;
|
||||
public msg: string;
|
||||
public handle: string;
|
||||
|
||||
setString(name: string, value: string): void {
|
||||
|
||||
if (name == "action") {
|
||||
this.action = value;
|
||||
} else if (name == "msg") {
|
||||
this.msg = value;
|
||||
} else if (name == "handle") {
|
||||
this.handle = value;
|
||||
}
|
||||
// json scheme is not strict, so we won't throw an error on excess fields
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user