Created
March 3, 2022 07:05
-
-
Save misTrasteos/36b23b12c625019c4c10a68174979e62 to your computer and use it in GitHub Desktop.
Twitter Serial example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//JAVA 17 | |
//DEPS com.twitter.serial:serial:0.1.6 | |
/// https://github.com/twitter/Serial | |
import java.io.IOException; | |
import com.twitter.serial.serializer.CoreSerializers; | |
import com.twitter.serial.serializer.ObjectSerializer; | |
import com.twitter.serial.serializer.SerializationContext; | |
import com.twitter.serial.stream.Serial; | |
import com.twitter.serial.stream.SerializerInput; | |
import com.twitter.serial.stream.SerializerOutput; | |
import com.twitter.serial.stream.bytebuffer.ByteBufferSerial; | |
public class TwitterSerialExample { | |
static record Patient (String name, int age ) { | |
@Override | |
public String toString() { | |
return name + "," + age; | |
} | |
}//Patient | |
static class PatientSerializer extends ObjectSerializer<Patient> { | |
private PatientSerializer(){} | |
private static PatientSerializer instance = null; | |
static PatientSerializer getInstance() { | |
if( instance == null) | |
instance = new PatientSerializer(); | |
return instance; | |
} | |
@Override | |
protected void serializeObject(SerializationContext context, SerializerOutput output, Patient patient) throws IOException { | |
output.writeObjectStart(0, "Patient"); | |
output.writeString( patient.name() ); | |
output.writeInt( patient.age() ); | |
output.writeObjectEnd(); | |
} | |
@Override | |
protected Patient deserializeObject(SerializationContext context, SerializerInput input, int versionNumber) throws IOException, ClassNotFoundException { | |
System.out.println(versionNumber); | |
int version = input.readObjectStart(); | |
String name = input.readNotNullString(); | |
int age = input.readInt(); | |
input.readObjectEnd(); | |
return new Patient(name, age); | |
} | |
}//PatientSerializer | |
public static void main(String[] args) throws Exception { | |
// ---------- serialize a String using CoreSerializers | |
String text = "Visit Burgos"; | |
final Serial serial = new ByteBufferSerial(); | |
final byte[] serializedText = serial.toByteArray(text, CoreSerializers.STRING); | |
System.out.println( serializedText ); | |
// ---------- deserialize a String using CoreSerializers | |
final String t = serial.fromByteArray(serializedText, CoreSerializers.STRING); | |
System.out.println( t ); | |
// ---------- serialize an Object | |
Patient patient = new Patient("Art Vandelay", 38); | |
final byte[] serializedObject = serial.toByteArray(patient, PatientSerializer.getInstance()); | |
System.out.println( serializedObject ); | |
// ---------- deserialize an Object | |
Patient p = serial.fromByteArray(serializedObject, PatientSerializer.getInstance()); | |
System.out.println( p ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment