belatuk_json_serializer

Creator: coderz1093

Last updated:

0 purchases

belatuk_json_serializer Image
belatuk_json_serializer Images

Languages

Categories

Add to Cart

Description:

belatuk json serializer

Belatuk JSON Serializer #




Replacement of package:json_god with breaking changes to support NNBD.
The new and improved definitive solution for JSON in Dart. It supports synchronously transform an object into a JSON string and also deserialize a JSON string back into an instance of any type.
Installation #
dependencies:
belatuk_json_serializer: ^7.1.0
copied to clipboard
Usage #
It is recommended to import the library under an alias, i.e., jsonSerializer.
import 'package:belatuk_json_serialization/belatuk_json_serialization.dart' as jsonSerializer;
copied to clipboard
Serializing JSON #
Simply call jsonSerializer.serialize(x) to synchronously transform an object into a JSON
string.
Map map = {"foo": "bar", "numbers": [1, 2, {"three": 4}]};

// Output: {"foo":"bar","numbers":[1,2,{"three":4]"}
String json = jsonSerializer.serialize(map);
print(json);
copied to clipboard
You can easily serialize classes, too. Belatuk JSON Serializer also supports classes as members.

class A {
String foo;
A(this.foo);
}

class B {
late String hello;
late A nested;
B(String hello, String foo) {
this.hello = hello;
this.nested = A(foo);
}
}

main() {
print(jsonSerializer.serialize( B("world", "bar")));
}

// Output: {"hello":"world","nested":{"foo":"bar"}}
copied to clipboard
If a class has a toJson method, it will be called instead.
Deserializing JSON #
Deserialization is equally easy, and is provided through jsonSerializer.deserialize.
Map map = jsonSerializer.deserialize('{"hello":"world"}');
int three = jsonSerializer.deserialize("3");
copied to clipboard
Deserializing to Classes #
Belatuk JSON Serializer lets you deserialize JSON into an instance of any type. Simply pass the type as the second argument to jsonSerializer.deserialize.
If the class has a fromJson constructor, it will be called instead.
class Child {
String foo;
}

class Parent {
String hello;
Child child = Child();
}

main() {
Parent parent = jsonSerializer.deserialize('{"hello":"world","child":{"foo":"bar"}}', Parent);
print(parent);
}
copied to clipboard
Any JSON-deserializable classes must initializable without parameters. If Foo() would throw an error, then you can't use Foo with JSON.
This allows for validation of a sort, as only fields you have declared will be accepted.
class HasAnInt { int theInt; }

HasAnInt invalid = jsonSerializer.deserialize('["some invalid input"]', HasAnInt);
// Throws an error
copied to clipboard
An exception will be thrown if validation fails.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.