An interface that represents an object that can be encoded to JSON.
The JsonEncodable interface is used to represent objects that can be
encoded to JSON. The interface defines a toJson method that returns a
JSON-encoded string representation of the object.
The T type parameter is used to specify the type of the object that is
being encoded to JSON.
Example usage:
import 'package:sealed_languages/sealed_languages.dart';
class Person implements JsonEncodable<Person> {
const Person({
required this.name,
required this.age,
});
final String name;
final int age;
@override
String toJson({JsonCodec codec = const JsonCodec()}) =>
codec.encode({'name': name, 'age': age});
}
void main() {
const person = Person(name: 'John', age: 30);
assert(person.toJson().contains('John'));
}
In this example, the Person class implements the JsonEncodable
interface with a Person type parameter. The class defines properties for
the person's name and age, and implements the toJson method to encode the
object to JSON.
- Implementers
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toJson(
{JsonCodec codec = const JsonCodec()}) → String - Returns a JSON-encoded string representation of the object.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited