lib/gloo/objs/web/json.rb in gloo-2.5.0 vs lib/gloo/objs/web/json.rb in gloo-3.0.0

- old
+ new

@@ -54,19 +54,19 @@ # # Get a list of message names that this object receives. # def self.messages - return super + %w[get parse pretty] + return super + %w[get set parse pretty] end # # Make the JSON pretty. # def msg_pretty - pretty = JSON.pretty_generate( self.value ) - puts pretty + json = JSON.parse( self.value ) + pretty = JSON.pretty_generate( json ) set_value pretty end # # Get a value from the JSON data. @@ -77,17 +77,42 @@ expr = Gloo::Expr::Expression.new( @engine, @params.tokens ) data = expr.evaluate end return unless data - h = JSON.parse( self.value ) - field = h[ data ] + field = Gloo::Objs::Json.get_value_in_json self.value, data @engine.heap.it.set_to field return field end # + # Convert the target object to JSON and set the value of + # this JSON to that value. + # + def msg_set + if @params&.token_count&.positive? + pn = Gloo::Core::Pn.new( @engine, @params.tokens.first ) + unless pn&.exists? + @engine.err 'Source path for objects does not exist' + return + end + else + @engine.err 'Source path for objects is required' + return + end + parent = pn.resolve + + h = convert_obj_to_hash( parent ) + json = JSON.parse( h.to_json ) + json = JSON.pretty_generate( json ) + + set_value json + @engine.heap.it.set_to json + return json + end + + # # Parse the JSON data and put it in objects. # The additional parameter is the path to the destination # for the parsed objects. # def msg_parse @@ -102,33 +127,65 @@ return end parent = pn.resolve json = JSON.parse( self.value ) - self.handle_json( json, parent ) + self.handle_json_to_obj( json, parent ) end + # --------------------------------------------------------------------- + # JSON Helper functions + # --------------------------------------------------------------------- + + # + # Convert the object to a hash of name and values. + # + def convert_obj_to_hash( obj ) + h = {} + + if obj.child_count > 0 + obj.children.each do |child| + h = h.merge( convert_obj_to_hash( child ) ) + end + else + h[ obj.name ] = obj.value + end + + return h + end + # # Handle JSON, creating objects and setting values. # Note that this is a recursive function. # - def handle_json( json, parent ) + def handle_json_to_obj( json, parent ) if json.class == Hash json.each do |k, v| - if v.class == Array + if ( v.class == Array ) || ( v.class == Hash ) o = parent.find_add_child( k, 'can' ) - handle_json( v, o ) + handle_json_to_obj( v, o ) else o = parent.find_add_child( k, 'untyped' ) o.set_value v end end elsif json.class == Array json.each_with_index do |o, index| child = parent.find_add_child( index.to_s, 'can' ) - handle_json( o, child ) + handle_json_to_obj( o, child ) end end + end + + # + # Parse the JSON data and look for the value within it. + # + def self.get_value_in_json( json, path_to_value ) + data = JSON.parse( json ) + path_to_value.split( '.' ).each do |segment| + data = data[ segment ] + end + return data end end end end