Class: GlooLang::Objs::Json
- Inherits:
-
Core::Obj
- Object
- Core::Baseo
- Core::Obj
- GlooLang::Objs::Json
- Defined in:
- lib/gloo_lang/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
-
.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(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.
-
#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, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, help, inherited, #initialize, #msg_reload, #msg_unload, #pn, #remove_child, #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 GlooLang::Core::Obj
Class Method Details
.messages ⇒ Object
Get a list of message names that this object receives.
58 59 60 |
# File 'lib/gloo_lang/objs/web/json.rb', line 58 def self. return super + %w[get parse pretty] end |
.short_typename ⇒ Object
The short name of the object type.
25 26 27 |
# File 'lib/gloo_lang/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_lang/objs/web/json.rb', line 18 def self.typename return KEYWORD end |
Instance Method Details
#handle_json(json, parent) ⇒ Object
Handle JSON, creating objects and setting values. Note that this is a recursive function.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gloo_lang/objs/web/json.rb', line 114 def handle_json( json, parent ) if json.class == Hash json.each do |k, v| if v.class == Array o = parent.find_add_child( k, 'can' ) handle_json( 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 ) end end end |
#line_count ⇒ Object
Get the number of lines of text.
47 48 49 |
# File 'lib/gloo_lang/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.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gloo_lang/objs/web/json.rb', line 75 def msg_get if @params&.token_count&.positive? expr = GlooLang::Expr::Expression.new( @engine, @params.tokens ) data = expr.evaluate end return unless data h = JSON.parse( self.value ) field = h[ 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.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/gloo_lang/objs/web/json.rb', line 93 def msg_parse if @params&.token_count&.positive? pn = GlooLang::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( json, parent ) end |
#msg_pretty ⇒ Object
Make the JSON pretty.
65 66 67 68 69 |
# File 'lib/gloo_lang/objs/web/json.rb', line 65 def msg_pretty pretty = JSON.pretty_generate( self.value ) puts pretty set_value pretty end |
#multiline_value? ⇒ Boolean
Does this object support multi-line values? Initially only true for scripts.
40 41 42 |
# File 'lib/gloo_lang/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_lang/objs/web/json.rb', line 32 def set_value( new_value ) self.value = new_value.to_s end |