Skip to main content

Command Palette

Search for a command to run...

(Teleporting Human, Understanding serialization and De serialization in Javascript) The Mission: Stellar exchange

Updated
3 min read
(Teleporting Human, Understanding serialization and De serialization in Javascript) The Mission: Stellar exchange

Introduction

In the year 2405, the Starship Enterprise, under the command of Captain Jean-Luc Picard, embarks on a critical mission to establish peaceful relations with the mysterious alien civilization known as the Xelthorians. The Xelthorians possess advanced technology that could revolutionize Starfleet’s understanding of the cosmos. However, their language and data formats are unlike anything the Federation has encountered before.

To facilitate communication and data exchange, Captain Picard and his crew must serialize their data to send it across subspace communication channels and deserialize the data they receive from the Xelthorians.

Serialization: Preparing Data for Subspace Transmission.

Ensign Wesley Crusher, the young and brilliant Starfleet officer, is tasked with preparing the crew's data for transmission. He gathers crucial information about the Enterprise's crew and uses JavaScript to serialize it. This process converts the data into a string format, making it easy to send over subspace communication.

// Data to be serialized
const starFleetOfficer = {
name: "Jean-luc Picard",
rank: "Captain",
ship: "USS Enterprise",
species: "Human",
}
// Serialize the object 
const serializedData = JSON.stringify(starFleetOfficer)
console.log(serializedData);
// the output will be
// {"name":"Jean-luc Picard","rank":"Captain","ship":"USS Enterprise","species":"Human"}

Lieutenant Commander Data, the android officer renowned for his computational prowess, receives a transmission from the Xelthorians. The data arrives in a serialized format, a jumble of characters that only a machine could understand. Using JavaScript, Data deserializes the alien data, converting it back into a readable object.

Deserialization: Deciphering the data

Now Deserialization is a process to convert the JSON string into a Javascript object again, and it is done by using JSON.parse.

const deserializedData = JSON.parse(serializedData);
console.log(deserializedData);
// the output will be again 
/*1111
{
  name: 'Jean-luc Picard',
  rank: 'Captain',
  ship: 'USS Enterprise',
  species: 'Human'
}
*/

Here is how it works in a flow diagram

The exchange of bridging worlds

The bridge of the Enterprise buzzes with activity as Wesley and Data work together. The crew’s data, now serialized, is transmitted to the Xelthorians. Moments later, Data deciphers the incoming alien data.

// Crew data to be sent
const crewData = [
    { name: "William Riker", rank: "Commander", ship: "USS Enterprise", species: "Human" },
    { name: "Data", rank: "Lieutenant Commander", ship: "USS Enterprise", species: "Android" }
];

// Serialize the crew data
const serializedCrewData = JSON.stringify(crewData);
console.log("Sending data:", serializedCrewData);
// Output: Sending data: [{"name":"William Riker","rank":"Commander","ship":"USS Enterprise","species":"Human"},{"name":"Data","rank":"Lieutenant Commander","ship":"USS Enterprise","species":"Android"}]

// Data received from the alien ship
const receivedData = '[{"name":"Spock","rank":"Commander","ship":"USS Enterprise","species":"Vulcan"},{"name":"Uhura","rank":"Lieutenant","ship":"USS Enterprise","species":"Human"}]';

// Deserialize the received data
const deserializedAlienData = JSON.parse(receivedData);
console.log("Received data:", deserializedAlienData);
// Output: Received data: [ { name: 'Spock', rank: 'Commander', ship: 'USS Enterprise', species: 'Vulcan' }, { name: 'Uhura', rank: 'Lieutenant', ship: 'USS Enterprise', species: 'Human' } ]

Conclusion

Thanks to the combined efforts of Wesley Crusher and Lieutenant Commander Data, the Enterprise successfully establishes a data exchange with the Xelthorians. This achievement marks a significant step towards understanding and cooperation between two civilizations, bringing the galaxy closer to a future of peace and harmony.

With serialization and deserialization, the crew of the Enterprise bridges the gap between worlds, proving once again that in the vastness of space, knowledge and collaboration are the keys to unlocking new frontiers.

And so, the mission continues, boldly going where no one has gone before... 🖖😄