今天, 我们发布了 kat+ 0.0.1
这是我们历时四个月基于南博kat库, 拓展改进的一个 Java 工具库。

那么 kat+ 库相对于南博 kat 库有什么优势呢?
KAT+ 一个轻量级的 KAT + XML + JSON 库

相对于南博 kat 库, kat+ 不仅支持 kat 还支持 xml、json,
并且支持了 Custom Coder/Spare/Supplier 等等, 拓展性更好, kat+ 后续还可以支持更多的数据文本

步入正题

我们将 kat+ 开源在 https://github.com/katplus/kat
欢迎大家给 Star 和提交 PR (由于我们有代码洁癖, 所以建议注意代码风格哈哈)

并且我们发布到了 Maven 中央仓库 https://search.maven.org/artifact/plus.kat/kat

食用方法

添加依赖

Maven:

<dependency>
    <groupId>plus.kat</groupId>
    <artifactId>kat</artifactId>
    <version>0.0.1</version>
</dependency>

Gradle:

dependencies {
    implementation 'plus.kat:kat:0.0.1'
}

Kotlin Gradle:

dependencies {
    implementation("plus.kat:kat:0.0.1")
}

使用步骤

这里简单使用, 不具体阐述, 详细可以到README.md

文本转对象

@Embed()
class User {
    @Expose("id")
    private int id;

    @Expose("name")
    private String name;
}

Java:

// kat
User user = Kat.decode(
    User.class, "${i:id(1)s:name(kraity)}"
);

// xml
User user = Doc.decode(
    User.class, "<user><id>1</id><name>kraity</name></user>"
);

// json
User user = Json.decode(
    User.class, "{\"id\":1,\"name\":\"kraity\"}"
);

使用供应商

@Embed("plus.kat.entity.User")
class User {
    @Expose("id")
    private int id;

    @Expose("name")
    private String name;
}

Use class type:

// default supplier
Supplier supplier = Supplier.ins();

// cast
User user = supplier.cast(
    User.class, Map.of(
        "id", 1, "name", "kraity"
    )
);

// kat
User user = supplier.read(
    User.class, new Event<>(
        "${i:id(1)s:name(kraity)}"
    );
);

// xml
User user = supplier.down(
    User.class, new Event<>(
        "<user><id>1</id><name>kraity</name></user>"
    );
);

// json
User user = supplier.parse(
    User.class, new Event<>(
        "{\"id\":1,\"name\":\"kraity\"}"
    );
);

Use package name:

// default Supplier
Supplier supplier = Supplier.ins();

// register User
supplier.embed(User.class);

// cast
User user = supplier.cast(
    "plus.kat.entity.User", Map.of(
        "id", 1, "name", "kraity"
    )
);

// kat
User user = supplier.read(
    "plus.kat.entity.User", new Event<>(
        "${i:id(1)s:name(kraity)}"
    );
);

// xml
User user = supplier.down(
    "plus.kat.entity.User", new Event<>(
        "<user><id>1</id><name>kraity</name></user>"
    );
);

// json
User user = supplier.parse(
    "plus.kat.entity.User", new Event<>(
        "{\"id\":1,\"name\":\"kraity\"}"
    );
);

使用事件

Create event:

// create event
Event<?> event = new Event<>();

// specify supplier
event.with(
    Supplier.ins()
);

// specify reader
Reader reader = ...;
event.with(reader);

// specify type
event.with(User.class);

// specify flag
event.with(Flag.ENUM_ORDINAL);

Extends event:

// default supplier
Supplier supplier = Supplier.ins();

User[] data = supplier.read(
    "A", new Event<User[]>("...") {}  // similar: event.with(User[].class);
);

ArrayList<User> data = supplier.read(
    "L", new Event<ArrayList<User>>("...") {}
);

HashMap<Long, User> data = supplier.read(
    "M", new Event<HashMap<Long, User>>("...") {}
);

Mutable event:

// use file
File file = ...;
Event<User> event = new Event<>(file);
Event<User> event = Event.file("./test/entity/user.kat");

// use remote
URL url = ...;
Event<User> event = new Event<>(url);
Event<User> event = Event.remote("https://kat.plus/test/entity/user.kat");

// use String
String data = ...;
Event<User> event = new Event<>(data);

// use String with Cipher
String data = ...;
Cipher cipher = ...;
Event<User> event = new Event<>(data, cipher);

// use byte array
byte[] data = ...;
Event<User> event = new Event<>(data);

// use byte array with Cipher
byte[] data = ...;
Cipher cipher = ...;
Event<User> event = new Event<>(data, cipher);

// use InputStream
InputStream stream = ...;
Event<User> event = new Event<>(stream);

// use InputStream with Cipher
Cipher cipher = Cipher.getInstance(
    "AES/CBC/PKCS5Padding"
);
cipher.init(
    Cipher.DECRYPT_MODE,
    new SecretKeySpec(
        "key".getBytes(), "AES"
    ),
    new IvParameterSpec(
        "iv".getBytes()
    )
);
InputStream stream = ...;
Event<User> event = new Event<>(stream, cipher);

END

这里简单的记录一下, 待慢慢完善文档

#none
更新于: 2022年06月27日 11:24
384
0
发表评论