-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
198 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { jsonArrayMember, jsonMember, jsonObject, TypedJSON } from "../js/typedjson"; | ||
|
||
describe('json (without automatic stringify)', function () { | ||
describe('string', function () { | ||
it('should deserialize', function () { | ||
// stringified json version cuz "" | ||
expect(TypedJSON.parse('"str"', String)).toEqual('str'); | ||
// already parsed | ||
expect(TypedJSON.parse('str', String)).toEqual('str'); | ||
|
||
// because we detect naively | ||
try { | ||
expect(TypedJSON.parse('"sdfs"fdsf"', String)).toEqual(undefined); | ||
fail(); | ||
} catch(e) {} | ||
}); | ||
|
||
it('should serialize', function () { | ||
expect(TypedJSON.toPlainJson('str', String)).toEqual('str'); | ||
}); | ||
}); | ||
|
||
describe('rest of primitives', function () { | ||
it('should deserialize', function () { | ||
expect(TypedJSON.parse(45834, Number)).toEqual(45834); | ||
expect(TypedJSON.parse(true, Boolean)).toEqual(true); | ||
expect(TypedJSON.parse(1543915254, Date)).toEqual(new Date(1543915254)); | ||
expect(TypedJSON.parse('1970-01-18T20:51:55.254Z', Date)).toEqual(new Date(1543915254)); | ||
|
||
const dataBuffer = Uint8Array.from([100, 117, 112, 97]) as any; | ||
expect(TypedJSON.parse('畤慰', ArrayBuffer)).toEqual(dataBuffer); | ||
expect(TypedJSON.parse('畤慰', DataView)).toEqual(dataBuffer); | ||
expect(TypedJSON.parse([100, 117, 112, 97], Uint8Array)).toEqual(dataBuffer); | ||
}); | ||
|
||
it('should serialize', function () { | ||
expect(TypedJSON.toPlainJson(45834, Number)).toEqual(45834); | ||
expect(TypedJSON.toPlainJson(true, Boolean)).toEqual(true); | ||
const dateMs = new Date(1543915254); | ||
expect(TypedJSON.toPlainJson(dateMs, Date)).toEqual(dateMs); | ||
const dateStr = new Date('2018-12-04T09:20:54'); | ||
expect(TypedJSON.toPlainJson(dateStr, Date)).toEqual(dateStr); | ||
|
||
const buffer = new ArrayBuffer(4); | ||
const view = new DataView(buffer); | ||
view.setInt8(0, 100); | ||
view.setInt8(1, 117); | ||
view.setInt8(2, 112); | ||
view.setInt8(3, 97); | ||
expect(TypedJSON.toPlainJson(buffer, ArrayBuffer)).toEqual('畤慰'); | ||
expect(TypedJSON.toPlainJson(view, DataView)).toEqual('畤慰'); | ||
expect(TypedJSON.toPlainJson(new Uint8Array(buffer), Uint8Array)).toEqual([100, 117, 112, 97]); | ||
}); | ||
}); | ||
|
||
describe('object', function () { | ||
|
||
@jsonObject | ||
class SomeThing { | ||
@jsonMember | ||
propStr: String; | ||
@jsonMember | ||
propNum: number; | ||
@jsonArrayMember(String) | ||
propArr: String[]; | ||
} | ||
|
||
const json = Object.freeze({ | ||
propStr: 'dsgs', | ||
propNum: 653, | ||
propArr: ['dslfks'], | ||
}); | ||
|
||
it('should deserialize', function () { | ||
expect(TypedJSON.parse(json, SomeThing)).toEqual(Object.assign(new SomeThing(), json)); | ||
expect(TypedJSON.parseAsArray([json], SomeThing)).toEqual([Object.assign(new SomeThing(), json)]); | ||
}); | ||
|
||
it('should serialize', function () { | ||
expect(TypedJSON.toPlainJson(Object.assign(new SomeThing(), json), SomeThing)).toEqual(json); | ||
expect(TypedJSON.toPlainArray([Object.assign(new SomeThing(), json)], SomeThing)).toEqual([json]); | ||
}); | ||
}); | ||
|
||
describe('array', function () { | ||
it('should deserialize', function () { | ||
expect(TypedJSON.parseAsArray(['alas', 'dfsd'], String)).toEqual(['alas', 'dfsd']); | ||
}); | ||
|
||
it('should serialize', function () { | ||
expect(TypedJSON.toPlainArray(['alas', 'dfsd'], String)).toEqual(['alas', 'dfsd']); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { parseToJSObject } from '../src/typedjson/helpers'; | ||
|
||
describe("parse To Object", function () { | ||
it("should passthrough objects", function () { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
}; | ||
|
||
const obj2 = parseToJSObject(obj, Object); | ||
expect(obj2).toBe(obj); | ||
}); | ||
|
||
it("should passthrough arrays", function () { | ||
const arr = [{ | ||
a: 1, | ||
b: 2, | ||
}]; | ||
|
||
const arr2 = parseToJSObject(arr, Array); | ||
expect(arr2).toBe(arr); | ||
}); | ||
|
||
it("should parse object string", function () { | ||
const arr = { | ||
a: 1, | ||
b: 2, | ||
}; | ||
|
||
const arr2 = parseToJSObject(JSON.stringify(arr), Object); | ||
expect(arr2).toEqual(arr); | ||
}); | ||
|
||
it("should passthrough primitives", function () { | ||
expect(parseToJSObject(1, Number)).toBe(1); | ||
expect(parseToJSObject(false, Boolean)).toBe(false); | ||
}); | ||
|
||
it("should parse strings with quotes, but passthrough other", function () { | ||
// string is obvious | ||
expect(parseToJSObject('"I am a string"', String)).toEqual('I am a string'); | ||
expect(parseToJSObject('just a string', String)).toBe('just a string'); | ||
// but also the types that are serialized to string | ||
expect(parseToJSObject('"1970-01-18T20:51:55.254Z"', Date)).toEqual('1970-01-18T20:51:55.254Z'); | ||
expect(parseToJSObject('1970-01-18T20:51:55.254Z', Date)).toBe('1970-01-18T20:51:55.254Z'); | ||
expect(parseToJSObject('"畤慰"', ArrayBuffer)).toEqual('畤慰'); | ||
expect(parseToJSObject('畤慰', ArrayBuffer)).toBe('畤慰'); | ||
expect(parseToJSObject('"畤慰"', DataView)).toEqual('畤慰'); | ||
expect(parseToJSObject('畤慰', DataView)).toBe('畤慰'); | ||
}); | ||
|
||
it("should passthrough builtins", function () { | ||
const date = new Date; | ||
expect(parseToJSObject(date, Date)).toBe(date); | ||
const buffer = new ArrayBuffer(3); | ||
expect(parseToJSObject(buffer, ArrayBuffer)).toBe(buffer); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters