Provides null-safety implementation to simplify JSON data handling by adding extension method to JSON object

Related tags

Utilities lazy_json
Overview

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 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 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);
You might also like...

AsyncCallQueue is a Dart class which provides a queuing mechanism to prevent concurrent access to asynchronous code.

async_call_queue AsyncCallQueue is a Dart class which provides a queuing mechanism to prevent concurrent access to asynchronous code. Getting Started

Jan 18, 2022

Provides Dart Build System builder for creating Injection pattern using annotations.

Provides Dart Build System builder for creating Injection pattern using annotations. Gate generator The core package providing generators using annoat

Dec 20, 2022

Provides simple conversion between Dart classes and Protobuf / Fixnum classes used in gRPC.

grpc_protobuf_convert Provides simple conversion between Dart classes and Protobuf / Fixnum classes used in gRPC. Using the library Add the repo to yo

Nov 1, 2022

A flutter plugin that provides external storage path and external public storage path

ext_storage ext_storage is minimal flutter plugin that provides external storage path and external public storage path

Nov 16, 2021

A flutter package provides controllers and editors for complex models and lists

A flutter package provides controllers and editors for complex models and lists

This package provides controllers and editors for complex models and lists and is inspired by simplicity of TextEditingController. It encapsulates sta

Sep 1, 2022

A Gura parser implementation for Dart

Gura Dart parser This repository contains the implementation of a Gura configuration format parser for Dart, written in pure Dart. (Compliant with spe

Aug 2, 2021

Flutter implementation of Clean Architecture, inspired on the guidelines created by Uncle Bob.

clean_framework Flutter implementation of Clean Architecture, inspired on the guidelines created by Uncle Bob. see http://www.amazon.com/dp/0134494164

Dec 13, 2022

A pure Dart implementation of the Pusher Channels Client

pusher_channels is a pure Dart pusher channels client. This client is work in progress and it is unstable. Usage A simple usage example: import 'packa

Nov 6, 2022

Reference implementation for the Zenon SDK for Dart and Flutter apps compatible

Zenon Dart SDK Reference implementation for the Zenon SDK for Dart and Flutter apps compatible with the Zenon Alphanet - Network of Momentum Phase 0.

Nov 23, 2022
Owner
Kinnara Digital Studio
Kinnara Digital Studio
A JMAP client library in Dart to make JMAP method calls and process the responses

JMAP Dart client A JMAP client library to make JMAP method calls and process the responses. We most notably use it to write the TMail Flutter applicat

LINAGORA 18 Dec 19, 2022
The Puzzle Cell Server Handling System

The Puzzle Cell Server Handling System This is the repository for the official server runtime How to setup? You can install the Dart SDK, download the

null 7 Nov 18, 2022
Collection of object-oriented Dart primitives

Dartoos — Dart Object-Oriented Software This package is a collection of object-oriented Dart primitives that implement classic data structures, algori

Dartoos 6 Nov 6, 2022
The `TypedEventNotifier` library allows notifying listeners with an object.

The TypedEventNotifier library allows notifying listeners with an object. listeners can be subscribed to only a special type or group of objects.

Evgeniy Ilyin 0 Nov 13, 2021
Extension functions on ValueListenable that allows you to work with them almost as if it was a synchronous stream.

functional_listener Extension functions on ValueListenable that allows you to work with them almost as if it was a synchronous stream. Each extension

null 54 Oct 9, 2022
Parse and compose Magnet URI extension (BEP53) ranges.

bep53-range Parse and compose Magnet URI extension (BEP53) ranges. Dart port of https://github.com/webtorrent/bep53-range Usage parse Parse Magnet URI

Chiziaruhoma Ogbonda 2 Feb 7, 2022
An extension to the Flutter SDK for building Flutter applications for Tizen devices.

Flutter for Tizen An extension to the Flutter SDK for building Flutter applications for Tizen devices. Flutter and the related logo are trademarks of

null 356 Dec 16, 2022
A flutter application , that create dynamic forms from json data

checktyper A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if th

DotCoder 1 Aug 23, 2022
Converts SVG icons to OTF font and generates Flutter-compatible class. Provides an API and a CLI tool.

Fontify The Fontify package provides an easy way to convert SVG icons to OpenType font and generate Flutter-compatible class that contains identifiers

Igor Kharakhordin 88 Oct 28, 2022
Provides API to generate Dart source code

DartWriter DartWriter provides API to generate Dart source code. It can make your job easier while developing flutter/dart tools. You can also generat

Ahmet ÇELİK 11 Oct 24, 2022