# magic-byte
**Repository Path**: mtils/magic-byte
## Basic Information
- **Project Name**: magic-byte
- **Description**: 一种简单的方式将java对象转为字节数据,用于快速高效的自定义序列化/反序列化场景,类似C的Strcut结构体,多用于私有通讯协议实现。
- **Primary Language**: Java
- **License**: BSD-3-Clause
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 8
- **Forks**: 0
- **Created**: 2021-12-23
- **Last Updated**: 2025-10-17
## Categories & Tags
**Categories**: Uncategorized
**Tags**: Java, 自定义序列化, 私有协议, 对象转字节, 对象序列化
## README
### Magic-Byte
[](https://github.com/misterchangray/magic-byte)
[](https://github.com/misterchangray/magic-byte/issues)
[](https://github.com/misterchangray/magic-byte/issues?q=is%3Aissue+is%3Aclosed)
[](./LICENSE)
[中文](https://github.com/MisterChangRay/magic-byte/blob/master/README.md)|[English](https://github.com/MisterChangRay/magic-byte/blob/master/README.en.md)
#### 1. Introduction
In the contemporary IoT industry, due to privacy and security issues, many companies choose to use custom private binary protocols.
In the C language, thanks to the support of structures, it is very simple to convert objects and byte arrays; but in Java, without native support, developers can only rely on code power to read and parse the data and then translated into objects
.
This seemingly simple encoding/decoding process is actually accompanied by many headaches, such as:
- Handling of big and small endian/network byte order
- Processing of unsigned/signed numbers
- Multibyte integer conversion processing
- Conversion processing between ASCII code and bytes
- Handling of null pointers/padding data
- Handling of array objects/nested objects
The purpose of this project is to solve the above problems as much as possible so that everyone can focus more time on business.
After introducing `MagicByte`, you only need to use annotations to declare the data type of the field while defining the class.
Next, you only need to call two simple methods to serialize: `MagicByte.unpack();` for converting objects to bytes and `MagicByte.pack()` for converting bytes to objects.
Try it now!
#### 2. Quick Start:
1. Introduce Jar package;
2. `@MagicClass` performs global configuration on the current class
2. `@MagicField` annotates the JAVA object attributes that need to be converted, and supports object combination nesting. Note: inheritance is not supported.
3. Use `MagicByte.registerCMD` to register messages to MagicByte
4. Use `MagicByte.pack()` or `MagicByte.unpack()` to quickly serialize or deserialize data or objects
5. Supports the use of `@MagicConverter()` annotation to implement custom serialization; [Go to see the enumeration class custom serialization example](https://github.com/MisterChangRay/magic-byte/wiki/%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BA%8F%E5%88%97%E5%8C%96%E7%9A%84%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5)
Maven projects can be imported directly:
[Click to view the version list](https://mvnrepository.com/artifact/io.github.misterchangray/magic-byte)
```
io.github.misterchangraymagic-byte2.4.4
```
#### 3. Code example
The following is a simple framework function display. It is recommended to refer to [Best Practices for Data Entity Definition](https://github.com/MisterChangRay/magic-byte/wiki/%E6%95%B0%E6%8D%AE%E5%AE%9E%E4%BD%93%E5%AE%9A%E4%B9%89%E7%9A%84%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5)
In the following packet example, there are two data packets, Student and School:
```java
// declare class must use public
// Use big endian mode, the default is big endian
@MagicClass(byteOrder = ByteOrder.BIG_ENDIAN)
public class School {
// 10 byte, common data type, occupies 10 bytes in length
@MagicField(order = 1, size = 10)
private String name;
// 2 byte, length field, the actual data length will be automatically filled during data serialization
@MagicField(order = 3, calcLength = true)
private short length;
//Supports combination mode, Student object is embedded here
//Total number of bytes = students.bytes * length
@MagicField(order = 5, size = 2)
private Student[] students;
// 0 byte, note, this cannot be serialized, unsupported data types will be ignored
@MagicField(order = 7)
private List