GithubHelp home page GithubHelp logo

edsonbonfim / json_serializer Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 39 KB

Automatically convert JSON to dart objects in runtime without code generation.

Home Page: https://pub.dev/packages/json_serializer

License: BSD 3-Clause "New" or "Revised" License

Dart 100.00%
dart flutter json json-serializer serializer

json_serializer's Introduction

JSON Serializer

A versatile Dart package for effortless JSON serialization and deserialization without the need for code generation or reflection.


Getting Started

To get started, simply import the json_serializer package:

import 'package:json_serializer/json_serializer.dart';

JSON Deserialization

Suppose you have the following Dart classes:

enum Gender { male, female }

class Person {
  final String name;
  final Gender gender;
  final Address address;

  Person({required this.name, required this.gender, required this.address});
}

class Address {
  final String street;
  final String city;

  Address({required this.street, required this.city});
}

You can deserialize JSON data into these classes as follows:

main() {
  // Define user-defined types for successful deserialization
  JsonSerializer.options = JsonSerializerOptions(types: [
    UserType<Person>(Person.new),
    UserType<Address>(Address.new),
    EnumType<Gender>(Gender.values),
  ]);

  var json =
      '{"name":"John","gender":"male","address":{"street":"123 Main St","city":"Sampletown"}}';

  var person = deserialize<Person>(json);

  print('Name: ${person.name}');
  print('Gender: ${person.gender.name}');
  print('Street: ${person.address.street}');
  print('City: ${person.address.city}');
}

Note that you should use JsonSerializerOptions to register all of your referenced classes or enums.

Serialization

To serialize, all your referenced classes should implement the Serializable interface, which requires an implementation of toMap, a simple map of key and value properties.

class Person implements Serializable {
  final String name;
  final Gender gender;
  final Address address;

  Person({required this.name, required this.gender, required this.address});

  @override
  Map<String, dynamic> toMap() {
    return {'name': name, 'gender': gender, 'address': address};
  }
}

class Address implements Serializable {
  final String street;
  final String city;

  Address({required this.street, required this.city});

  @override
  Map<String, dynamic> toMap() {
    return {'street': street, 'city': city};
  }
}

You can then easily serialize objects:

print(serialize(person));
// {"name":"John","gender":"male","address":{"street":"123 Main St","city":"Sampletown"}}

License

This library is licensed under the BSD 3-Clause License. Feel free to use it and contribute to its development.


Made with ❤️ by Edson Bonfim

json_serializer's People

Contributors

edsonbonfim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.