• 登入
sketchucation logo sketchucation
  • 登入
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

JSON in Ruby

已排程 已置頂 已鎖定 已移動 Developers' Forum
48 貼文 7 Posters 7.0k 瀏覽 7 Watching
正在載入更多貼文
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • O 離線
    OricAtmos
    最後由 編輯 2012年11月28日 下午8:57

    @myhand said:

    Where did you get the library from?

    I don't remember where I got the Windows binaries from. Perhaps I still have a bookmark in my web browser at work, but right now I'm at home and can't have a look. But since I have access to our project repository from home I can offer you this:

    Link Preview Image
    Dropbox - 404

    favicon

    (dl.dropbox.com)

    1 條回覆 最後回覆 回覆 引用 0
    • M 離線
      Myhand
      最後由 編輯 2012年11月28日 下午10:12

      @oricatmos said:

      @myhand said:

      Where did you get the library from?

      I don't remember where I got the Windows binaries from. Perhaps I still have a bookmark in my web browser at work, but right now I'm at home and can't have a look. But since I have access to our project repository from home I can offer you this:

      Link Preview Image
      Dropbox - 404

      favicon

      (dl.dropbox.com)

      Thanks OricAtmos! Will give it a try.

      http://www.keepingmyhandin.com/

      1 條回覆 最後回覆 回覆 引用 0
      • O 離線
        OricAtmos
        最後由 編輯 2012年11月30日 下午4:00

        @myhand said:

        Thanks OricAtmos! Will give it a try.

        Did you get it to work? Unfortunately I've been unsuccessful in finding out where I got it from.

        1 條回覆 最後回覆 回覆 引用 0
        • M 離線
          Myhand
          最後由 編輯 2012年12月1日 上午12:25

          @oricatmos said:

          @myhand said:

          Thanks OricAtmos! Will give it a try.

          Did you get it to work? Unfortunately I've been unsuccessful in finding out where I got it from.

          Sorry, I got a bit tied up with trying to solve the MAC bug in my Material_Maintenance Plugin.

          Thank you, I have now got it to not throw an error, but my test code does not seem to serialise objects, so will need to read the library docs to see what I am doing wrong

          ` require 'yajl'

          h = {"key1", "val1", "key2", "val2"};

          obj = ["Hello", "world", "I am here", ["where", "what", "are", "you"]];

          class TestClass

          @name = nil;
          @adress = nil;
          @list = nil;

          def initialize(p1, p2)
          @name, @adress = p1, p2;
          @list = [1,2,3.01,-4.35];
          end

          end

          t = TestClass.new("Richo", "37 Scotland Rd, Buckhurst Hill, IG9 5NP");

          obj = ["Hello", "world", t, "I am here", ["where", "what", "are", "you"]];

          str = Yajl::Encoder.encode(obj);

          puts str;`

          Produces

          
          ["Hello","world","#<TestClass;0x144d9cc0>","I am here",["where","what","are","you"]]
          
          

          Instead of the

          
          ["Hello","world",{"@list";[1,2,3.01,-4.35],"@name";"Richo","@adress";"37 Scotland Rd, Buckhurst Hill, IG9 5NP"},"I am here",["where","what","are","you"]]
          
          

          I would expect and which is what my simple JSON Serializer produces.

          http://www.keepingmyhandin.com/

          1 條回覆 最後回覆 回覆 引用 0
          • M 離線
            Myhand
            最後由 編輯 2012年12月1日 下午4:24

            Hi OricAtmos,

            I got it to work now thank you. It seems that it can only serialise Hash and Array objects though, which is good enough for most cases I guess. Given the non-trivial install and given the fact that it uses binary libraries, I suspect it will not work on a MAC either without libraries built for that.

            I will therefore continue with my simple JSON encoder, which while probably much slower, is simple to install and should work out of the box on a MAC as it is pure RUBY. It can also encode standard objects which can be useful.

            Thanks for your help in getting this working though. I will keep it in mind if I ever have to do large data sets.

            http://www.keepingmyhandin.com/

            1 條回覆 最後回覆 回覆 引用 0
            • O 離線
              OricAtmos
              最後由 編輯 2012年12月1日 下午5:17

              All right, I wasn't aware Yajl isn't able to work with arbitrary objects. I was only using it to encode/decode hashes with simple values and arrays.

              1 條回覆 最後回覆 回覆 引用 0
              • M 離線
                Myhand
                最後由 編輯 2012年12月3日 上午12:23

                Hi guys,

                I need some help. I have run into problems creating a json encoding where a string contains a single quote char. e.g. "Betty's pie shop"

                somehow ruby creates a new char which replaces the y and the '. You can see this by executing the following command:

                puts "Betty's pie shop"

                and I get

                Betts pie shops pie shop
                

                if you past this into an editor that supports UTF8 you see that a special char has been added into where the y and ' was. Also note that the last bit of the string is now duplicated.

                I can escape it, but this is then not a valid json string as json does not allow you to escape the ' char.

                http://www.keepingmyhandin.com/

                1 條回覆 最後回覆 回覆 引用 0
                • D 離線
                  driven
                  最後由 編輯 2012年12月3日 上午2:09

                  May be your encoding... only fails with single quotes here

                  > puts "Betty's pie shop"
                  Betty's pie shop
                  nil
                  > p "Betty's pie shop"
                  "Betty's pie shop"
                  nil
                  > print "Betty's pie shop\n"
                  Betty's pie shop
                  nil
                  > print 'Betty's pie shop\n'
                  Error; #<SyntaxError; (eval); compile error
                  (eval); parse error, unexpected tIDENTIFIER, expecting $
                  print 'Betty's pie shop\n'
                                ^>
                  (eval)
                  

                  john

                  learn from the mistakes of others, you may not live long enough to make them all yourself...

                  1 條回覆 最後回覆 回覆 引用 0
                  • D 離線
                    Dan Rathbun
                    最後由 編輯 2012年12月3日 上午2:16

                    @myhand said:

                    ... somehow ruby creates a new char which replaces the y and the '. You can see this by executing the following command:
                    puts "Betty's pie shop"
                    and I get:
                    %(#008000)[>> Betts pie shops pie shop]

                    At the SketchUp Ruby Console:
                    ` puts "Betty's pie shop"
                    >> Betty's pie shop

                    puts "Betty's pie shop".inspect()
                    >> "Betty's pie shop"

                    puts "Betty's pie shop".inspect().inspect()
                    >> ""Betty's pie shop""`

                    I'm not here much anymore.

                    1 條回覆 最後回覆 回覆 引用 0
                    • D 離線
                      Dan Rathbun
                      最後由 編輯 2012年12月3日 上午2:40

                      s = %q(Dan's friggin' advice, "Hey, use Ruby's % lowercase 'q' interpretive delimiter. It's a great help when your bleepin' strings have "'" and '"' characters in them!")
                      

                      %Q and % is a double-quoted string. You can choose any 2 delimeters you want.

                      name = %$MyHand$

                      str = %q{I want some quotes right "HERE"! Let's go.}

                      I'm not here much anymore.

                      1 條回覆 最後回覆 回覆 引用 0
                      • M 離線
                        Myhand
                        最後由 編輯 2012年12月3日 下午1:21

                        @driven said:

                        May be your encoding... only fails with single quotes here
                        john

                        Thought so too initially. But I got the same results by typing straight into the Sketchup Ruby console... Can you set encoding at the Sketchup level?

                        http://www.keepingmyhandin.com/

                        1 條回覆 最後回覆 回覆 引用 0
                        • M 離線
                          Myhand
                          最後由 編輯 2012年12月3日 下午1:31

                          @dan rathbun said:

                          At the SketchUp Ruby Console:
                          ` puts "Betty's pie shop"
                          >> Betty's pie shop

                          puts "Betty's pie shop".inspect()
                          >> "Betty's pie shop"

                          puts "Betty's pie shop".inspect().inspect()
                          >> ""Betty's pie shop""`

                          This works for me today on my work PC. So must be something specific with my Sketchup installation/version at home. Both are Version 8 out of the box installations on Windows. The one that corrupts the "'" char is the Pro addition, while the one at work is the Standard edition.

                          http://www.keepingmyhandin.com/

                          1 條回覆 最後回覆 回覆 引用 0
                          • M 離線
                            Myhand
                            最後由 編輯 2012年12月3日 下午1:42

                            @dan rathbun said:

                            s = %q(Dan's friggin' advice, "Hey, use Ruby's % lowercase 'q' interpretive delimiter. It's a great help when your bleepin' strings have "'" and '"' characters in them!")
                            

                            %Q and % is a double-quoted string. You can choose any 2 delimeters you want.

                            name = %$MyHand$

                            str = %q{I want some quotes right "HERE"! Let's go.}

                            Thank you Dan, I will give this a go tonight. As you might have guessed though the actual string I am having problems with (not the pie shop one 😉) is ComponentDefinition name field. Something like "Bath 6'x34"x54"". So I never actually input the string, I am just reading it. So I am not sure how I will use the above technique to step around the problem. Will have to give it some thought.

                            http://www.keepingmyhandin.com/

                            1 條回覆 最後回覆 回覆 引用 0
                            • D 離線
                              Dan Rathbun
                              最後由 編輯 2012年12月3日 下午2:15

                              Disable ALL plugins and libraries, (Sketchup::plugins_disabled=true , close SketchUp and restart,) then try again ...

                              If it works, then you've installed a plugin that changes the String base class. A very big no-no, in a shared Ruby environment, such as SketchUp embedded Ruby (or DoubleCAD XT embedded Ruby, etc.)

                              In a normal "system" Ruby script, changing a base class doesn't matter because (99% of the time,) a script runs in it's own temporary Ruby process, finishes, and the Ruby process exits.

                              So.. the most likely culprit would be a library that is written to run temporarily in "system" Ruby, such as Yajl, or the standard JSON library, that changes the String class, or the global methods in mixin module Kernel (such as puts,) or even stream classes, like IO.

                              Encoding. It is set to UTF-8 by SketchUp just after it loads the interpreter. It should stay that way. Changing it via $KCODE is not a good idea as that (editable) feature has been removed in newer Ruby versions. (You'd just set your code up to fail in a newer SketchUp version if it distro's with Ruby v1.9.x)

                              I'm not here much anymore.

                              1 條回覆 最後回覆 回覆 引用 0
                              • T 離線
                                thomthom
                                最後由 編輯 2012年12月3日 下午2:48

                                @myhand said:

                                This works for me today on my work PC. So must be something specific with my Sketchup installation/version at home. Both are Version 8 out of the box installations on Windows. The one that corrupts the "'" char is the Pro addition, while the one at work is the Standard edition.

                                Do you have the SketchUp Developer Tools installed? (https://github.com/thomthom/sketchup-developer-tools ) Might be that it's interfering.

                                Try with just your plugin installed.

                                Thomas Thomassen — SketchUp Monkey & Coding addict
                                List of my plugins and link to the CookieWare fund

                                1 條回覆 最後回覆 回覆 引用 0
                                • M 離線
                                  Myhand
                                  最後由 編輯 2012年12月3日 下午3:45

                                  Thanks guys, I will give this a go tonight. Yes I do have the SketchUp Developer Tools installed.

                                  http://www.keepingmyhandin.com/

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • D 離線
                                    Dan Rathbun
                                    最後由 編輯 2012年12月3日 下午5:24

                                    @myhand said:

                                    ... is ComponentDefinition name field. Something like "Bath 6'x34"x54"".

                                    OK.. yea that reminds me. We have similar problems with SketchUp's formatted Length strings when saving things into the registry with Sketchup::write_default() and Sketchup::read_default() methods.

                                    There are some topics on that and using gsub() and tr() before writing values.
                                    The situation is similar because we also get these values with embedded single quotes from the application.

                                    I'm not here much anymore.

                                    1 條回覆 最後回覆 回覆 引用 0
                                    • M 離線
                                      Myhand
                                      最後由 編輯 2012年12月3日 下午9:05

                                      @thomthom said:

                                      @myhand said:

                                      This works for me today on my work PC. So must be something specific with my Sketchup installation/version at home. Both are Version 8 out of the box installations on Windows. The one that corrupts the "'" char is the Pro addition, while the one at work is the Standard edition.

                                      Do you have the SketchUp Developer Tools installed? (https://github.com/thomthom/sketchup-developer-tools ) Might be that it's interfering.

                                      Try with just your plugin installed.

                                      Yup it was the SketchUp Developer Tools. Disabling that fixed the problem thanks.

                                      http://www.keepingmyhandin.com/

                                      1 條回覆 最後回覆 回覆 引用 0
                                      • M 離線
                                        Myhand
                                        最後由 編輯 2012年12月4日 上午12:02

                                        OK, with the single quote now working, I have run into the next (and hopefully the last) problem. It appears as if the

                                        WebDialog.execute_script

                                        method strips all \ characters from its arguments as it passes it into JavaScript. I think it is because it does a eval() call on the JS.

                                        e.g.

                                        js_command = "material_maintenance.Sketchup.callUI(\"a string >\\< with a \\\" in the middle\")"; puts "js_command = #{js_command}";

                                        produces this in the Ruby console:

                                        
                                        js_command = material_maintenance.Sketchup.callUI("a string >\< with a \" in the middle")
                                        
                                        

                                        and this when you print the parameter to the callUI function as it hits JS.

                                        
                                        a string >< with a " in the middle 
                                        
                                        

                                        replacing js_command with

                                        js_command = "material_maintenance.Sketchup.callUI(\"a string >\\u005c\\u0022< with a \\u005c\\u0022 in the middle\")"

                                        works and produces the desired output in JS

                                        
                                        a string >\"< with a \" in the middle
                                        
                                        

                                        but this feels a bit clunky. Any other ideas?

                                        http://www.keepingmyhandin.com/

                                        1 條回覆 最後回覆 回覆 引用 0
                                        • A 離線
                                          Aerilius
                                          最後由 編輯 2012年12月4日 上午12:49

                                          1. You need to escape \ characters in the double-quoted javascript code that you want to bring into the webdialog (this first escaping is what makes a valid Ruby string).
                                            %(#000000)["alert('C:\users')"] # bad Ruby string
                                            %(#000000)["alert('C:\\users')"] # good Ruby string
                                          2. In the webdialog, the string arrives written in a script element. As always, the same escaping rules apply again:
                                            %(#000000)[alert('C:\users')] // bad JavaScript code
                                            %(#000000)[alert('C:\\users')] // good JavaScript code

                                          => So finally this means we need double escaping on the Ruby side!
                                          %(#000000)["alert('C:\\\\users')"] # Ruby string

                                          P.S: SketchUp does not offer error handling for syntax errors in the Ruby-to-JavaScript string. Assuming my js contains user generated data with unknown characters, or is broken (truncated), we will definitely get a popup about "Syntax Error" that we cannot suppress and handle in other ways. In order to get control of such errors, we could send the code as a JavaScript string and eval() it:
                                          %(#000000)[webdialog.execute_script("try{eval(\"}bad_$yn7aX{\")}catch(e){}")]

                                          All of this is an old story and to many people have suffered, discovered, solved this and re-invented the wheel. For an all-in-one solution that covers this and many other WebDialog issues (for example you will discovered that when sending \\\ from JS to Ruby, it randomly drops some ), please take a look and make use of the WebDialogX project.

                                          1 條回覆 最後回覆 回覆 引用 0
                                          • 1
                                          • 2
                                          • 3
                                          • 2 / 3
                                          2 / 3
                                          • 第一個貼文
                                            28/48
                                            最後的貼文
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement