rails to_json quoting decimal values - ruby-on-rails

(update)
An array of geocoordinates, built from a collection of records
[{"point_lon"=>0.1307336132e3, "point_lat"=>-0.252978933e2, "title"=>"kata tjuta"},
{"point_lon"=>0.154984876e3, "point_lat"=>-0.17e2, "title"=>"error case"},
{"point_lon"=>0.1310369614747e3, "point_lat"=>-0.253455545e2, "title"=>"uluru"}]
has proper quoting structure, but for JSON input to a javascript needs the rockets to be replaced by a colon.
Transforming the array via JSON.generate or to_json unfortunately leads to quoting of the decimal values and being ignored by the javascript
[{"point_lon":"130.7336132","point_lat":"-25.2978933","point_name":"kata tjuta"},
{"point_lon":"154.984876","point_lat":"-17.0","point_name":"error case"},
{"point_lon":"131.0369614747","point_lat":"-25.3455545","point_name":"uluru"}]
How can this array be transformed without quoting decimals?

This is because you use decimal numbers instead of float, so rails quotes the strings to preserve the precision. You can find methods to avoid this in this related question: Rails JSON Serialization of Decimal adds Quotes

Related

How do I keep my rails integer from being converted to binary?

As you may be able to see in the image, I have a User model and #user.zip is stored as an integer for validation purposes (ie, so only digits are stored, etc.). I was troubleshooting an error when I discovered that my sample zip code (00100) was automatically being converted to binary, and ending up as the number 64.
Any ideas on how to keep this from happening? I am new to Rails, and it took me a few hours to figure out the cause of this error, as you might imagine :)
I can't imagine any other information would be helpful here, but please inform me if otherwise.
This is not binary, this is octal.
In Ruby, any number starting with 0 will be treated as an octal number. You should check the Ruby number literals to learn more about this, here's a quote:
You can use a special prefix to write numbers in decimal, hexadecimal, octal or binary formats. For decimal numbers use a prefix of 0d, for hexadecimal numbers use a prefix of 0x, for octal numbers use a prefix of 0 or 0o, for binary numbers use a prefix of 0b. The alphabetic component of the number is not case-sensitive.
For your case, you should not store zipcodes as numbers. Not only in the database, but even as variables don't treat them as numeric values. Instead, store and treat them as strings.
The zip should probably be stored as a string since you can't have a valid integer with leading zeroes.

Convert double 15,34 to 15.34

I'm trying to get a json string but I noticed that when I pass some double numbers to my query the integer part is separated by a coma from the floating part. I need them to be separated by a point. Is this connected to the Language of the os?
You can use a simple regex to fix this issue:
\d+,\d+ to select the offending bits of JSON, and a string-based replace on the results. If you already have the numbers separated out, use the replace function (something like val.replace(",",".")) on the value you have, and cast it to a float (float(val) in Python).

Float in Ruby on Rails Json

I faced a big problem: I have an API method, that returns JSON.
One of fields has property float and I need it to be 5.0. But when I convert it to JSON it becomes 5.
Even if I make this thing render :json => 5.to_f it returns me integer anyway.
What do I need to do to have 5.0 in JSON response?
Many Thanks
All numbers are floats in Javascript. Numbers are "double-precision 64-bit format IEEE 754 values".
see ECMAScript 2015 Language Specification.
What it means, is that, 5.0 and 5 are the same in JS. There is no differentiation of int, float or double.
As JSON (which stand for JavaScript Object Notation) generally end up as a JS object, 5 represents a float. Since JSON format is mostly used for data transfert/communication purpose, the decision to omit the the decimal part must be to save a few bytes in the string that is passed around.
In #z.shan's answer, the 5.0 is passed as a string "5.0".
Depending on what you want, I would recommend to keep the 5 as a number in the JSON payload.
All numbers are floats in Javascript. Numbers are "double-precision 64-bit format IEEE 754 values".
if you want to do then you should have to do like this
when we render json there is no floating points >0 that's why it is showing as integer you can convert it to string value to do this like
render json: 5.to_f.to_s
only if integer value is in object field then it will show as float
Or you can use conversion like that :
integer = 5
render json: ('%.2f'%integer)

convert hash value into string in objective c

I have a hash value and I want to convert it into string formate but I do not know how to do that.
Here is the hash value
7616db6c232292d2e56a2de9da49ea810d5bb80d53c10e7b07d9521dc88b3177
Hashing technique is a one way algorithm. So that is not possible, sorry.
You can't. A hash is a one-way function. Plus, two strings could theoretically generate the same hash.
BUT. If you had a large enough, pre-computed list of as many hashes as you could generate for each unique string, you could potentially convert your hash back into a string. Although it would require thousands of GB in entries and a lot of time to compute each and every hash. So it practically isn't possible.

Yaml gotcha's, are there any issues with storing certain types of characters in Yaml?

I'm very new to yaml, and I just want to know what I can and can't store character wise in yaml?
What are the escape characters for double quotes etc?
Can I span multiple lines?
Basically, you can store everything. Quotes aren't an issue, you can type text out without quotes (and for non-printable characters that you can't incorporate casually, there are the usual escape sequences). That means purely numerical text is considered a number, though - but then again, you can add quotes or an explicit type annotation (and I assume most libraries do that when necessary), e.g. !!str 100. Also, if you want to include the comment sign (#), you have to add quotes.
Another issue is that some strings may look like more complex YAML (e.g. certain uses of exclamation signs look like casts and certain uses of colons look like singleton associative tables). You can avoid these by using "multi-line" strings that just consist of a single line. Multi-line strings exist and come in two flavors, preserving linebreaks (--- |) and ignoring newlines except for blank lines (--- >, much like markdown).

Resources