Class: Gloo::Objs::Json
- Inherits:
-
Core::Obj
- Object
- Core::Baseo
- Core::Obj
- Gloo::Objs::Json
- Defined in:
- lib/gloo/objs/web/json.rb
Constant Summary collapse
- KEYWORD =
'json'.freeze
- KEYWORD_SHORT =
'json'.freeze
Constants inherited from Core::Baseo
Core::Baseo::NOT_IMPLEMENTED_ERR
Instance Attribute Summary
Attributes inherited from Core::Obj
Attributes inherited from Core::Baseo
Class Method Summary collapse
-
.convert_obj_to_hash(obj) ⇒ Object
Convert the object to a hash of name and values.
-
.convert_obj_to_json(obj) ⇒ Object
Convert the object to JSON.
-
.get_value_in_json(json, path_to_value) ⇒ Object
Parse the JSON data and look for the value within it.
-
.messages ⇒ Object
Get a list of message names that this object receives.
-
.short_typename ⇒ Object
The short name of the object type.
-
.typename ⇒ Object
The name of the object type.
Instance Method Summary collapse
-
#handle_json_to_obj(json, parent) ⇒ Object
Handle JSON, creating objects and setting values.
-
#line_count ⇒ Object
Get the number of lines of text.
-
#msg_get ⇒ Object
Get a value from the JSON data.
-
#msg_parse ⇒ Object
Parse the JSON data and put it in objects.
-
#msg_pretty ⇒ Object
Make the JSON pretty.
-
#msg_set ⇒ Object
Convert the target object to JSON and set the value of this JSON to that value.
-
#multiline_value? ⇒ Boolean
Does this object support multi-line values? Initially only true for scripts.
-
#set_value(new_value) ⇒ Object
Set the value with any necessary type conversions.
Methods inherited from Core::Obj
#add_child, #add_children_on_create?, #add_default_children, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, help, inherited, #initialize, #is_alias?, #is_function?, #msg_reload, #msg_unload, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?
Methods inherited from Core::Baseo
Constructor Details
This class inherits a constructor from Gloo::Core::Obj
Class Method Details
.convert_obj_to_hash(obj) ⇒ Object
Convert the object to a hash of name and values.
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/gloo/objs/web/json.rb', line 160 def self.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 |
.convert_obj_to_json(obj) ⇒ Object
Convert the object to JSON.
147 148 149 150 151 152 153 154 155 |
# File 'lib/gloo/objs/web/json.rb', line 147 def self.convert_obj_to_json( obj ) # TODO: put container objects in an array h = obj ? convert_obj_to_hash( obj ) : {} json = JSON.parse( h.to_json ) json = JSON.pretty_generate( json ) return json end |
.get_value_in_json(json, path_to_value) ⇒ Object
Parse the JSON data and look for the value within it.
200 201 202 203 204 205 206 |
# File 'lib/gloo/objs/web/json.rb', line 200 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 |
.messages ⇒ Object
Get a list of message names that this object receives.
58 59 60 |
# File 'lib/gloo/objs/web/json.rb', line 58 def self. return super + %w[get set parse pretty] end |
.short_typename ⇒ Object
The short name of the object type.
25 26 27 |
# File 'lib/gloo/objs/web/json.rb', line 25 def self.short_typename return KEYWORD_SHORT end |
.typename ⇒ Object
The name of the object type.
18 19 20 |
# File 'lib/gloo/objs/web/json.rb', line 18 def self.typename return KEYWORD end |
Instance Method Details
#handle_json_to_obj(json, parent) ⇒ Object
Handle JSON, creating objects and setting values. Note that this is a recursive function.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/gloo/objs/web/json.rb', line 178 def handle_json_to_obj( json, parent ) if json.class == Hash json.each do |k, v| if ( v.class == Array ) || ( v.class == Hash ) o = parent.find_add_child( k, 'can' ) 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_to_obj( o, child ) end end end |
#line_count ⇒ Object
Get the number of lines of text.
47 48 49 |
# File 'lib/gloo/objs/web/json.rb', line 47 def line_count return value.split( "\n" ).count end |
#msg_get ⇒ Object
Get a value from the JSON data. The additional parameter is the path to the value.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/gloo/objs/web/json.rb', line 80 def msg_get if @params&.token_count&.positive? expr = Gloo::Expr::Expression.new( @engine, @params.tokens ) data = expr.evaluate end return unless data field = Gloo::Objs::Json.get_value_in_json self.value, data @engine.heap.it.set_to field return field end |
#msg_parse ⇒ Object
Parse the JSON data and put it in objects. The additional parameter is the path to the destination for the parsed objects.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/gloo/objs/web/json.rb', line 123 def msg_parse if @params&.token_count&.positive? pn = Gloo::Core::Pn.new( @engine, @params.tokens.first ) unless pn&.exists? @engine.err 'Destination path for parsed objects does not exist' return end else @engine.err 'Destination path for parsed objects is required' return end parent = pn.resolve json = JSON.parse( self.value ) self.handle_json_to_obj( json, parent ) end |
#msg_pretty ⇒ Object
Make the JSON pretty.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gloo/objs/web/json.rb', line 65 def msg_pretty return unless self.value json = JSON.parse( self.value ) if self.value.start_with?( '"{' ) json = JSON.parse( json ) end pretty = JSON.pretty_generate( json ) set_value pretty end |
#msg_set ⇒ Object
Convert the target object to JSON and set the value of this JSON to that value.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/gloo/objs/web/json.rb', line 96 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 |
#multiline_value? ⇒ Boolean
Does this object support multi-line values? Initially only true for scripts.
40 41 42 |
# File 'lib/gloo/objs/web/json.rb', line 40 def multiline_value? return false end |
#set_value(new_value) ⇒ Object
Set the value with any necessary type conversions.
32 33 34 |
# File 'lib/gloo/objs/web/json.rb', line 32 def set_value( new_value ) self.value = new_value.to_s end |