Code Monkey home page Code Monkey logo

Comments (11)

7even avatar 7even commented on July 25, 2024

Sorry, it also doesn't seem to work.

require 'faraday'
require 'crack/json'

response = Faraday.get('https://api.vkontakte.ru/method/groups.getById?gids=23201%2C23202%2C23203&fields=country%2Ccity')
Crack::JSON.parse(response.body)
# => Psych::SyntaxError: (<unknown>): control characters are not allowed at line 1 column 1

Or am I doing something wrong?

from vkontakte_api.

nemilya avatar nemilya commented on July 25, 2024

this works for me:

# gem list --local
# crack 0.3.1
# faraday 0.8.1

require 'faraday'
require 'crack/json'
require 'openssl/ssl'

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
response = Faraday.get('https://api.vkontakte.ru/method/groups.getById?gids=23201%2C23202%2C23203&fields=country%2Ccity')
p Crack::JSON.parse(response.body)

from vkontakte_api.

nemilya avatar nemilya commented on July 25, 2024

I realize, that the problem is because of YAML impelementation.

If you noted in sources - 'crack/json' - just convert json to yaml format and then parse yaml.
We need to find right yaml parser )

from vkontakte_api.

7even avatar 7even commented on July 25, 2024

I'm afraid it creates more questions than answers.

  1. How to make crack use some YAML parser instead of psych shipped with MRI 1.9?
  2. How to allow people to use oj for production (to go fast) and crack for development (to use windows for some reason) without depending on them both in gemspec? I really don't like the idea to make someone manually install other gems for vkontakte_api to work. It would be ok to have a default slow option working everywhere and a fast option installed separately, but crack doesn't work correctly on 1.9 because of the aforementioned YAML problems.
  3. Parsing JSON with crack requires a separate faraday middleware. I wrote one for oj and it was dead simple, but still.

I personally think the best solution would be to report a bug about installing on windows to oj developers.

from vkontakte_api.

nemilya avatar nemilya commented on July 25, 2024

I agree with you. I'm on 1.8.7 now.

In my vision - the best solution was - have in vkontakte_api inner json parser - slow but plain ruby,
and make check for fast gem installed - and use it for fast parse.

There is plain ruby json parsers, and OJ - will not support win32 I think, and they will be right.

What is the problem of VK's json? Bytes in String? f.e. %01 - if it espace to '\001' - plain ruby json parse it ok.

from vkontakte_api.

7even avatar 7even commented on July 25, 2024

The problem here is with this symbol: \x03. You can get the JSON here, but neither browser nor curl won't show the character - you need to get it in irb or pry or by a script.

bug

And this is not the first time I see broken JSON from VKontakte, it is at least the third time.

from vkontakte_api.

nemilya avatar nemilya commented on July 25, 2024

BTW, this fix works:

require 'faraday'
require 'json'
require 'openssl/ssl'

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
response = Faraday.get('https://api.vkontakte.ru/method/groups.getById?gids=23201%2C23202%2C23203&fields=country%2Ccity')
body = response.body.gsub(/\003/, '\u0003')
p JSON.parse(body)

from vkontakte_api.

nemilya avatar nemilya commented on July 25, 2024

In VK documentations described \001 byte in JSON response, f.e. "Uploading Audio":

http://vk.com/pages?f=%D0%97%D0%B0%D0%B3%D1%80%D1%83%D0%B7%D0%BA%D0%B0%2B%D0%B0%D1%83%D0%B4%D0%B8%D0%BE%D0%B7%D0%B0%D0%BF%D0%B8%D1%81%D0%B5%D0%B9&oid=-1&p=%D0%9F%D1%80%D0%BE%D1%86%D0%B5%D1%81%D1%81_%D0%B7%D0%B0%D0%B3%D1%80%D1%83%D0%B7%D0%BA%D0%B8_%D1%84%D0%B0%D0%B9%D0%BB%D0%BE%D0%B2_%D0%BD%D0%B0_%D1%81%D0%B5%D1%80%D0%B2%D0%B5%D1%80_%D0%92%D0%9A%D0%BE%D0%BD%D1%82%D0%B0%D0%BA%D1%82%D0%B5

In example of JSON response:
{"server": "1234", "audio": "audio%011300", "hash": "2c75c7e511c5ed39e03cec7fae5a143a"}

note: "%01" in "audio" value.

from vkontakte_api.

targence avatar targence commented on July 25, 2024

@7even насколько я понимаю OJ сейчас лучший гем для парсинга JSON.

Но вот на что я наткнулся. Интересные тесты скорости и сравнение с OJ.

https://github.com/msgpack/msgpack-ruby
https://gist.github.com/4371201

from vkontakte_api.

7even avatar 7even commented on July 25, 2024

Да, похожее сравнение есть и в README проекта Oj, но msgpack - это все-таки отдельный формат, несовместимый с JSON - а при работе с ВКонтакте API нужно парсить именно JSON.

from vkontakte_api.

targence avatar targence commented on July 25, 2024

@7even ага, понял, я думал они все таки совместимы, ну ok, спасибо за коммент. =)

from vkontakte_api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.