In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge of how to use Protostuff. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
First, introduce maven dependencies as follows:
Io.protostuff
Protostuff-core
1.4.4
Io.protostuff
Protostuff-runtime
1.4.4
After that, write the serialization tool Util:
Public class SerializeUtil {
Private static class SerializeData {
Private Object target
}
@ SuppressWarnings ("unchecked")
Public static byte [] serialize (Object object) {
SerializeData serializeData = new SerializeData ()
SerializeData.target = object
Class serializeDataClass = (Class) serializeData.getClass ()
LinkedBuffer linkedBuffer = LinkedBuffer.allocate (1024 * 4)
Try {
Schema schema = RuntimeSchema.getSchema (serializeDataClass)
Return ProtostuffIOUtil.toByteArray (serializeData, schema, linkedBuffer)
} catch (Exception e) {
Throw new IllegalStateException (e.getMessage (), e)
} finally {
LinkedBuffer.clear ()
}
}
@ SuppressWarnings ("unchecked")
Public static T deserialize (byte [] data, Class clazz) {
Try {
Schema schema = RuntimeSchema.getSchema (SerializeData.class)
SerializeData serializeData = schema.newMessage ()
ProtostuffIOUtil.mergeFrom (data, serializeData, schema)
Return (T) serializeData.target
} catch (Exception e) {
Throw new IllegalStateException (e.getMessage (), e)
}
}
}
What we need to pay attention to is the RuntimeSchema.getSchema code. By looking at the source code, we can see that a cache map has been placed to help us cache the generated content, so we don't need to cache it ourselves.
Since protostuff currently does not support serialization of objects such as list, you need to wrap it in plain POJO.
Finally, let's write a test:
Public static void main (String... Args) throws Exception {
User user = new User ()
User.setUserId (123456)
User.setAddress ("I am a good boy")
User.setNote ("this is test")
List list = new ArrayList ()
List.add ("record1")
List.add ("record2")
List.add ("record3")
User.setRecords (list)
Teacher teacher1 = new Teacher ()
Teacher1.setName ("Chinese teacher")
Teacher teacher2 = new Teacher ()
Teacher2.setName ("math teacher")
List teachers = new ArrayList ()
Teachers.add (teacher1)
Teachers.add (teacher2)
User.setTeachers (teachers)
Byte [] b = serialize (user)
User rst = deserialize (b, User.class)
System.out.println (JSON.toJSONString (rst))
}
Class User {
Private Integer userId
Private String address
Private String note
Private List records
Private List teachers
Public Integer getUserId () {
Return userId
}
Public void setUserId (Integer userId) {
This.userId = userId
}
Public String getAddress () {
Return address
}
Public void setAddress (String address) {
This.address = address
}
Public String getNote () {
Return note
}
Public void setNote (String note) {
This.note = note
}
Public List getRecords () {
Return records
}
Public void setRecords (List records) {
This.records = records
}
Public List getTeachers () {
Return teachers
}
Public void setTeachers (List teachers) {
This.teachers = teachers
}
}
Class Teacher {
Private String name
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
}
The final running result is as follows:
{"address": "I am a good boy", "note": "this is test", "records": ["record1", "record2", "record3"], "teachers": [{"name": "Chinese teacher"}, {"name": "math teacher"}], "userId": 123456}
These are all the contents of the article "how to use Protostuff". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.