Bug in std.json or my problem
Craig Dillabaugh
craig.dillabaugh at gmail.com
Wed Apr 22 17:48:18 UTC 2020
So perhaps I am the only person in the world using std.json, but
I was wondering
if the following code should work.
=====================================================================
import std.json;
import std.conv;
import std.stdio;
struct Person {
string name;
float income;
this (string name, float income) {
this.name = name;
this.income = income;
}
this(JSONValue js) {
this.name = to!string(js["name"]);
/* This next line crashes with .. JSONValue is not
floating type.
* to!float( js["income"].toString()) works.
*/
this.income = js["income"].floating;
}
JSONValue toJSON() {
JSONValue json;
json["name"] = JSONValue(this.name);
json["income"] = JSONValue(this.income);
return json;
}
}
int main(string[] argv) {
Person bob = Person("Bob", 0.0);
string bob_json = bob.toJSON().toString();
Person sonofbob = Person(parseJSON(bob_json));
writeln(sonofbob.toJSON().toPrettyString());
return 0;
}
=======================================================================
The crash is caused because the 'income' field with value 0.0 is
output as 0 (rather than 0.0) and when it is read this is
interpreted
as an integer.
Shouldn't this work?
More information about the Digitalmars-d-learn
mailing list