Friends
I have a simple Dart class that cannot be encoded into JSON.
The output of the following code prints out to the console
flutter: Converting object to an encodable object failed: Instance of 'TestJsonConversion'
class TestJsonConversion {
String testString = "123245abcde";
int testIneger = 1234;
}
void main() {
var testJsonConversion = TestJsonConversion();
try {
var testString = jsonEncode(testJsonConversion);
// ignore: avoid_print
print(testString);
}catch(e){
// ignore: avoid_print
print(e.toString());
}
runApp(const MyApp());
}
This is the default application generated by Visual Studio with just these lines added.
You cannot encode an instance of a user class with the built-in jsonEncode. These are things you can encode by default: "a number, boolean, string, null, list or a map with string keys". For this class to encode, you'd have to define a .toJson method on it, and I don't see one there.
The class has no constructors and tojson . Try this
class TestJsonConversion {
final String testString;
final int testInteger;
TestJsonConversion(this.testString, this.testInteger);
TestJsonConversion.fromJson(Map<String, dynamic> json)
: testString = json['trstString'],
testInteger = json['testInteger'];
Map<String, dynamic> toJson() => {
'testString': testString,
'testInteger': testInteger,
};
}
And when you create an instance
var testJsonConversion = TestJsonConversion(testString: 'abc', testInteger: 123);
print(json.encode(testJsonConversion.toJson());
Related
im new to Dart-flutter.
i have watching a tutorial video from udemy course writing in dart pad
so i also wrote in dartpad. but it showing error in it.
this is the code wrote in udemy..
import 'dart:convert';
void main(){
var rawJson = '{"url": "https://helo.com","id": 2}';
var parsedJson = json.decode(rawJson);
var imageModel = new ImageModel.fromJson(parsedJson);
print(imageModel.url);
}
class ImageModel{
int id;
String url;
ImageModel.fromJson(parsedJson) {
id = parsedJson['id'];
url = parsedJson['url'];
}
ImageModel(this.id, this.url);
}
in that video it runs, but for me it shows error as
Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:15:3:
Error: This constructor should initialize field 'id' because its type 'int' doesn't allow null.
ImageModel.fromJson(parsedJson) {
^
lib/main.dart:12:7:
Info: 'id' is defined here.
int id;
^^
lib/main.dart:15:3:
Error: This constructor should initialize field 'url' because its type 'String' doesn't allow null.
ImageModel.fromJson(parsedJson) {
^
lib/main.dart:13:10:
Info: 'url' is defined here.
String url;
^^^
Error: Compilation failed.
i have no idea what the problem is..
can you guys help me to troubleshoot the error
Since you're compiling with null safety, you may change to this:
int? id;
String? url;
For further info about null safety, please refer to this link https://dart.dev/null-safety
Try using null safe code:
eg:
int? id;
String? url;
You will try like this
import 'dart:convert';
class ImageModel{
final int id;
final String url;
ImageModel({required this.id, required this.url});
factory ImageModel.fromJson(Map<String, dynamic> parsedJson) {
return ImageModel(
id: parsedJson['id'] as int,
url: parsedJson['url'] as String,
);
}
}
void main() {
var rawJson = '{"url": "https://helo.com","id": 2}';
var parsedJson = json.decode(rawJson);
print(parsedJson['url']);
print(parsedJson['id']);
}
You are probably watching tutorial that was recorded before null-safety introduction in Dart (null-safety is a Dart feature that helps prevent NullRefference exceptions in runtime).
You can either go with nullable values (like #Sowat Kheang suggested in another answer) for id and url in ImageModel class (change String to String? and int to int?). Then you will be fine, but it defeats the purpose of null-safety.
Or you can change your code so it will handle null values in proper way. Code will look something like this:
import 'dart:convert';
void main(){
var rawJson = '{"url": "https://helo.com","id": 2}';
var parsedJson = json.decode(rawJson);
var imageModel = ImageModel.fromJson(parsedJson);
print(imageModel.url);
}
class ImageModel{
// Make your fields 'final' if you don't plan to change them later
// this will make your life easier and is a good practice.
final int id;
final String url;
// we add curly braces to make arguments named
// and add 'required' keyword to indicate that constructor can't be called
// without actually passing arguments into it
ImageModel({required this.id, required this.url});
// here we use factory constructor that will call our default constructor
// it's also a good practice to specify type of a variable if you know it (Map<String, dynamic> in this case)
factory ImageModel.fromJson(Map<String, dynamic> parsedJson) {
// since we don't know for sure if 'parsedJson' has key 'id'
// we would add '??' — null checking operator.
// So if parsedJson['id'] is null, value -1 will be passed
// to the constructor. Same with 'parsedJson['url']'.
return ImageModel(
id: parsedJson['id'] ?? -1,
url: parsedJson['url'] ?? '');
}
}
var rawJson = '{"url": "https://helo.com","id": 2}';
Actually it is a Sting Json and Encoding is not perform properly So follow the step..
Map data={"url": "https://helo.com","id": 2};
Encode that data. json.encode(data);
Now the Encoding is done then you can send the data any server if you have..
When you get back data then... json.decode(rawData)
Then parsing work properly
I want to encode my Map to a json. It looks like this:
Map<MyEnum, int> map = {type: limit};
Where MyEnum is an enum. A simple json.encode(map) won't work as it does not know how to serialize the enum class I guess.
The error message is:
Unhandled Exception: Converting object to an encodable object failed: _LinkedHashMap len:1
How can I manage to serialize this map to a json?
you can use describeEnum method inside foundation.dart
This is really not a solution I would recommend but I ended up doing it mostly for "fun". I don't guarantee anything about the solution besides the fact that it is horrible. :)
The problem is that enum is not defined as a valid type for Json so the whole concept does give us some problems. One solution is to translate enum values into String with the name of the enum first, and then the name of value like MyFirstEnum.first1. This representation is what Dart gives you if calling toString() on a enum value.
This is fine but for safety we could also add a magic string in the beginning like DART_ENUM:MyFirstEnum.first1 so it is easier to recognize between other strings which could have the same name as the enum value without being an enum.
Next is type safety. In Json, we know that all maps has String as the type of keys. By making our own representation of enum and allowing it to also be keys, we cannot expect a decoder to return e.g. Map<String, dynamic> but must return Map<dynamic, dynamic>.
With that said, here is my attempt to build a Json decoder and encoder which handles enum values. It also works for enum keys in maps:
import 'dart:convert';
class JsonConverterWithEnumSupport {
final String magicString;
final Set<Object> allEnumValues = {};
final Map<String, Object> enumStringToEnumValue = {};
JsonConverterWithEnumSupport(List<List<Object>>? enumsValues,
{this.magicString = "DART_ENUM:"}) {
enumsValues?.forEach(addEnumValues);
}
void addEnumValues(List<Object> enumValues) {
for (final enumValue in enumValues) {
enumStringToEnumValue[enumValue.toString()] = enumValue;
allEnumValues.add(enumValue);
}
}
String _addMagic(dynamic enumValue) => '$magicString$enumValue';
String _removeMagic(String string) => string.substring(magicString.length);
String encode(Object? value) =>
json.encode(value, toEncodable: (dynamic object) {
if (object is Map) {
return object.map<dynamic, dynamic>((dynamic key, dynamic value) =>
MapEntry<dynamic, dynamic>(
allEnumValues.contains(key) ? _addMagic(key) : key,
allEnumValues.contains(value) ? _addMagic(value) : value));
}
if (object is List) {
return object.map<dynamic>(
(dynamic e) => allEnumValues.contains(e) ? _addMagic(e) : e);
}
if (allEnumValues.contains(object)) {
return _addMagic(object);
}
return object;
});
dynamic decode(String source) => json.decode(source, reviver: (key, value) {
if (value is String && value.startsWith(magicString)) {
return enumStringToEnumValue[_removeMagic(value)];
}
if (value is Map) {
return value.map<dynamic, dynamic>((dynamic key, dynamic value) =>
MapEntry<dynamic, dynamic>(
(key is String) && key.startsWith(magicString)
? enumStringToEnumValue[_removeMagic(key)]
: key,
value));
}
return value;
});
}
enum MyFirstEnum { first1, first2 }
enum MySecondEnum { second1, second2 }
void main() {
final converter =
JsonConverterWithEnumSupport([MyFirstEnum.values, MySecondEnum.values]);
final jsonString = converter.encode({
MyFirstEnum.first1: [MySecondEnum.second2, MySecondEnum.second1],
'test': {MyFirstEnum.first2: 5}
});
print(jsonString);
// {"DART_ENUM:MyFirstEnum.first1":["DART_ENUM:MySecondEnum.second2","DART_ENUM:MySecondEnum.second1"],"test":{"DART_ENUM:MyFirstEnum.first2":5}}
print(converter.decode(jsonString));
// {MyFirstEnum.first1: [MySecondEnum.second2, MySecondEnum.second1], test: {MyFirstEnum.first2: 5}}
}
You will need to feed into JsonConverterWithEnumSupport all the possible enum values there is possible (see the main method in the bottom for example).
If you don't want the magic string to be appended on each enum you can just create JsonConverterWithEnumSupport with: magicString: '' as parameter.
You could create an extension on your enum to convert it to a String then convert your map to a Map<String, int> so it will be encoded correctly:
import 'dart:convert';
enum MyEnum { type }
extension MyEnumModifier on MyEnum {
String get string => this.toString().split('.').last;
}
void main() {
Map<MyEnum, int> map = {MyEnum.type: 10};
Map<String, int> newMap = {};
map.forEach((key, value) =>
newMap[key.string] = value);
final json = jsonEncode(newMap);
print(json);
}
Output
{"type":10}
you will need a function to convert string to enum:
T enumFromString<T>(List<T> values, String value) {
return values.firstWhere((v) => v.toString().split('.')[1] == value, orElse: () => null);
}
your enum is
enum MyEnum {type, destype};
suppose it is used as map inside a class to serialize and deserialize:
class MyClass {
Map<MyEnum, int> myProperty = {type: 1};
// serialize
Map<String, dynamic> toJson() {
return {
'myProperty': myProperty.map((key, value) => MapEntry(key.name, value)),
}
// deserialize
MyClass.fromJson(Map<String, dynamic> json) {
myProperty=
json['myProperty'].map((k, v) => MapEntry(enumFromString<MyEnum>(MyEnum.values, k), v)).cast<MyEnum, int>();
}
}
Firstly define enum and value extension
enum OrderState { pending, filled, triggered, cancelled }
extension OrderStateExt on OrderState {
String get value {
switch (this) {
case OrderState.pending:
return "PENDING";
case OrderState.filled:
return "FILLED";
case OrderState.triggered:
return "TRIGGERED";
case OrderState.cancelled:
return "CANCELLED";
}
}
}
model class
class OrderRequest {
OrderState state;
OrderRequest({required this.state});
Map<String, dynamic> toMap() {
return {
'state': state.value,
};
}
}
String toJson() => json.encode(toMap());
factory OrderRequest.fromMap(Map<String, dynamic> map) {
return OrderRequest (
state: OrderState.values
.where((e) => e.value == map['state']).first
);
}
factory OrderRequest.fromJson(String source) =>
OrderRequest.fromMap(json.decode(source));
Aye Aye good people,
[edited:
running this in dartpad
import 'dart:convert';
void main() {
const String _json = '{"myListInt": [1]}';
final Map<String, dynamic> _map = jsonDecode(_json);
final List<int> _list = _map['myListInt'] as List<int>;
_list.forEach((i) {
String _s = i.toString();
print(_s);
});
}
returns
Uncaught exception:
CastError: Instance of 'JSArray': type 'JSArray' is not a subtype of type
'List<int>'
in case I use
final List<int> _list = List<int>.from(_map['myListInt'] as List<int>);
or
List<int>.generate(_map['myListInt'].length, (i)=>_map['myListInt'][i] as int);
returns
Uncaught exception:
Cannot read property 'length' of undefined
]
what am I doing wrong?
Thank you in advance
Francesco
Instead of this line
myListInt: List<int>.from(_map['myListInt'] as List<int>),
you can use
myListInt: List<int>.generate(_map['myListInt'].length, (i)=>_map['myListInt'][i] as int
Basically instead of casting the whole list, you have to cast each element one by one.
ok, using ""as Iterable"" works,
import 'dart:convert';
void main() {
const String _json = '{"myListInt": [1]}';
final Map<String, dynamic> _map = jsonDecode(_json);
final List<int> _list= List<int>.from(_map['myListInt'] as Iterable);
_list.forEach((i) {
String _s = i.toString();
print(_s);
});
}
I'm wondering how can I parse a nested json to a class with generic types. My intention is to wrap responses from the backend (like loginRespose that contains a token) with a code and a message
I have
class BaseResponse<T>{
int code;
String message;
T responseObject;
BaseResponse.fromJson(Map<String, dynamic> parsedJson)
: code = parsedJson['Code'],
message = parsedJson['Message'],
responseObject = T.fromJson(parsedJson['ResponseObject']); //This is what I'd like to do
}
Obviously the last line throws an error because T doesn't has a named constructor "fromJson".
I tried adding some restrictions to the Type but I didn't find any solutions. Do you have any idea on how to pull this off?
You can't do such thing, at least not in flutter. As dart:mirror is disabled and there's no interface for classes constructors.
You'll have to take a different route.
I'll recommend using POO instead. You would here give up on deserializing responseObject from your BaseResponse. And then have subclass of BaseResponse handles this deserialization
Typically you'd have one subclass per type:
class IntResponse extends BaseResponse<int> {
IntResponse.fromJson(Map<String, dynamic> json) : super._fromJson(json) {
this.responseObject = int.parse(json["Hello"]);
}
}
You can then hide this mess away by adding a custom factory constructor on BaseResponse to make it more convenient to use.
class BaseResponse<T> {
int code;
String message;
T responseObject;
BaseResponse._fromJson(Map<String, dynamic> parsedJson)
: code = parsedJson['Code'],
message = parsedJson['Message'];
factory BaseResponse.fromJson(Map<String, dynamic> json) {
if (T == int) {
return IntResponse.fromJson(json) as BaseResponse<T>;
}
throw UnimplementedError();
}
}
Then either instantiate the wanted type directly, or use the factory constructor :
final BaseResponse foo = BaseResponse.fromJson<int>({"Hello": "42", "Code": 42, "Message": "World"});
You can achieve this with the built_value package (you'll also need built_value_generator and build_runner). Your class will look something like this:
part 'base_response.g.dart';
abstract class BaseResponse<T> implements Built<BaseResponse<T>, BaseResponseBuilder<T>> {
int code;
String message;
T responseObject;
factory BaseResponse([updates(BaseResponseBuilder<T> b)]) = _$BaseResponse<T>;
static Serializer<BaseResponse> get serializer => _$baseResponseSerializer;
}
You will have to run flutter packages pub run build_runner build to make the generated file. Then you use it like this:
BaseResponse baseResponse = serializers.deserialize(
json.decode(response.body),
specifiedType: const FullType(BaseResponse, const [FullType(ConcreteTypeGoesHere)])
);
There's just one more bit of boilerplate you have to take care of. You need another file called serializers.dart. You need to manually add all the classes you want to deserialize here, and also an addBuilderFactory function for each class that takes a type parameter - and for each concrete type you want to use.
part 'serializers.g.dart';
#SerializersFor(const [
BaseResponse,
ConcreteTypeGoesHere,
])
final Serializers serializers = (_$serializers.toBuilder()
..addBuilderFactory(
FullType(BaseResponse, const [const FullType(ConcreteTypeGoesHere)]),
() => new BaseResponseBuilder<ConcreteTypeGoesHere>()
)
..addPlugin(StandardJsonPlugin()))
.build();
Then re-run flutter packages pub run build_runner build
Makes me wish for Gson... :S
Here is my approach:
class Wrapper<T, K> {
bool? isSuccess;
T? data;
Wrapper({
this.isSuccess,
this.data,
});
factory Wrapper.fromJson(Map<String, dynamic> json) => _$WrapperFromJson(json);
Map<String, dynamic> toJson() => _$WrapperToJson(this);
}
Wrapper<T, K> _$WrapperFromJson<T, K>(Map<String, dynamic> json) {
return Wrapper<T, K>(
isSuccess: json['isSuccess'] as bool?,
data: json['data'] == null ? null : Generic.fromJson<T, K>(json['data']),
);
}
class Generic {
/// If T is a List, K is the subtype of the list.
static T fromJson<T, K>(dynamic json) {
if (json is Iterable) {
return _fromJsonList<K>(json) as T;
} else if (T == LoginDetails) {
return LoginDetails.fromJson(json) as T;
} else if (T == UserDetails) {
return UserDetails.fromJson(json) as T;
} else if (T == Message) {
return Message.fromJson(json) as T;
} else if (T == bool || T == String || T == int || T == double) { // primitives
return json;
} else {
throw Exception("Unknown class");
}
}
static List<K> _fromJsonList<K>(List<dynamic> jsonList) {
return jsonList?.map<K>((dynamic json) => fromJson<K, void>(json))?.toList();
}
}
In order to add support for a new data model, simply add it to Generic.fromJson:
else if (T == NewDataModel) {
return NewDataModel.fromJson(json) as T;
}
This works with either generic objects:
Wrapper<Message, void>.fromJson(someJson)
Or lists of generic objects:
Wrapper<List<Message>, Message>.fromJson(someJson)
I have this code. Notice that the serialization is simply renaming the template_items property to template_items_attributes:
export class Template {
constructor(
) {}
public id: string
public account_id: string
public name: string
public title: string
public info: string
public template_items: Array<TemplateItem>
toJSON(): ITemplateSerialized {
return {
id: this.id,
account_id: this.account_id,
name: this.name,
title: this.title,
info: this.info,
template_items_attributes: this.template_items
}
}
}
export interface ITemplateSerialized {
id: string,
account_id: string,
name: string,
title: string,
info: string,
template_items_attributes: Array<TemplateItem>
}
Creating an object locally works fine and stringify calls the toJSON() method.
However, once I send that object to the API:
private newTemplate(name: string): Template {
let template = new Template();
template.name = name;
template.account_id = this._userService.user.account_id;
// next 5 lines are for testing that toJSON() is called on new obj
let item = new TemplateItem();
item.content = "Test"
template.template_items.push(item);
let result = JSON.stringify(template);
console.log('ready', result); // SHOWS the property changes
return template;
}
postTemplate(name: string): Observable<any> {
return this._authService.post('templates', JSON.stringify(this.newTemplate(name)))
.map((response) => {
return response.json();
});
}
It is saved and returned, but from that point on when I stringify and save again it does NOT call toJSON().
patchTemplate(template: Template): Observable<any> {
console.log('patching', JSON.stringify(template)); // DOES NOT CHANGE!
return this._authService.patch('templates' + `/${template.id}`, JSON.stringify(template))
.map((response) => {
return response.json();
});
}
Why does toJSON() only work on new objects?
In fact, your question has nothing to do with Angular or Typescript, it's just some JavaScript and the logic of how serialization work and why do we serialize objects.
I send that object to the API, save and return it
When you return an "object" from an API, you're returning a string which you parse as a JSON serialized object. Then you get a plain JavaScript object, not an instance of your class.
Object prototype in JavaScript does not have toJSON method, and even if it had, it's not the method you've written inside the Template class, so it won't be called.
You don't even need a server call to replicate this, just do
const obj = JSON.parse(JSON.stringify(new Template()))
obj.toJSON // undefined
And you'll see that obj is not an instance of Template. It's simply an object which simply happens to have all the fields as your original object made as a Template instance, but it's not an instance of that class.