https://github.com/aswinmurali-io/osum_serializable
Serialize almost everything you ever need! 📦 Supports serializing MaterialColor, Color, Size, Locale, IconData, UuidValue, DateTime, Directory, File, Duration, and many more.
https://github.com/aswinmurali-io/osum_serializable
dart flutter json library package serialization
Last synced: 8 months ago
JSON representation
Serialize almost everything you ever need! 📦 Supports serializing MaterialColor, Color, Size, Locale, IconData, UuidValue, DateTime, Directory, File, Duration, and many more.
- Host: GitHub
- URL: https://github.com/aswinmurali-io/osum_serializable
- Owner: aswinmurali-io
- License: apache-2.0
- Created: 2022-09-19T14:32:20.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-21T02:29:40.000Z (almost 4 years ago)
- Last Synced: 2025-10-23T01:58:33.072Z (8 months ago)
- Topics: dart, flutter, json, library, package, serialization
- Language: Dart
- Homepage: https://pub.dev/packages/osum_serializable
- Size: 45.9 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# osum_serializable
[](https://github.com/aswinmurali-io/osum_serializable/actions/workflows/osum_serializable.yaml)
The goal is to serialize almost everything you ever need! `json_serializable` is an amazing package to serialize classes but cannot serialize complex types. This package provides convertors for these types. Check the supported types to know more.
## Supported types
|Type|Support|Type|Support|
|---|---|---|---|
|`Color`|✅|`MaterialColor`|✅|
|`Offset`|✅|`Size`|✅|
|`Directory`|✅|`File`|✅|
|`DateTime`|✅|`IconData`|✅|
|`Duration`|✅|`UuidValue`|✅|
|`Locale` |✅|
## How it works
|Code|Json|
|----|----|
|
|
|
## How to use?
### Install
`osum_serializable` depends on `build_runner` and `json_serializable` to building the `toJson` and `fromJson` implementations. To install run these commands in your terminal.
```bash
flutter pub add --dev build_runner
flutter pub add --dev json_serializable
flutter pub add osum_serializable
```
### Usage
To use the convertors, simply postfix the data type name with `Convertor` and call the annotation.
```dart
@{DATATYPE}Converter()
final {DATATYPE} variable;
```
For example, serializing `IconData` can be done like this.
```dart
@IconDataConverter()
final IconData variable;
```
### Example
Refer the example section for the full code.
```dart
// imports ...
part 'test.g.dart';
@JsonSerializable()
class Example {
const Example(this.directory, this.file, this.color, this.duration, this.materialColor);
factory Example.fromJson(Json json) => _$ExampleFromJson(json);
@DirectoryConverter() // <- Convertors provided by osum_serializable!
final Directory directory;
@MaterialColorConverter()
final MaterialColor materialColor;
@FileConverter()
final File file;
@ColorConverter()
final Color color;
@DurationConverter()
final Duration duration;
Json toJson() => _$ExampleToJson(this);
}
```
Finally, don't forget to call the `build_runner` to generate the json implementation.
```bash
flutter pub run build_runner build
```