galileo_json_god

Last updated:

0 purchases

galileo_json_god Image
galileo_json_god Images
Add to Cart

Description:

galileo json god

galileo_json_god #
The new and improved definitive solution for JSON in Dart.
Installation #
dependencies:
galileo_json_god: any
copied to clipboard
Usage #
It is recommended to import the library under an alias, i.e., god.
import 'package:galileo_json_god/galileo_json_god.dart' as god;
copied to clipboard
Serializing JSON #
Simply call god.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 = god.serialize(map);
print(json);
copied to clipboard
You can easily serialize classes, too. JSON God also supports classes as members.
class A {
String foo;
A(this.foo);
}

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

main() {
God god = new God();
print(god.serialize(new 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 god.deserialize.
Map map = god.deserialize('{"hello":"world"}');
int three = god.deserialize("3");
copied to clipboard
Deserializing to Classes #
JSON God lets you deserialize JSON into an instance of any type. Simply pass the
type as the second argument to god.deserialize.
If the class has a fromJson constructor, it will be called instead.
class Child {
String foo;
}

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

main() {
God god = new God();
Parent parent = god.deserialize('{"hello":"world","child":{"foo":"bar"}}', Parent);
print(parent);
}
copied to clipboard
Any JSON-deserializable classes must initializable without parameters.
If new 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 = god.deserialize('["some invalid input"]', HasAnInt);
// Throws an error
copied to clipboard
An exception will be thrown if validation fails.
Thank you for using JSON God #
Thank you for using this library. I hope you like it.

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.