lazy json
provides null-safety implementation to simplify json data handling by adding extension method to json object and json array.
getting started
add dependency in pubspec.yml
dependencies:
lazy_json: ^1.0.2
usage
import library to your .dart file:
import 'package:lazy_json/lazy_json.dart';
supported data types
string
string
type has default value ” (empty string)
usage:
// on json object, if 'mykey' not found it returns ''
final mystring = myjsonobject.string('mykey');
// on json object, if 'mykey' not found it returns 'hello' as default value
final mystring = myjsonobject.string('mykey', 'hello');
// on json array, retrieve string value at index 0
final mystring = myjsonarray.string(0);
// on json array, retrieve string value at index 3, returns 'hello world' as default value
final mystring = myjsonarray.string(3, 'hello world');
integer
int
type has default value 0 (zero)
usage:
// on json object, if 'mykey' not found it returns 0
final myinteger = myjsonobject.integer('mykey');
// on json object, if 'mykey' not found it returns 99 as default value
final myinteger = myjsonobject.integer('mykey', 99);
// on json array, retrieve integer value at index 0
final myinteger = myjsonarray.integer(0);
// on json array, retrieve integer value at index 3, returns 100 as default value
final myinteger = myjsonarray.integer(3, 100);
float / double
double
type has default value 0.0 (zero)
usage:
// on json object, if 'mykey' not found it returns 0.0
final myfloat = myjsonobject.float('mykey');
// on json object, if 'mykey' not found it returns 99.9 as default value
final myfloat = myjsonobject.float('mykey', 99.9);
// on json array, retrieve float/double value at index 0
final myfloat = myjsonarray.float(0);
// on json array, retrieve float/double value at index 3, returns 100.89 as default value
final myfloat = myjsonarray.float(3, 100.89);
boolean
bool
type has default value false
usage:
// on json object, if 'mykey' not found it returns false
final myboolean = myjsonobject.boolean('mykey');
// on json object, if 'mykey' not found it returns true as default value
final myboolean = myjsonobject.boolean('mykey', true);
// on json array, retrieve boolean value at index 0
final myboolean = myjsonarray.boolean(0);
// on json array, retrieve double value at index 3, returns true as default value
final myboolean = myjsonarray.boolean(3, true);
json object
map<string, dynamic>
type has default value {}
(empty object)
usage:
// on json object, if 'mykey' not found it returns empty object
final myobject = myjsonobject.integer('mykey');
// on json object, if 'mykey' not found it returns json object {'a' : 10}
final myobject = myjsonobject.integer('mykey', {'a' : 10});
// on json array, retrieve json object at index 0
final myobject = myjsonarray.integer(0);
// on json array, retrieve json object at index 3, returns {'b' : 'hello world'} as default value
final myobject = myjsonarray.integer(3, {'b' : 'hello world'});
json array
list<dynamic>
type has default value []
(empty array)
usage:
// on json array, if 'mykey' not found it returns empty array
final myarray = myjsonobject.array('mykey');
// on json object, if 'mykey' not found it returns json array ['a', 'b']
final myarray = myjsonobject.array('mykey', ['a', 'b']);
// on json array, retrieve json array at index 0
final myarray = myjsonarray.array(0);
// on json array, retrieve json array at index 3, returns [100, 200, {'b' : 'hello'}] as default value
final myarray = myjsonarray.array(3, [100, 200, {'b' : 'hello'}]);
shorthands methods
even lazier, all shorthand methods are basically regular methods with first letter only
myjsonobject.s('mykey'); // shorthand for myjsonobject.string('mykey');
myjsonobject.i('mykey'); // shorthand for myjsonobject.integer('mykey');
myjsonobject.f('mykey'); // shorthand for myjsonobject.float('mykey');
myjsonobject.b('mykey'); // shorthand for myjsonobject.boolean('mykey');
myjsonobject.o('mykey'); // shorthand for myjsonobject.object('mykey');
myjsonobject.a('mykey'); // shorthand for myjsonobject.array('mykey');
myjsonarray.s(1); // shorthand for myjsonarray.string(1);
myjsonarray.i(2); // shorthand for myjsonarray.integer(2);
myjsonarray.f(3); // shorthand for myjsonarray.float(3);
myjsonarray.b(4); // shorthand for myjsonarray.boolean(4);
myjsonarray.o(5); // shorthand for myjsonarray.object(5);
myjsonarray.a(6); // shorthand for myjsonarray.array(6);
Comments are closed.