Giter VIP home page Giter VIP logo

model's Introduction

UON Model

A decorator-based schema/model library with json and binary (de)serialization.

Getting started

Install @uon/model using npm

    npm i @uon/model

Usage

Model definitions are expressed in the following way:

import { Model, Member, ArrayMember } from '@uon/model'

@Model()
export class MyModel {

    // Generic member supports String, Boolean, Number and Date types
    @Member() 
    myStringField: string;

    // For array members you need to specify the array element type
    // as a decorator argument due to TypeScript not emiting array element type
    @ArrayMember(String) 
    myArrayField: string[];

}

TypedNumber

In order to efficiently serialize to binary, number types have been added.

Generic members with type Number are treated as Float64.

@Model()
export class MyModel {

    // Supported number types are Int8, Int16, Int32, Uint8, Uint16, Uint32, Float32 and Float64, (no (U)Int64 yet)
    @NumberMember(Uint8) myByte: number;

    // You can also use a TypedNumber as array element type 
    @ArrayMember(Uint32) myNumberArray: number[];

}

Modelizing an external type

If you want the serializers to be able to support types that are not under your control, we provide a utility function to let you "modelize" it. Here is an example:

import { Modelize, NumberMember, Float32 } from '@uon/model';
import { Vec2 } from 'some-math-lib';

Modelize(Vec2, {
    x: NumberMember(Float32),
    y: NumberMember(Float32)
});

Serialization

To serialize a model instance you can either do it manually via the JsonSerializer interface or simply pass it to JSON.stringify().

let obj = new MyModel();
let serializer = new JsonSerializer(MyModel);
let serialized = serializer.serialize(obj);

Note that serializer.serialize does not return a string but rather a JSON object using only builtin types (String, Number, Boolean, Array, Object and Date).

To deserialize, we do the following:

let serializer = new JsonSerializer(MyModel);
let my_obj = serializer.deserialize(JSON.parse(my_json_str));

Limitations

  • Array members are limited to 1 dimension, n-dimensional arrays might be supported in the future.

TODO

  • Add serialization support for builtin typed arrays (Uint8Array, etc...)
  • Handle array element validation, also TypedNumbers range
  • Complete this README

model's People

Contributors

uon-io avatar

Watchers

 avatar

model's Issues

Feature: Ability to specify how a class is serialized and deserialized

Because I want to specify a class that behave like a type.

For example:

@Model()
class MyCounterClass {
     @Member()
    counter: { [key: string]: number; }

    // Some Methods
}

will be serialized this way:

{counter: { [key: string]: number; }}

but I would like it to be this way directly:

{ [key: string]: number; }

I'm currently declaring:

type MyCounterType = { [key: string]: number; }

with the drawback that a type doesn't have methods and I have to create another class for this:

class MyCounterTypeMethods {
   // Some static methods to manipulate MyCounterType.
}

Unable to detect changes on a deserialized array

When I deserialize an object using the JsonSerializer the type of the array change. When displaying the constructor.name of the array, the name become "bound Array".

And then overriding the push() method for example doesn't work:

const MYTYPE_SERIALIZER = new JsonSerializer(MyType)

console.log(data.myArray.constructor.name); // It display "Array"
let myType = MYTYPE_SERIALIZER.deserialize(data, true);
console.log(myType .myArray.constructor.name); // It display "bound Array"

let myArray = myType.myArray;
myArray.push = function () {
        console.log("Debug");
        return Array.prototype.push.apply(this, arguments);
}
myArray.push("Something"); // The console.log("Debug") will not be called

Unable to generalize model members.

I'm trying to do the following:

export class CommonModel {

    @Member()
    createdOn: Date;

    @Member()
    updatedOn: Date;
}

@Model()
export class SubscriptionSeat extends CommonModel {

    @ID()
    id: string;
}

But I'm getting the following error:

Error: Can only have 1 Member decorator on a property, got 2 on SubscriptionSeat.id
    at eval (webpack:///./node_modules/@uon/model/meta/model.decorator.js?:29:19)

I have debug and the property id get included twice: filtered = [{"key":"id"},{"key":"id"}]

What am I doing wrong? Is it supported?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.