简体   繁体   English

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

[英]Mongoid and UTF-8 issues in a JRuby on Rails app

I'm taking a JSON string that's the result from polling the Foursquare venue API: 我采用的是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
    }
}

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. 如您所知,其中有一些非标准字符,例如Cafés ,这打破了我在JRuby on Rails应用程序中基于Mongoid的模型。 When trying to to create an instance with MyModel.create, here's what I get. 当尝试使用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)

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? 我可以使我的Mongoid / MongoDB文档与UTF-8一起使用吗? do I need to "asciify" them somehow first if that's not possible? 如果那不可能,我是否需要先以某种方式“交往”他们?

Could be an encoding bug in JRuby's 1.9 mode. 可能是JRuby 1.9模式下的编码错误。 Does the same thing happen when you run it in 1.8 mode? 在1.8模式下运行时是否会发生相同的事情? Either way, a stacktrace should be filed as a bug at http://bugs.jruby.org . 无论哪种方式,都应将堆栈跟踪作为Bug提交到http://bugs.jruby.org Thanks! 谢谢!

gem install bson_ext might help. gem install bson_ext可能会有所帮助。

Source: MongoDB, Ruby and UTF-8 资料来源: MongoDB,Ruby和UTF-8

If you are using ubuntu, then you need to do some extra steps with spidermonkey/mongodb installation: 如果您使用的是ubuntu,则需要执行额外的步骤来安装spidermonkey / mongodb:

Most pre-built Javascript SpiderMonkey libraries do not have UTF-8 support compiled in; 大多数预构建的Javascript SpiderMonkey库没有编译UTF-8支持。 MongoDB requires this. MongoDB需要这个。

Source: Building for Linux 来源: Build for Linux

MongoDB and mongoid handle utf-8 properly. MongoDB和mongoid可以正确处理utf-8。 I was doing the same thing with the Foursquare API not long ago via the Quimby wrapper. 我不久前通过Quimby包装器使用Foursquare API进行了相同的操作。

As a result, I would suspect the bug is closely related to the use of JRuby. 结果,我怀疑该错误与JRuby的使用密切相关。

Have you set up JRuby to use UTF8? 您是否已设置JRuby以使用UTF8?

require 'jcode'
$KCODE = 'u'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM