JRuby on Rails 应用程序中的 Mongoid 和 UTF-8 问题

发布于 2024-12-20 23:10:49 字数 3780 浏览 1 评论 0原文

我正在获取一个 JSON 字符串,它是轮询 Foursquare 场地 API 的结果:

{
    "id"=>"4e404742c65b4ec27606deb4",
    "name"=>"Sarah's Cheesecake & Cafe",
    "contact"=>{
        "phone"=>"4134436678",
        "formattedPhone"=>"(413) 443-6678"
    },
    "location"=>{
        "address"=>"180 Elm St",
        "lat"=>42.44345873,
        "lng"=>-73.23804678,
        "distance"=>1063,
        "postalCode"=>"01201",
        "city"=>"Pittsfield",
        "state"=>"MA"
    },
    "categories"=>[
        {
            "id"=>"4bf58dd8d48988d16d941735",
            "name"=>"Café",
            "pluralName"=>"Cafés",
            "shortName"=>"Café",
            "icon"=>{
                "prefix"=>"https://foursquare.com/img/categories/food/cafe_",
                "sizes"=>[
                    32,
                    44,
                    64,
                    88,
                    256
                ],
                "name"=>".png"
            },
            "primary"=>true
        }
    ],
    "verified"=>false,
    "stats"=>{
        "checkinsCount"=>7,
        "usersCount"=>5,
        "tipCount"=>0
    },
    "hereNow"=>{
        "count"=>0
    }
}

如您所知,其中有一些非标准字符,例如 Cafés,这破坏了我基于 Mongoid 的模型JRuby on Rails 应用程序。当尝试使用 MyModel.create 创建实例时,这就是我得到的结果。

jruby-1.6.5 :012 > FoursquareVenue.create(hash)
Java::JavaLang::NullPointerException: 
    from org.jruby.exceptions.RaiseException.<init>(RaiseException.java:101)
    from org.jruby.Ruby.newRaiseException(Ruby.java:3348)
    from org.jruby.Ruby.newEncodingCompatibilityError(Ruby.java:3323)
    from org.jruby.RubyString.cat(RubyString.java:1285)
    from org.jruby.RubyString.cat19(RubyString.java:1221)
    from org.jruby.RubyHash$5.visit(RubyHash.java:727)
    from org.jruby.RubyHash.visitAll(RubyHash.java:594)
    from org.jruby.RubyHash.inspectHash(RubyHash.java:721)
    from org.jruby.RubyHash.inspect(RubyHash.java:745)
    from org.jruby.RubyHash$i$0$0$inspect.call(RubyHash$i$0$0$inspect.gen:65535)
    from org.jruby.RubyClass.finvoke(RubyClass.java:632)
    from org.jruby.javasupport.util.RuntimeHelpers.invoke(RuntimeHelpers.java:545)
    from org.jruby.RubyBasicObject.callMethod(RubyBasicObject.java:353)
    from org.jruby.RubyObject.inspect(RubyObject.java:408)
    from org.jruby.RubyArray.inspectAry(RubyArray.java:1483)
    from org.jruby.RubyArray.inspect(RubyArray.java:1509)
... 420 levels...
    from org.jruby.evaluator.ASTInterpreter.INTERPRET_METHOD(ASTInterpreter.java:75)
    from org.jruby.internal.runtime.methods.InterpretedMethod.call(InterpretedMethod.java:190)
    from org.jruby.internal.runtime.methods.DefaultMethod.call(DefaultMethod.java:179)
    from org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:312)
    from org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:169)
    from usr.local.rvm.rubies.jruby_minus_1_dot_6_dot_5.bin.jirb.__file__(/usr/local/rvm/rubies/jruby-1.6.5/bin/jirb:17)
    from usr.local.rvm.rubies.jruby_minus_1_dot_6_dot_5.bin.jirb.load(/usr/local/rvm/rubies/jruby-1.6.5/bin/jirb)
    from org.jruby.Ruby.runScript(Ruby.java:693)
    from org.jruby.Ruby.runScript(Ruby.java:686)
    from org.jruby.Ruby.runNormally(Ruby.java:593)
    from org.jruby.Ruby.runFromMain(Ruby.java:442)
    from org.jruby.Main.doRunFromMain(Main.java:321)
    from org.jruby.Main.internalRun(Main.java:241)
    from org.jruby.Main.run(Main.java:207)
    from org.jruby.Main.run(Main.java:191)
    from org.jruby.Main.main(Main.java:171)

如果我去掉所有奇怪的字符,一切都会按预期工作,并且不会引发异常。处理这个问题的正确方法是什么?我可以启用我的 Mongoid/MongoDB 文档以使用 UTF-8 吗?如果不可能的话,我需要先以某种方式将它们“asciify”吗?

I'm taking a JSON string that's the result from polling the Foursquare venue API:

{
    "id"=>"4e404742c65b4ec27606deb4",
    "name"=>"Sarah's Cheesecake & Cafe",
    "contact"=>{
        "phone"=>"4134436678",
        "formattedPhone"=>"(413) 443-6678"
    },
    "location"=>{
        "address"=>"180 Elm St",
        "lat"=>42.44345873,
        "lng"=>-73.23804678,
        "distance"=>1063,
        "postalCode"=>"01201",
        "city"=>"Pittsfield",
        "state"=>"MA"
    },
    "categories"=>[
        {
            "id"=>"4bf58dd8d48988d16d941735",
            "name"=>"Café",
            "pluralName"=>"Cafés",
            "shortName"=>"Café",
            "icon"=>{
                "prefix"=>"https://foursquare.com/img/categories/food/cafe_",
                "sizes"=>[
                    32,
                    44,
                    64,
                    88,
                    256
                ],
                "name"=>".png"
            },
            "primary"=>true
        }
    ],
    "verified"=>false,
    "stats"=>{
        "checkinsCount"=>7,
        "usersCount"=>5,
        "tipCount"=>0
    },
    "hereNow"=>{
        "count"=>0
    }
}

