5.9.101 • Published 12 months ago

@hishprorg/maxime-voluptates-enim v5.9.101

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

JSOBin

JSOBin是一个用于序列化javascript对象的规范和实现

  • 优点 对比JSON
    • 更小
      • 使用二进制格式
      • 对整数进行变长压缩
    • 更好的支持
      • 支持 循环引用
      • 支持 有向无环式引用
      • 支持 Bigint
      • 支持 部分javascript内建类
        • 支持 Map类
        • 支持 Set类
        • 支持 ArrayBuffer类
        • 支持 TypedArrey类
      • 支持 自定义类
        • 支持 自定义类的序列化和反序列化函数
      • 支持 symbol (有限制)
      • 支持 undefined
      • 无损的支持 64位浮点数 (没有转换成十进制带来的偏差)
        • 支持 负 0
        • 支持 正/负 无限
        • 支持 NaN
  • 缺点 对比JSON
    • 人类不可读 几乎抛弃了所有可读性

关于序列化后格式的规范 见此仓库中的 standard.md

使用 JSOBin

1 导入JSOBin

import { JSOBin } from "@hishprorg/maxime-voluptates-enim";

2 创建用于操作的上下文

JSOBin的编解码依赖一个上下文来操作
因为我们需要在其中注册需要序列化的类或者其他的东西

let jsob = new JSOBin();

3 序列化 (编码)

这样就编码完成了!

let serializedBinaryFormat = jsob.encode({
    testObjectKey: "test object value"
});

4 反序列化 (解码)

这样就解码完成了!

let deserializedObject = jsob.decode(serializedBinaryFormat);

5 实现自定义类的编解码

需要实现自定义类的编解码需要将类在上下文中注册
否则将会像普通js对象一样处理
用于编解码的上下文都需要注册相同的类才能使得此类支持编解码
且这些上下文中对同一类登记的标识符必须相同
不同的类必须使用不同的标识符

现在jsob这个上下文支持MyClass的编解码了!

jsob.addClass("myClassName", MyClass);

6 自定义类序列化

也许你不喜欢类的默认序列化和反序列化方案
或者他们可能不会正常工作?
这是大概正常的 因为默认的反序列化方案不会调用构造函数等等原因
很多情况下我们需要自定义的类序列化函数

import { serializationFunctionSymbol, deserializationFunctionSymbol } from "@hishprorg/maxime-voluptates-enim"; // 需要额外引用这两个符号

class MyClass
{
    /*
        这个函数在此类序列化时被调用
        他的this指向正在被序列化的对象(类的实例)
        然后他需要返回一个对象
        返回对象的键必须是字符串
        返回对象的值可以是任何其他的@hishprorg/maxime-voluptates-enim支持的类型
        当然 如果返回对象的值中存在此类 或者其他的循环 可能导致此函数被递归调用最后永远循环下去!
        所以小心使用 总之尽量避免返回对象的值中有此类本身或者其他在序列化过程可能造成循环的一切
    */
    [serializationFunctionSymbol]()
    {
        return { // 在这里导出足够多的数据以便在反序列化过程中还原此类实例
            someValue: "",
            someObject: {},
            somethingElse: [
                ""
            ]
        };
    }

    /*
        这个函数是静态的
        当需要反序列化此类时将调用此函数
        会将 数据对象 作为其参数传入
        这里的数据对象就是你在上面的函数中返回的对象!
        然后 你可以返回创建的类的实例 他会被放在本来的位置上
        或者 你也可以返回和此类无关的其他类实例或对象
        总之 返回的值将被放在这个类实例原本在@hishprorg/maxime-voluptates-enim中的位置上
    */
    static [deserializationFunctionSymbol](dataObj)
    {
        let ret = new MyClass();

        // do something here

        return ret;
    }
}

7 "序列化"安全函数

虽然我不确定这么做的具体用途
但如果你愿意你可以将函数绑定并进行编解码
注意: 这并不会导出函数的代码等 仅仅是将函数与对应标识符绑定
当你确定没有安全问题后 可以这样将函数绑定到上下文
与类的编解码相同 所有相关的上下文都需要绑定对应的函数才能完成函数的编解码

function testFunction()
{
    console.log("test");
}

jsob.addSafetyFunction("testFunction", testFunction); // 绑定函数

let functionBindTest = jsob.encode({ // 尝试序列化
    test: testFunction
});

let testObj = jsob.decode(functionBindTest); // 尝试反序列化

