sketchucation logo sketchucation
    • Login
    ⚠️ Attention | Having issues with Sketchucation Tools 5? Report Here

    JSON in Ruby

    Scheduled Pinned Locked Moved Developers' Forum
    48 Posts 7 Posters 7.3k Views 7 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      @myhand said:

      I can also not call them as you would call a static method, i.e. JSON.escape().

      OK Example:

      Assume your "plugins" dir, has sub-dir "myhand", which has a sub-dir "lib", which contains a file "json.rb"

      I recommend having your nested dir names match your nested namespaces, so you can remember the relative paths when it comes time to type a require expression.

      module MyHand
        module Lib
          module JSON # mixin module
      
            def escape(value)
              ret = ""
              value.split("").each do |c|
                if (/["\\\/\b\f\n\r\t]/.match( c ) )
                  ret << '\\' << c
                else
                  ret << c
                end
              end
              return ret
            end # escape()
      
          end # module JSON
        end # module Lib
      end # module MyHand
      

      .. and in a plugin:

      module Author
        module NiftyPlugin
       
          # make sure the lib file is loaded
          require("myhand/lib/json.rb")
       
          # mix in the library module, as a nested module;
          module JSON
            #
            # bring in public mixin instance methods
            # as public module methods in THIS module;
            #
            extend(MyHand;;Lib;;JSON)
            #
          end # module
       
          # use it calling a module method;
          str = JSON.escape("\tHello World!\n")
      
          puts(str)
       
        end # module NiftyPlugin
      end # module Author
      

      OR ... using module_function() like the Math module does ...

      module MyHand
        module Lib
          module JSON # mixin module
      
            module_function()
      
            def escape(value)
              ret = ""
              value.split("").each do |c|
                if (/["\\\/\b\f\n\r\t]/.match( c ) )
                  ret << '\\' << c
                else
                  ret << c
                end
              end
              return ret
            end # escape()
      
          end # module JSON
        end # module Lib
      end # module MyHand
      

      .. and in a plugin:

      module Author
        module NiftyPlugin
       
          # make sure the lib file is loaded
          require("myhand/lib/json.rb")
       
          # Use a library module function, via a
          # local constant aliasing the library;
          JSON = MyHand;;Lib;;JSON
       
          # use it calling a module method;
          str = JSON.escape("\tHello World!\n")
      
          puts(str)
       
        end # module NiftyPlugin
      end # module Author
      

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • OricAtmosO Offline
        OricAtmos
        last edited by

        Dan already explained more than I could have. He also wrote a nice ruby script to include additional folders in $LOAD_PATH. Read all about it here: http://sketchucation.com/forums/viewtopic.php?f=180&t=29412&p=342471&hilit=!loadpaths

        1 Reply Last reply Reply Quote 0
        • M Offline
          Myhand
          last edited by

          @dan rathbun said:

          @myhand said:

          I tried to get this to work but keep getting errors when trying to include the library:
          require 'yajl'
          If you do not specify some kind of filepath, then the file must be in one of the directories listed in the $LOAD_PATH array.

          Ruby's require() first checks to see if the argument resolves to an absolute path, and if so, checks to see if the file exists, and if true, attempts to load it.

          Secondly, it checks to see if the argument is a relative path (incl. no path at all,) and if so, require() then iterates the $LOAD_PATH array prepending the base paths in front of your relative path. If it finds match, it loads the file, IF such a path is NOT ALREADY present in the $LOADED_FEATURES (aka $") array.

          Thank you Dan for the detailed explanation and links to starter guides. I will review and try this when I get home tonight. One interesting point in this case though is that the only file called yajl.* is c:/temp/yajl.rb which is also the file that contains the

          require 'yajl/yajl'
          

          line. There is a a subdirectory called yajl, but no files named "yajl.rb", "yajl.so" or "yajl.dll" anywhere else in the yajl library distribution... So cannot see how adding the "c:/temp" path to $LOAD_PATH will find the file as it does not appear to exist.

          Will let you know.

          http://www.keepingmyhandin.com/

          1 Reply Last reply Reply Quote 0
          • OricAtmosO Offline
            OricAtmos
            last edited by

            @myhand said:

            There is a a subdirectory called yajl, but no files named "yajl.rb", "yajl.so" or "yajl.dll" anywhere else in the yajl library distribution... So cannot see how adding the "c:/temp" path to $LOAD_PATH will find the file as it does not appear to exist.

            Sounds like you don't have the complete library.
            This is a list of files I have in my Yajl folder:

            
            [...]\rubylibs\yajl.rb
            [...]\rubylibs\yajl\1.8\yajl.so
            [...]\rubylibs\yajl\1.9\yajl.so
            [...]\rubylibs\yajl\bzip2.rb
            [...]\rubylibs\yajl\bzip2\stream_reader.rb
            [...]\rubylibs\yajl\bzip2\stream_writer.rb
            [...]\rubylibs\yajl\deflate.rb
            [...]\rubylibs\yajl\deflate\stream_reader.rb
            [...]\rubylibs\yajl\deflate\stream_writer.rb
            [...]\rubylibs\yajl\gzip.rb
            [...]\rubylibs\yajl\gzip\stream_reader.rb
            [...]\rubylibs\yajl\gzip\stream_writer.rb
            [...]\rubylibs\yajl\http_stream.rb
            [...]\rubylibs\yajl\json_gem.rb
            [...]\rubylibs\yajl\json_gem\encoding.rb
            [...]\rubylibs\yajl\json_gem\parsing.rb
            [...]\rubylibs\yajl\version.rb
            [...]\rubylibs\yajl\yajl.rb
            
            
            1 Reply Last reply Reply Quote 0
            • M Offline
              Myhand
              last edited by

              @oricatmos said:

              @myhand said:

              There is a a subdirectory called yajl, but no files named "yajl.rb", "yajl.so" or "yajl.dll" anywhere else in the yajl library distribution... So cannot see how adding the "c:/temp" path to $LOAD_PATH will find the file as it does not appear to exist.

              Sounds like you don't have the complete library.
              This is a list of files I have in my Yajl folder:

              Yes OricAtmos, I think you are right. I took my version from here

              https://rubygems.org/gems/yajl-ruby.

              As I cannot install the gem in Sketchup I unzipped the gem file and took the files from the lib directory. This gives me the following files which is clearly not complete.

              
              C;\Temp\lib\yajl
              C;\Temp\lib\yajl.rb
              C;\Temp\lib\yajl\bzip2
              C;\Temp\lib\yajl\bzip2.rb
              C;\Temp\lib\yajl\deflate
              C;\Temp\lib\yajl\deflate.rb
              C;\Temp\lib\yajl\gzip
              C;\Temp\lib\yajl\gzip.rb
              C;\Temp\lib\yajl\http_stream.rb
              C;\Temp\lib\yajl\json_gem
              C;\Temp\lib\yajl\json_gem.rb
              C;\Temp\lib\yajl\version.rb
              C;\Temp\lib\yajl\bzip2\stream_reader.rb
              C;\Temp\lib\yajl\bzip2\stream_writer.rb
              C;\Temp\lib\yajl\deflate\stream_reader.rb
              C;\Temp\lib\yajl\deflate\stream_writer.rb
              C;\Temp\lib\yajl\gzip\stream_reader.rb
              C;\Temp\lib\yajl\gzip\stream_writer.rb
              C;\Temp\lib\yajl\json_gem\encoding.rb
              C;\Temp\lib\yajl\json_gem\parsing.rb
              
              

              Where did you get the library from?

              Cheers,

              myhand

              http://www.keepingmyhandin.com/

              1 Reply Last reply Reply Quote 0
              • OricAtmosO Offline
                OricAtmos
                last edited by

                @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 Reply Last reply Reply Quote 0
                • M Offline
                  Myhand
                  last edited by

                  @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 Reply Last reply Reply Quote 0
                  • OricAtmosO Offline
                    OricAtmos
                    last edited by

                    @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 Reply Last reply Reply Quote 0
                    • M Offline
                      Myhand
                      last edited by

                      @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 Reply Last reply Reply Quote 0
                      • M Offline
                        Myhand
                        last edited by

                        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 Reply Last reply Reply Quote 0
                        • OricAtmosO Offline
                          OricAtmos
                          last edited by

                          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 Reply Last reply Reply Quote 0
                          • M Offline
                            Myhand
                            last edited by

                            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 Reply Last reply Reply Quote 0
                            • D Offline
                              driven
                              last edited by

                              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 Reply Last reply Reply Quote 0
                              • Dan RathbunD Offline
                                Dan Rathbun
                                last edited by

                                @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 Reply Last reply Reply Quote 0
                                • Dan RathbunD Offline
                                  Dan Rathbun
                                  last edited by

                                  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 Reply Last reply Reply Quote 0
                                  • M Offline
                                    Myhand
                                    last edited by

                                    @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 Reply Last reply Reply Quote 0
                                    • M Offline
                                      Myhand
                                      last edited by

                                      @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 Reply Last reply Reply Quote 0
                                      • M Offline
                                        Myhand
                                        last edited by

                                        @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 Reply Last reply Reply Quote 0
                                        • Dan RathbunD Offline
                                          Dan Rathbun
                                          last edited by

                                          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 Reply Last reply Reply Quote 0
                                          • thomthomT Offline
                                            thomthom
                                            last edited by

                                            @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 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 3
                                            • 3 / 3
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement