Re: Create an empty json object with std.json

2016-01-22 Thread Borislav Kosharov via Digitalmars-d-learn

On Friday, 22 January 2016 at 11:53:11 UTC, Andrea Fontana wrote:

If you declare a JSONValue like this:

JSONValue json;

then:

assert(json.type() == JSON_TYPE.NULL);

Documentation at 
https://dlang.org/phobos/std_json.html#.JSONValue.type.2 
suggests not to change type but to assign a new value instead.


My problem is: how can I assign an empty object like {}?

The only way i found is using that deprecated method:
json.type = JSON_TYPE.OBJECT;

or

json = `{}`.parseJSON;

Please notice that to init as array this works:
json = JSONValue[].init;


What you can do is use the JSONValue ctor and pass it an empty 
associative array


auto json = JSONValue(string[string].init); //or 
JSONValue((JSONValue[string]).init)


It's a little verbose but gets the job done without using 
deprecated things or parseJSON


Re: core.time Duration how to get units in double/float format?

2016-01-19 Thread Borislav Kosharov via Digitalmars-d-learn

On Tuesday, 19 January 2016 at 15:25:58 UTC, wobbles wrote:
On Tuesday, 19 January 2016 at 14:07:50 UTC, Borislav Kosharov 
wrote:
On Monday, 18 January 2016 at 12:46:31 UTC, Jonathan M Davis 
wrote:

[...]


I want to use float time in a game where I call the update 
method passing the delta time as float seconds. It's more easy 
to multiply the dt with a speed constant meaning how much the 
object will move after 1 second. Such float delta time is used 
in many game engines and it's somewhat standart way of doing 
it. Also the method you wrote returns a string and I need a 
float to multiply it with a number.

Thanks for the reply anyway


Checkout out how DSFML handles this.
You simply pass around a Clock object that you can restart 
every frame using


clock.restart();

You then call your update function with 
update(Clock.getElapsedTime());
Then in each objects update(Time t) method you just get the 
time in whatever unit you want. Works pretty well.


http://www.dsfml.com/


Yes that's exactly what I'm using. But they changed it to return 
a Duration instead of Time and that broke my code. See the source 
https://github.com/Jebbs/DSFML/blob/master/src/dsfml/system/clock.d#L65


Re: core.time Duration how to get units in double/float format?

2016-01-19 Thread Borislav Kosharov via Digitalmars-d-learn
On Monday, 18 January 2016 at 12:46:31 UTC, Jonathan M Davis 
wrote:
In general, using floating point values with time is an 
incredibly bad idea. It can certainly make sense when printing 
stuff out, but using it in calculations is just asking for 
trouble given all of the unnecessary imprecision that it adds. 
So, Duration does not directly support floating point values at 
all, and that's very much on purpose. I'd strongly argue that 
the fact that TickDuration does was a mistake.


So, if you're doing floating point calculations with time, I'd 
strongly urge you to rethink your code. And if you're just 
trying to print out the a duration as a floating point value, 
because it's nice to view that way, then that's fine, but 
you'll need to do the conversion yourself. And it's not that 
hard. It just isn't handed to you directly, because aside from 
printing, code really shouldn't be using floating point values 
for time. e.g.


string toFloatingSeconds(Duration d)
{
import std.conv;
enum hnsecsPerSecond = convert!("seconds", "hnsecs")(1);
auto s = d.split!("seconds", "hnsecs")();
return to!string(s.seconds + cast(real)(s.hnsecs) / 
hnsecsPerSecond);

}

And yes, that's a bit more of a pain than using to!("seconds", 
float) with TickDuration, but it's also the sort of thing that 
most code really shouldn't be doing. So, adding that 
functionality to Duration would just encourage folks to write 
buggy code.


- Jonathan M Davis


I want to use float time in a game where I call the update method 
passing the delta time as float seconds. It's more easy to 
multiply the dt with a speed constant meaning how much the object 
will move after 1 second. Such float delta time is used in many 
game engines and it's somewhat standart way of doing it. Also the 
method you wrote returns a string and I need a float to multiply 
it with a number.

Thanks for the reply anyway


Re: core.time Duration how to get units in double/float format?

2016-01-17 Thread Borislav Kosharov via Digitalmars-d-learn

On Sunday, 17 January 2016 at 18:57:13 UTC, biozic wrote:
On Sunday, 17 January 2016 at 14:43:26 UTC, Borislav Kosharov 
wrote:
Seeing that TickDuration is being deprecated and that I should 
use Duration instead, I faced a problem. I need to get total 
seconds like a float. Using .total!"seconds" returns a long 
and if the duration is less than 1 second I get 0. My question 
is whats the right way to do it. Because I saw that 
TickDuration has a to!("seconds", float) method, but Duration 
doesn't have one. I can convert Duration to TickDuration and 
call to but seeing that its deprecated makes me think there is 
a better way.


Why not just use a smaller granularity for Duration.total and 
convert the result?


duration.total!"nsecs" / cast(float) 1e9


Yea I could do that, but its not intuitive to get a total of one 
magnitude to just convert it to another. My question is why 
doesn't `to!` accept Duration.




core.time Duration how to get units in double/float format?

2016-01-17 Thread Borislav Kosharov via Digitalmars-d-learn
Seeing that TickDuration is being deprecated and that I should 
use Duration instead, I faced a problem. I need to get total 
seconds like a float. Using .total!"seconds" returns a long and 
if the duration is less than 1 second I get 0. My question is 
whats the right way to do it. Because I saw that TickDuration has 
a to!("seconds", float) method, but Duration doesn't have one. I 
can convert Duration to TickDuration and call to but seeing that 
its deprecated makes me think there is a better way.


How to split a string/array with multiple separators?

2015-12-16 Thread Borislav Kosharov via Digitalmars-d-learn
I want to split a string using multiple separators. In std.array 
the split function has a version where it takes a range as a 
separator, but it works differently than what I want. Say if I 
call it with " -> " it will search for the whole thing together. 
I want to pass split a list of separators say [":", ",", ";"] and 
if it finds any of those to split it.


Sorry if this questions is stupid but I cant find how to do it.


Re: How to check if JSONValue of type object has a key?

2015-10-08 Thread Borislav Kosharov via Digitalmars-d-learn

Thanks guys that was I was looking for!


How to check if JSONValue of type object has a key?

2015-10-06 Thread Borislav Kosharov via Digitalmars-d-learn
I'm using std.json for parsing json. I need to check if a 
specific string key is in JSONValue.object. The first thing I 
tried was:


JSONValue root = parseJSON(text);
if(root["key"].isNull == false) {
//do stuff with root["key"]
}

But that code doesn't work, because calling root["key"] will 
throw an exception of key not fount if "key" isn't there.


I need some method `root.contains("key")` method to check if the 
object has a key.


Thanks in advance!