testObj.test(); // 他工作了!
@hishprorg/ipsa-consectetur-minima@hishprorg/nemo-blanditiis@hishprorg/neque-laborum-vel@hishprorg/quis-quibusdam@hishprorg/quibusdam-dolores@hishprorg/quis-doloribus@hishprorg/quidem-delectus@hishprorg/quidem-et@hishprorg/quis-est@hishprorg/quis-perferendis-culpa@hishprorg/sed-nihil-iusto@hishprorg/sequi-inventore-labore@hishprorg/sed-sunt@hishprorg/temporibus-aliquam-neque@hishprorg/tempore-dolor-ut@hishprorg/accusantium-architecto@hishprorg/ad-labore-dolor@hishprorg/ad-minima@hishprorg/ad-soluta-et@hishprorg/accusantium-consectetur-incidunt@hishprorg/beatae-cumque@hishprorg/beatae-animi-commodi@hishprorg/autem-quo@hishprorg/debitis-quaerat@hishprorg/debitis-qui@hishprorg/cupiditate-ut@hishprorg/dolores-ut@hishprorg/et-esse-quasi@hishprorg/est-odio-laudantium@hishprorg/eum-eos@hishprorg/et-dicta-cum@hishprorg/eum-nihil-maiores@hishprorg/illo-voluptas@hishprorg/impedit-consectetur-dicta@hishprorg/architecto-sequi@hishprorg/architecto-sapiente@hishprorg/asperiores-hic@hishprorg/aspernatur-consequatur@hishprorg/asperiores-voluptates@hishprorg/asperiores-provident@hishprorg/consectetur-sunt@hishprorg/consectetur-ab@hishprorg/consectetur-dolores@hishprorg/deserunt-est@hishprorg/deserunt-velit-quod@hishprorg/deserunt-nihil@hishprorg/deserunt-deleniti@hishprorg/doloremque-soluta@hishprorg/doloremque-mollitia@hishprorg/dolore-suscipit@hishprorg/hic-omnis@hishprorg/hic-perferendis@hishprorg/fuga-repellat@hishprorg/fugiat-accusamus-laboriosam@hishprorg/fugiat-eius@hishprorg/fugiat-libero-itaque@hishprorg/aliquam-quibusdam-repudiandae@hishprorg/aliquid-delectus-nisi@hishprorg/aliquid-ut-debitis@hishprorg/alias-maiores-saepe@hishprorg/amet-qui@hishprorg/beatae-officia@hishprorg/earum-occaecati@hishprorg/earum-sint-veritatis@hishprorg/earum-inventore@hishprorg/earum-consectetur@hishprorg/est-dicta-quis@hishprorg/esse-voluptates-veritatis@hishprorg/id-ab@hishprorg/id-harum@hishprorg/id-modi@hishprorg/id-dignissimos@hishprorg/id-voluptas@hishprorg/id-consequuntur@hishprorg/hic-repellendus-hic@hishprorg/illo-dolores@hishprorg/cumque-optio@hishprorg/cupiditate-quaerat-qui@hishprorg/doloribus-vero@hishprorg/dolorum-commodi-architecto@hishprorg/doloribus-reiciendis@hishprorg/doloribus-minima@hishprorg/dolorum-est@hishprorg/exercitationem-ipsum@hishprorg/distinctio-repudiandae-molestias@hishprorg/distinctio-alias@hishprorg/dignissimos-sunt-quaerat@hishprorg/distinctio-in@hishprorg/eos-enim-voluptate@hishprorg/eos-asperiores@hishprorg/eligendi-laborum@hishprorg/enim-fugiat-quas@hishprorg/eligendi-neque@hishprorg/eveniet-maxime@hishprorg/ex-tempora-libero@hishprorg/architecto-consectetur@hishprorg/culpa-ea-facilis@hishprorg/culpa-ratione-dolores@hishprorg/corrupti-voluptas@hishprorg/corrupti-vero
3.6.61

1 year ago

3.6.60

1 year ago

2.5.46

1 year ago

2.3.39

1 year ago

2.1.16

1 year ago

2.3.38

1 year ago

2.1.17

1 year ago

2.1.15

1 year ago

2.3.35

1 year ago

2.3.34

1 year ago

2.3.37

1 year ago

2.3.36

1 year ago

2.3.31

1 year ago

2.3.30

1 year ago

2.3.33

1 year ago

2.3.32

1 year ago

5.8.92

12 months ago

5.8.91

12 months ago

4.6.62

1 year ago

4.6.63

1 year ago

2.1.18

1 year ago

4.6.61

1 year ago

2.1.19

1 year ago

4.6.66

1 year ago

4.6.67

1 year ago

4.6.64

1 year ago

4.6.65

1 year ago

2.1.23

1 year ago

2.1.24

1 year ago

4.6.68

1 year ago

2.1.21

1 year ago

4.6.69

1 year ago

2.1.22

1 year ago

5.9.101

12 months ago

2.1.20

1 year ago

5.9.100

12 months ago

3.6.59

1 year ago

3.6.58

1 year ago

3.6.57

1 year ago

3.6.56

1 year ago

3.6.55

1 year ago

3.6.54

1 year ago

3.6.53

1 year ago

3.6.52

1 year ago

3.6.51

1 year ago

3.5.51

1 year ago

2.4.39

1 year ago

3.5.50

1 year ago

5.7.91

12 months ago

5.7.90

12 months ago

2.2.28

1 year ago

2.2.29

1 year ago

2.2.26

1 year ago

2.2.27

1 year ago

2.2.24

1 year ago

2.4.46

1 year ago

2.2.25

1 year ago

2.4.43

1 year ago

2.4.42

1 year ago

2.4.45

1 year ago

2.4.44

1 year ago

2.4.41

1 year ago

2.4.40

1 year ago

5.7.85

1 year ago

5.7.84

1 year ago

5.7.87

12 months ago

5.7.86

1 year ago

5.7.81

1 year ago

5.7.80

1 year ago

5.7.83

1 year ago

5.7.82

1 year ago

4.7.70

1 year ago

4.7.71

1 year ago

4.7.72

1 year ago

4.7.73

1 year ago

2.0.15

1 year ago

2.2.30

1 year ago

5.7.89

12 months ago

5.7.88

12 months ago

5.9.97

12 months ago

5.7.74

1 year ago

3.5.47

1 year ago

5.9.98

12 months ago

5.7.73

1 year ago

3.5.46

1 year ago

5.9.99

12 months ago

5.7.76

1 year ago

5.7.75

1 year ago

5.9.93

12 months ago

5.9.94

12 months ago

5.9.95

12 months ago

5.9.96

12 months ago

5.9.92

12 months ago

3.5.49

1 year ago

3.5.48

1 year ago

4.7.69

1 year ago

5.7.78

1 year ago

5.7.77

1 year ago

5.7.79

1 year ago

2.0.14

1 year ago

2.0.13

1 year ago

2.0.11

1 year ago

2.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago