isar / isar/hive

Create JsonSerializable type Adapter.

Open
#1,097 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Create new abstract type adapter that can be used with json Serializable functions fromJson toJson

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

Creating new type adapters for classes that are already jsonSerializable would bo as simple as extending the abstract type

class MyModelAdapter extends JsonSerializableAdapter{
@override
final typeId = 16;
}

Implementation sudocode I didn't test it yet.

import 'dart:convert';
import 'package:hive/hive.dart';

abstract class JsonSerializer{
T fromJson(Map json);
Map toJson(T obj);
}

abstract class JsonSerializableAdapter> extends TypeAdapter {

@override
T read(BinaryReader reader) {
final json = reader.readString();
return (T as JsonSerializer).fromJson(jsonDecode(json));
}

@override
void write(BinaryWriter writer, T obj) {
writer.writeString( jsonEncode( (T as JsonSerializer).toJson(obj) ) );
}
}
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Version**
- Platform: iOS, Android, Mac, Windows, Linux, Web
- Flutter version: [e.g. 1.5.4]
- Hive version: [e.g. 0.5.0]

Contributor guide

No contributing guide indexed for this repository

Research direction

No repository files or tests are named. Start by reviewing Hive's TypeAdapter, BinaryReader, and BinaryWriter entry points alongside dart:convert, then determine how a generic adapter could use fromJson and toJson; done means a working adapter design with coverage for reading and writing serializable models.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.