Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 22, 2020 Bug in std.json or my problem | ||||
---|---|---|---|---|
| ||||
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? |
April 22, 2020 Re: Bug in std.json or my problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | On Wednesday, 22 April 2020 at 17:48:18 UTC, Craig Dillabaugh wrote:
> 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?
Yes, it's just buggy.
Giving it a value of an even 1.0 will make it throw the same exception (js["income"].type is JSONType.integer), but a value of 1.1 will make it pass (.type properly becomes JSONType.float_).
I don't know of a solution other than to check js["income"].type beforehand and use .floating or .integer as befits, or simply use an alternative JSON library (asdf?).
File an issue if you have the time, maybe it will get attention. Unreported bugs can only be fixed by accident.
|
April 22, 2020 Re: Bug in std.json or my problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | On Wednesday, 22 April 2020 at 18:23:48 UTC, Anonymouse wrote:
> On Wednesday, 22 April 2020 at 17:48:18 UTC, Craig Dillabaugh wrote:
>> 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?
>
> Yes, it's just buggy.
>
> Giving it a value of an even 1.0 will make it throw the same exception (js["income"].type is JSONType.integer), but a value of 1.1 will make it pass (.type properly becomes JSONType.float_).
>
> I don't know of a solution other than to check js["income"].type beforehand and use .floating or .integer as befits, or simply use an alternative JSON library (asdf?).
>
> File an issue if you have the time, maybe it will get attention. Unreported bugs can only be fixed by accident.
I thought it might be worth filing a bug, but wanted to confirm if others thought this was actually a bug. I had encountered an identical issue with vibe-d years ago.
Thanks for the feedback.
|
April 22, 2020 Re: Bug in std.json or my problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to CraigDillabaugh | On Wednesday, 22 April 2020 at 18:35:49 UTC, CraigDillabaugh wrote: > On Wednesday, 22 April 2020 at 18:23:48 UTC, Anonymouse wrote: >> On Wednesday, 22 April 2020 at 17:48:18 UTC, Craig Dillabaugh wrote: clip >> >> File an issue if you have the time, maybe it will get attention. Unreported bugs can only be fixed by accident. > > I thought it might be worth filing a bug, but wanted to confirm if others thought this was actually a bug. I had encountered an identical issue with vibe-d years ago. > > Thanks for the feedback. After some digging it appears there is already a fix for this (though a bit old) … maybe I am the only person who cares about std.json after all :o) https://github.com/dlang/phobos/pull/5005/commits/e7d8fb83d2510b252cd8cfd2b744310de6fa84e5 |
Copyright © 1999-2021 by the D Language Foundation