Comments (2)
不会的, 这两个可以共同使用;
检查下,代码中应该存在两个可变数据区。
from magic-byte.
不会的, 这两个可以共同使用; 检查下,代码中应该存在两个可变数据区。
下面这个代码就会报dynamicSize & dynamicSizeOf only use one in the class;
异常
@MagicClass
public class A {
@MagicField(order = 1)
private int id;
@MagicField(order = 2)
@MagicConverter(converter = BConvert.class)
private String b;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
A a = (A) o;
return id == a.id && Objects.equals(b, a.b);
}
@Override
public int hashCode() {
return Objects.hash(id, b);
}
public static class BConvert implements MConverter<String> {
@Override
public MResult<String> pack(int nextReadIndex, byte[] fullBytes, String[] attachParams, Class clz, Object obj) {
A a = (A) obj;
if (a.getId() == 1) {
return MResult.build(1, new String(Arrays.copyOfRange(fullBytes, nextReadIndex, nextReadIndex + 1)));
} else {
return MResult.build(2, new String(Arrays.copyOfRange(fullBytes, nextReadIndex, nextReadIndex + 2)));
}
}
@Override
public byte[] unpack(String object, String[] attachParams) {
return object.getBytes(StandardCharsets.UTF_8);
}
}
}
@MagicClass
public class B {
@MagicField(order = 1)
private A a;
@MagicField(order = 2, dynamicSize = true, size = 10)
private byte[] data;
@MagicField(order = 3, size = 4)
private byte[] check;
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public byte[] getCheck() {
return check;
}
public void setCheck(byte[] check) {
this.check = check;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
B b = (B) o;
return Objects.equals(a, b.a) && Arrays.equals(data, b.data) && Arrays.equals(check, b.check);
}
@Override
public int hashCode() {
int result = Objects.hash(a);
result = 31 * result + Arrays.hashCode(data);
result = 31 * result + Arrays.hashCode(check);
return result;
}
}
@Test
public void testDynamicSizeWithConvert() {
MagicByte.configDefaultCharset(StandardCharsets.UTF_8);
A a = new A();
a.setId(1);
a.setB("1");
byte[] data = MagicByte.unpackToByte(a);
A a1 = MagicByte.pack(data, A.class);
Assert.assertEquals(a, a1);
B b = new B();
b.setA(a);
b.setData("你好".getBytes());
b.setCheck(new byte[4]);
data = MagicByte.unpackToByte(b);
B b1 = MagicByte.pack(data, B.class);
Assert.assertEquals(b, b1);
}
from magic-byte.
Related Issues (20)
- 能否提供一个方法获取标记为 magicclass 的类的二进制数据长度 HOT 15
- @MagicConverter 的 attachParams 能否通过读取别的值获取参数 HOT 8
- 新增一个方法:返回读取的字节数和对象
- 自定义序列化传入的Object,应该传入根基对象 HOT 5
- 自定义序列化 unpack 方法希望传入根对象
- 异常栈无法看出报错位置 HOT 3
- byte[] 这种不定长类型如果配置了 custom 注解还检查是否指定长度 HOT 1
- BUG:作为属性的类的属性如果都使用了 @MagicConverter 将会导致反序列结果不正确 HOT 5
- BUG:子类继承父类的时候序列化结果为空数组 HOT 1
- 新版本没有发布到**仓库 HOT 1
- 框架序列化的数据能否自动补齐 HOT 8
- 嵌套 custom convert 的 rootObj 不是最顶层 HOT 3
- 功能增强:反序列化方法可以传入一个 customConvert 变量
- 二进制数据长度小于一个 MagicClass 所有属性的长度的时候,数据读取完了还会继续反序列化 HOT 2
- 序列化过程中能否获取到前面已经序列化好的数据? HOT 2
- 有没有什么序列化的同时分片的设计思路? HOT 9
- MConverter 的 pack 方法的优化建议 HOT 2
- unpack 返回的 buffer 能否 flip 之后再返回?
- 通过文件方式声明数据格式
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magic-byte.