gpt4 book ai didi

json - 将具有双重成员的对象序列化为 JSON 时的堆栈溢出

转载 作者:行者123 更新时间:2023-12-03 02:51:16 26 4
gpt4 key购买 nike

尝试运行此代码以使用 dartson 序列化 JSON 中的对象时,在成员中使用 double 值时会生成 Stack Overflow 异常。
我怎么解决这个问题?

 import 'package:dartson/dartson.dart';


@MirrorsUsed(targets:const['example'], override:'*')
import 'dart:mirrors';

@Entity()
class GeoFence {
String label;
num latitude;
num longitude;
num radius;
}


main(List<String> args) {
var dson = new Dartson.JSON();

GeoFence object = new GeoFence()
..label = "test"
..latitude = 46.2
..longitude = 12
..radius = 10;


String jsonString = dson.encode(object);
print(jsonString);
}

最佳答案

我尝试了您的示例,看起来“num”定义的属性中使用的 double 数存在问题。请试试:

import 'package:dartson/dartson.dart';

@Entity()
class GeoFence {
String label;
double latitude;
double longitude;
double radius;
}


main(List<String> args) {
var dson = new Dartson.JSON();

GeoFence object = new GeoFence()
..label = "test"
..latitude = 46.2
..longitude = 12
..radius = 10;


String jsonString = dson.encode(object);
print(jsonString);
}

另请注意,使用 dartson 时不再需要 @MirrorUsed 注释。我将在 github 上更新问题并尽快查看。

关于json - 将具有双重成员的对象序列化为 JSON 时的堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31894113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com