As you can tell, there are some non-standard characters in there such as Cafés and that's breaking my Mongoid based Model in this JRuby on Rails app. When trying to to create an instance with MyModel.create, here's what I get.

jruby-1.6.5 :012 > FoursquareVenue.create(hash)
Java::JavaLang::NullPointerException: 
    from org.jruby.exceptions.RaiseException.<init>(RaiseException.java:101)
    from org.jruby.Ruby.newRaiseException(Ruby.java:3348)
    from org.jruby.Ruby.newEncodingCompatibilityError(Ruby.java:3323)
    from org.jruby.RubyString.cat(RubyString.java:1285)
    from org.jruby.RubyString.cat19(RubyString.java:1221)
    from org.jruby.RubyHash$5.visit(RubyHash.java:727)
    from org.jruby.RubyHash.visitAll(RubyHash.java:594)
    from org.jruby.RubyHash.inspectHash(RubyHash.java:721)
    from org.jruby.RubyHash.inspect(RubyHash.java:745)
    from org.jruby.RubyHash$i$0$0$inspect.call(RubyHash$i$0$0$inspect.gen:65535)
    from org.jruby.RubyClass.finvoke(RubyClass.java:632)
    from org.jruby.javasupport.util.RuntimeHelpers.invoke(RuntimeHelpers.java:545)
    from org.jruby.RubyBasicObject.callMethod(RubyBasicObject.java:353)
    from org.jruby.RubyObject.inspect(RubyObject.java:408)
    from org.jruby.RubyArray.inspectAry(RubyArray.java:1483)
    from org.jruby.RubyArray.inspect(RubyArray.java:1509)
... 420 levels...
    from org.jruby.evaluator.ASTInterpreter.INTERPRET_METHOD(ASTInterpreter.java:75)
    from org.jruby.internal.runtime.methods.InterpretedMethod.call(InterpretedMethod.java:190)
    from org.jruby.internal.runtime.methods.DefaultMethod.call(DefaultMethod.java:179)
    from org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:312)
    from org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:169)
    from usr.local.rvm.rubies.jruby_minus_1_dot_6_dot_5.bin.jirb.__file__(/usr/local/rvm/rubies/jruby-1.6.5/bin/jirb:17)
    from usr.local.rvm.rubies.jruby_minus_1_dot_6_dot_5.bin.jirb.load(/usr/local/rvm/rubies/jruby-1.6.5/bin/jirb)
    from org.jruby.Ruby.runScript(Ruby.java:693)
    from org.jruby.Ruby.runScript(Ruby.java:686)
    from org.jruby.Ruby.runNormally(Ruby.java:593)
    from org.jruby.Ruby.runFromMain(Ruby.java:442)
    from org.jruby.Main.doRunFromMain(Main.java:321)
    from org.jruby.Main.internalRun(Main.java:241)
    from org.jruby.Main.run(Main.java:207)
    from org.jruby.Main.run(Main.java:191)
    from org.jruby.Main.main(Main.java:171)

If I strip out all the odd characters, everything works as expected and no exception is thrown. What's the proper way of handling this? Can I enabled my Mongoid/MongoDB documents to work with UTF-8? do I need to "asciify" them somehow first if that's not possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寻找一个思念的角度 2024-12-27 23:10:49

可能是 JRuby 1.9 模式中的编码错误。当你在 1.8 模式下运行它时,是否会发生同样的情况?无论哪种方式,堆栈跟踪都应作为错误提交到 http://bugs.jruby.org。谢谢!

Could be an encoding bug in JRuby's 1.9 mode. Does the same thing happen when you run it in 1.8 mode? Either way, a stacktrace should be filed as a bug at http://bugs.jruby.org. Thanks!

冰雪梦之恋 2024-12-27 23:10:49

gem install bson_ext 可能会有所帮助。

来源:MongoDB、Ruby 和 UTF-8

如果您使用ubuntu,那么你需要对 Spidermonkey/mongodb 安装执行一些额外的步骤:

大多数预构建的 Javascript SpiderMonkey 库没有 UTF-8
支持编译; MongoDB 需要这个。

来源:Linux 构建

gem install bson_ext might help.

Source: MongoDB, Ruby and UTF-8

If you are using ubuntu, then you need to do some extra steps with spidermonkey/mongodb installation:

Most pre-built Javascript SpiderMonkey libraries do not have UTF-8
support compiled in; MongoDB requires this.

Source: Building for Linux

音栖息无 2024-12-27 23:10:49

MongoDB 和 mongoid 正确处理 utf-8。不久前,我通过 Quimby 包装器使用 Foursquare API 做了同样的事情。

因此,我怀疑该错误与 JRuby 的使用密切相关。

MongoDB and mongoid handle utf-8 properly. I was doing the same thing with the Foursquare API not long ago via the Quimby wrapper.

As a result, I would suspect the bug is closely related to the use of JRuby.

北音执念 2024-12-27 23:10:49

您是否已将 JRuby 设置为使用 UTF8?

require 'jcode'
$KCODE = 'u'

Have you set up JRuby to use UTF8?

require 'jcode'
$KCODE = 'u'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文