wip classes
This commit is contained in:
parent
981e74c3cc
commit
78261b5384
5 changed files with 2671 additions and 4 deletions
1195
dist/index.js
vendored
Normal file
1195
dist/index.js
vendored
Normal file
File diff suppressed because it is too large
Load diff
1409
dist/test.js
vendored
Normal file
1409
dist/test.js
vendored
Normal file
File diff suppressed because it is too large
Load diff
35
src/de.ts
35
src/de.ts
|
@ -74,7 +74,6 @@ export class CommaSeparated implements MapAccess, IterableAccess {
|
||||||
nextValueSeed<T, V extends Deserialize<T>>(seed: V): Nullable<T> {
|
nextValueSeed<T, V extends Deserialize<T>>(seed: V): Nullable<T> {
|
||||||
const next = this.de.buffer.next()
|
const next = this.de.buffer.next()
|
||||||
if (next !== Token.Colon) {
|
if (next !== Token.Colon) {
|
||||||
console.log(this.de.buffer.toString())
|
|
||||||
throw unexpected(':', next.toString(), this.de.buffer.position)
|
throw unexpected(':', next.toString(), this.de.buffer.position)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,11 +171,29 @@ class StringBuffer {
|
||||||
return this.take(index)
|
return this.take(index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
takeUntil(fn: Predicate<number>): StringBuffer {
|
||||||
|
return this.takeWhile((v: number) => !fn(v))
|
||||||
|
}
|
||||||
|
|
||||||
drop(limit: number) {
|
drop(limit: number) {
|
||||||
this.index += limit
|
this.index += limit
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dropWhile(fn: Predicate<number>): StringBuffer {
|
||||||
|
let index = 0
|
||||||
|
|
||||||
|
while (!this.done() && fn(this.at(index))) {
|
||||||
|
index += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.drop(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
dropUntil(fn: Predicate<number>): StringBuffer {
|
||||||
|
return this.dropWhile((v: number) => !fn(v))
|
||||||
|
}
|
||||||
|
|
||||||
peek(limit: number = 1): StringBuffer {
|
peek(limit: number = 1): StringBuffer {
|
||||||
const index = this.index
|
const index = this.index
|
||||||
return this.slice(index, index + limit)
|
return this.slice(index, index + limit)
|
||||||
|
@ -279,6 +296,22 @@ export class JSONDeserializer implements Deserializer {
|
||||||
return this._deserializeObject(visitor.visitObject.bind(visitor))
|
return this._deserializeObject(visitor.visitObject.bind(visitor))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deserializeClass<T, V extends Visitor<T>>(name: string, visitor: V): T {
|
||||||
|
if (this.buffer.peek(9).startsWith('{"type":"')) {
|
||||||
|
const name = this.buffer.drop(9).takeUntil((v: number) => v === Token.Quote)
|
||||||
|
|
||||||
|
if (this.buffer.startsWith('","object":')) {
|
||||||
|
this.buffer.drop(11)
|
||||||
|
return this._deserializeObject(visitor.visitClass.bind(visitor, name.toString()))
|
||||||
|
} else {
|
||||||
|
throw unexpected('"object" property', this.buffer.toString(), this.buffer.position)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw unexpected('"type" property', this.buffer.toString(), this.buffer.position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
deserializeString<T, V extends Visitor<T>>(visitor: V): T {
|
deserializeString<T, V extends Visitor<T>>(visitor: V): T {
|
||||||
const next = this.buffer.take()
|
const next = this.buffer.take()
|
||||||
if (next.next() === Token.Quote) {
|
if (next.next() === Token.Quote) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { serializeWith } from 'serde/ser'
|
import { serializeWith } from 'serde/ser'
|
||||||
import { Deserialize } from 'serde/de'
|
|
||||||
import { JSONSerializer } from './ser'
|
import { JSONSerializer } from './ser'
|
||||||
|
import { Deserialize } from 'serde/de'
|
||||||
import { JSONDeserializer } from './de'
|
import { JSONDeserializer } from './de'
|
||||||
|
|
||||||
export function toString(value: any): string {
|
export function toString(value: any): string {
|
||||||
|
|
34
src/ser.ts
34
src/ser.ts
|
@ -1,7 +1,5 @@
|
||||||
import { IterableSerializer, ObjectSerializer, Serializable, Serializer, serializeWith } from 'serde/ser'
|
import { IterableSerializer, ObjectSerializer, Serializable, Serializer, serializeWith } from 'serde/ser'
|
||||||
|
|
||||||
const Identifier = (value: string) => `\x02${value}\x04`
|
|
||||||
|
|
||||||
class JSONObjectSerializer implements ObjectSerializer<void> {
|
class JSONObjectSerializer implements ObjectSerializer<void> {
|
||||||
private ser: JSONSerializer
|
private ser: JSONSerializer
|
||||||
private first: boolean = true
|
private first: boolean = true
|
||||||
|
@ -55,6 +53,34 @@ class JSONIterableSerializer implements IterableSerializer<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class JSONClassSerializer implements ObjectSerializer<void> {
|
||||||
|
outer: JSONObjectSerializer
|
||||||
|
inner: JSONObjectSerializer
|
||||||
|
|
||||||
|
constructor(serializer: JSONSerializer, name: string) {
|
||||||
|
this.outer = new JSONObjectSerializer(serializer)
|
||||||
|
|
||||||
|
this.outer.serializeKey('type')
|
||||||
|
this.outer.serializeValue(name)
|
||||||
|
|
||||||
|
this.outer.serializeKey('object')
|
||||||
|
this.inner = new JSONObjectSerializer(serializer)
|
||||||
|
}
|
||||||
|
|
||||||
|
serializeKey<U extends Serializable>(key: U): void {
|
||||||
|
return this.inner.serializeKey(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
serializeValue<U extends Serializable>(value: U): void {
|
||||||
|
return this.inner.serializeValue(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
end(): void {
|
||||||
|
this.inner.end()
|
||||||
|
this.outer.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class JSONSerializer implements Serializer<void> {
|
export class JSONSerializer implements Serializer<void> {
|
||||||
output: string = ''
|
output: string = ''
|
||||||
|
|
||||||
|
@ -83,6 +109,10 @@ export class JSONSerializer implements Serializer<void> {
|
||||||
return new JSONObjectSerializer(this)
|
return new JSONObjectSerializer(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serializeClass(name: PropertyKey): ObjectSerializer<void> {
|
||||||
|
return new JSONClassSerializer(this, name as string)
|
||||||
|
}
|
||||||
|
|
||||||
serializeNumber(value: number) {
|
serializeNumber(value: number) {
|
||||||
this.write(value.toString())
|
this.write(value.toString())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue