Json class abstract
This abstract class provides a simple, intuitive, and safe way to handle JSON.
This class provides a constructor that generates a Json instance from a JSON string, a JSON map, and a byte representation of the JSON string. You can use the Json.fromString constructor for JSON strings, the Json.fromMap constructor for JSON maps, and the Json.fromBytes constructor for byte representations of JSON strings.
It also provides methods to easily and safely retrieve values from JSON objects. For example, if you want to retrieve a string associated with a specific key, use the getString method. If this key does not exist or if the value is null, a non-null value set as the default value in each method will be returned. If you want to set a specific default value, you can set the default value as an argument of each method.
If the values associated with a particular key are structured as a list, you can use getStringValues for example. These methods will retrieve all the values in the list associated with the specified key and return them as a list.
You can use use getJson if the value associated with a particular key is a JSON object represented by a map. The type returned by this method is Json, the same as this class. Even if the value associated with this particular key is a JSON object expressed as a string, no special procedure is required, and you can simply call the getJson method.
Also you can use getJsonList if the values associated with a particular key are multiple JSON objects. This method recursively traverses all JSON objects associated with the key, so if there are nested JSON objects associated with this particular key, it is okay. This method will return a list of Json classes.
Example:
void main() {
// It provides constructors to get JSON from JSON string, JSON map, and JSON bytes.
final jsonFromString = Json.fromString(value: '{"test": "something"}');
final jsonFromMap = Json.fromMap(value: {'test': 'something'});
final jsonFromBytes = Json.fromBytes(
bytes: Uint8List.fromList('{"test": "something"}'.codeUnits));
// You can use handful methods in the same interface once instance is created.
print(jsonFromString.getString(key: 'test'));
print(jsonFromMap.getString(key: 'test'));
print(jsonFromBytes.getString(key: 'test'));
final testJson = Json.fromMap(
value: {
'testValueList': ['value1', 'value2'],
'testJsonString': '{"key1": "value2"}',
'testJsonList': [
{
'key1': 'value1',
'key2': 'value2',
}
],
'testRecursiveJsonList': [
[
{
'key1': 'value1',
'key2': 'value2',
}
],
{
'key3': 'value3',
'key4': 'value4',
}
]
},
);
if (testJson.isEmpty) {
// Do something when json is empty.
return;
}
// It provides features to safely get values from JSON.
print(testJson.getStringValues(key: 'testValueList'));
// You can easily get a JSON object or JSON list associated with a key.
// If the JSON object associated with the key is a string,
// it will be automatically detected and parsed into a JSON object.
print(testJson.getJson(key: 'testJsonString'));
print(testJson.getJsonList(key: 'testJsonList'));
// If your JSON list is nested, that's okay!
// All JSON expressions associated with a key will be returned as JSON objects.
print(testJson.getJsonList(key: 'testRecursiveJsonList'));
}
Constructors
- Json.fromBytes({required Uint8List bytes})
-
Returns the new instance of Json from bytes.
factory
-
Json.fromMap({required Map<
String, dynamic> value}) -
Returns the new instance of Json from json map.
factory
- Json.fromString({required String value})
-
Returns the new instance of Json from json string.
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isEmpty → bool
-
Returns true if this json object is empty, otherwise false.
no setter
- isNotEmpty → bool
-
Returns true if this json object is not empty, otherwise false.
no setter
-
keySet
→ Set<
String> -
Returns the key set of this json object.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
containsKey(
{required String key}) → bool -
Returns true if json contains key linked to
key
passed as an argument. -
getBool(
{required String key, bool defaultValue = false}) → bool -
Returns the bool value linked to the
key
, otherwisedefaultValue
. -
getDouble(
{required String key, double defaultValue = -1.0}) → double -
Returns the double value linked to the
key
, otherwisedefaultValue
. -
getDoubleValues(
{required String key}) → List< double> -
Returns the double value list linked to the
key
, otherwise empty list. -
getInt(
{required String key, int defaultValue = -1}) → int -
Returns the int value linked to the
key
, otherwisedefaultValue
. -
getIntValues(
{required String key}) → List< int> -
Returns the int value list linked to the
key
, otherwise empty list. -
getJson(
{required String key}) → Json -
Returns the child json linked to the
key
, otherwise empty json object. -
getJsonList(
{required String key}) → List< Json> -
Returns the child json list linked to the
key
, otherwise empty json list. -
getString(
{required String key, String defaultValue = ''}) → String -
Returns the string value linked to the
key
, otherwisedefaultValue
. -
getStringValues(
{required String key}) → List< String> -
Returns the string value list linked to the
key
, otherwise empty list. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited