Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromJSON/toJSON #72

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/AsyncData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { keys, values } from "./Dict";
import { Option, Result } from "./OptionResult";
import { LooseRecord } from "./types";
import { JsonAsyncData, LooseRecord } from "./types";
import { zip } from "./ZipUnzip";

class __AsyncData<A> {
Expand Down Expand Up @@ -92,6 +92,14 @@ class __AsyncData<A> {
// @ts-ignore
value != null && value.__boxed_type__ === "AsyncData";

static fromJSON = <A>(value: JsonAsyncData<A>) => {
return value.tag === "NotAsked"
? AsyncData.NotAsked()
: value.tag === "Loading"
? AsyncData.Loading()
: AsyncData.Done(value.value);
};

map<B>(this: AsyncData<A>, func: (value: A) => B): AsyncData<B> {
if (this === NOT_ASKED || this === LOADING) {
return this as unknown as AsyncData<B>;
Expand Down Expand Up @@ -266,6 +274,14 @@ class __AsyncData<A> {
isNotAsked(this: AsyncData<A>): this is NotAsked<A> {
return this === NOT_ASKED;
}

toJSON(this: AsyncData<A>): JsonAsyncData<A> {
return this.match<JsonAsyncData<A>>({
NotAsked: () => ({ __boxed_type__: "AsyncData", tag: "NotAsked" }),
Loading: () => ({ __boxed_type__: "AsyncData", tag: "Loading" }),
Done: (value) => ({ __boxed_type__: "AsyncData", tag: "Done", value }),
});
}
}

// @ts-expect-error
Expand Down
1 change: 1 addition & 0 deletions src/Boxed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export { Future } from "./Future";
export { Lazy } from "./Lazy";
export { Option, Result } from "./OptionResult";
export * as Serializer from "./Serializer";
export { JsonAsyncData, JsonOption, JsonResult } from "./types";
26 changes: 25 additions & 1 deletion src/OptionResult.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { keys, values } from "./Dict";
import { LooseRecord } from "./types";
import { JsonOption, JsonResult, LooseRecord } from "./types";
import { zip } from "./ZipUnzip";

class __Option<A> {
Expand Down Expand Up @@ -99,6 +99,10 @@ class __Option<A> {
: a.tag === b.tag;
};

static fromJSON = <A>(value: JsonOption<A>) => {
return value.tag === "None" ? Option.None() : Option.Some(value.value);
};

/**
* Returns the Option containing the value from the callback
*
Expand Down Expand Up @@ -222,6 +226,13 @@ class __Option<A> {
isNone(this: Option<A>): this is None<A> {
return this === NONE;
}

toJSON(this: Option<A>): JsonOption<A> {
return this.match<JsonOption<A>>({
None: () => ({ __boxed_type__: "Option", tag: "None" }),
Some: (value) => ({ __boxed_type__: "Option", tag: "Some", value }),
});
}
}

// @ts-expect-error
Expand Down Expand Up @@ -387,6 +398,12 @@ class __Result<A, E> {
return false;
};

static fromJSON = <A, E>(value: JsonResult<A, E>) => {
return value.tag === "Ok"
? Result.Ok(value.value)
: Result.Error(value.error);
};

/**
* Returns the Result containing the value from the callback
*
Expand Down Expand Up @@ -522,6 +539,13 @@ class __Result<A, E> {
isError(this: Result<A, E>): this is Error<A, E> {
return this.tag === "Error";
}

toJSON(this: Result<A, E>): JsonResult<A, E> {
return this.match<JsonResult<A, E>>({
Ok: (value) => ({ __boxed_type__: "Result", tag: "Ok", value }),
Error: (error) => ({ __boxed_type__: "Result", tag: "Error", error }),
});
}
}

// @ts-expect-error
Expand Down
45 changes: 4 additions & 41 deletions src/Serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,7 @@ import { AsyncData } from "./AsyncData";
import { Option, Result } from "./OptionResult";

export const encode = (value: any, indent?: number | undefined) => {
return JSON.stringify(
value,
function (key, value) {
if (value == null) {
return;
}
if (value.__boxed_type__ === "Option") {
return {
__boxed_type__: "Option",
tag: value.tag,
value: value.value,
};
}
if (value.__boxed_type__ === "Result") {
return {
__boxed_type__: "Result",
tag: value.tag,
value: value.value,
error: value.error,
};
}
if (value.__boxed_type__ === "AsyncData") {
return {
__boxed_type__: "AsyncData",
tag: value.tag,
value: value.value,
};
}
return value;
},
indent,
);
return JSON.stringify(value, null, indent);
};

export const decode = (value: string) => {
Expand All @@ -42,19 +11,13 @@ export const decode = (value: string) => {
return value;
}
if (value.__boxed_type__ === "Option") {
return value.tag === "Some" ? Option.Some(value.value) : Option.None();
return Option.fromJSON(value);
}
if (value.__boxed_type__ === "Result") {
return value.tag === "Ok"
? Result.Ok(value.value)
: Result.Error(value.error);
return Result.fromJSON(value);
}
if (value.__boxed_type__ === "AsyncData") {
return value.tag === "NotAsked"
? AsyncData.NotAsked()
: value.tag === "Loading"
? AsyncData.Loading()
: AsyncData.Done(value.value);
return AsyncData.fromJSON(value);
}
return value;
});
Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export type LooseRecord<T> = Record<PropertyKey, T>;

export type JsonResult<A, E> =
| { __boxed_type__: "Result"; tag: "Ok"; value: A }
| { __boxed_type__: "Result"; tag: "Error"; error: E };

export type JsonAsyncData<A> =
| { __boxed_type__: "AsyncData"; tag: "NotAsked" }
| { __boxed_type__: "AsyncData"; tag: "Loading" }
| { __boxed_type__: "AsyncData"; tag: "Done"; value: A };

export type JsonOption<A> =
| { __boxed_type__: "Option"; tag: "None" }
| { __boxed_type__: "Option"; tag: "Some"; value: A };
3 changes: 3 additions & 0 deletions test/AsyncData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,15 @@ test("AsyncData.equals", () => {

test("AsyncData serialize", () => {
expect(JSON.parse(JSON.stringify(AsyncData.NotAsked()))).toEqual({
__boxed_type__: "AsyncData",
tag: "NotAsked",
});
expect(JSON.parse(JSON.stringify(AsyncData.Loading()))).toEqual({
__boxed_type__: "AsyncData",
tag: "Loading",
});
expect(JSON.parse(JSON.stringify(AsyncData.Done(1)))).toEqual({
__boxed_type__: "AsyncData",
tag: "Done",
value: 1,
});
Expand Down
2 changes: 2 additions & 0 deletions test/Option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ test("Option.equals", () => {

test("Option serialize", () => {
expect(JSON.parse(JSON.stringify(Option.None()))).toEqual({
__boxed_type__: "Option",
tag: "None",
});
expect(JSON.parse(JSON.stringify(Option.Some(1)))).toEqual({
__boxed_type__: "Option",
tag: "Some",
value: 1,
});
Expand Down
2 changes: 2 additions & 0 deletions test/Result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ test("Result.equals", () => {

test("Result serialize", () => {
expect(JSON.parse(JSON.stringify(Result.Error(1)))).toEqual({
__boxed_type__: "Result",
tag: "Error",
error: 1,
});
expect(JSON.parse(JSON.stringify(Result.Ok(1)))).toEqual({
__boxed_type__: "Result",
tag: "Ok",
value: 1,
});
Expand Down
Loading