7

I have learned that with hive database in flutter it is possible to store custom objects as json strings. And then there is the option to write a custom type adapter that stores the data in binary format.

So, why it could be needed to spend more time and implement a type adapter when developer can just store the custom object in json string format?

lxknvlk
  • 2,744
  • 1
  • 27
  • 32
  • For the average project, use JSON string, if the project needs TypeAdapter, you'll be more likely to know that (performance issues) – Ahmed I. Elsayed May 28 '23 at 16:33

2 Answers2

0

Although JSON string is easier to implement and requires less code, it is much slower than TypeAdapter because it takes some time to convert back and forth. Besides, JSON string cannot guarantee type safety, and it would lead to runtime error-prone while manipulating data and transforming processes. However, if the JSON string is just a standard key-value pair, JSON string should be considered. Last but not least, writing TypeAdapter is a preferred choice since it deals with a clean code design and can store complex data structures with a type-safe guarantee. Moreover, developers have more control over the data as they can see what is wrong in the implementation process.

Sowat Kheang
  • 668
  • 2
  • 4
  • 12
0

Using a TypeAdapter in Hive for storing custom objects in binary format has several advantages over storing objects as JSON strings:

  1. Efficiency: Storing objects in binary format using a TypeAdapter is more efficient in terms of both storage space and performance. Binary data takes up less space compared to JSON strings, which can be important if you have a large amount of data to store. Additionally, reading and writing binary data is generally faster than parsing and serializing JSON strings.

  2. Type Safety: With a TypeAdapter, Hive can ensure type safety when reading and writing objects. It can perform proper serialization and deserialization of object properties, handling different data types correctly. This helps prevent errors and inconsistencies when working with data.

  3. Flexibility: Using a TypeAdapter allows you to have more control over how your objects are serialized and deserialized. You can define custom serialization logic, handle complex data structures, and optimize the storage format for your specific use case. JSON serialization, on the other hand, has certain limitations and may not handle all object types or complex data structures efficiently.

  4. Backward Compatibility: If you need to make changes to your object structure, such as adding or removing properties, using a TypeAdapter allows you to maintain backward compatibility with existing data. You can handle migrations and transformations of old data to the new object structure, ensuring a smooth transition.

  5. Interoperability: Storing objects in binary format using a TypeAdapter allows for interoperability with other programming languages or systems that may not have built-in JSON support. Binary data can be more easily transferred or shared across different platforms and technologies.

While storing objects as JSON strings is a quick and simple approach, using a TypeAdapter provides better performance, type safety, flexibility, and compatibility. It is a recommended approach when working with Hive to store custom objects efficiently and effectively.