diff --git a/lib/protoboeuf/codegen.rb b/lib/protoboeuf/codegen.rb index af9b1e1..cd692d1 100644 --- a/lib/protoboeuf/codegen.rb +++ b/lib/protoboeuf/codegen.rb @@ -88,8 +88,7 @@ def result(message, toplevel_enums, generate_types:, requires:, syntax:, options end end - attr_reader :message, :fields, :oneof_fields, :syntax - attr_reader :optional_fields, :enum_field_types + attr_reader :message, :fields, :oneof_fields, :syntax, :optional_fields, :enum_field_types, :oneof_json_names def initialize(message, toplevel_enums, generate_types:, requires:, syntax:, options:) @message = message @@ -109,6 +108,7 @@ def initialize(message, toplevel_enums, generate_types:, requires:, syntax:, opt @oneof_fields = [] @enum_fields = [] @oneof_selection_fields = [] + @oneof_json_names = {} optional_field_count = 0 @@ -123,6 +123,7 @@ def initialize(message, toplevel_enums, generate_types:, requires:, syntax:, opt optional_field_count += 1 elsif field.has_oneof_index? (@oneof_fields[field.oneof_index] ||= []) << field + @oneof_json_names[field.name] = field.json_name elsif field.type == :TYPE_ENUM @enum_fields << field else @@ -164,27 +165,46 @@ def conversion field.has_oneof_index? && !field.optional? end - oneofs = @oneof_selection_fields.map do |field| - "send(#{field.name.dump}).tap { |f| result[f.to_sym] = send(f) if f }" - end + oneofs = @oneof_selection_fields <<~RUBY #{type_signature(returns: "T::Hash[Symbol, T.untyped]")} def to_h result = {} - #{(oneofs + fields.map { |field| convert_field(field) }).join("\n")} + #{oneofs.map { |oneof| hash_assignment_for_oneof(oneof, json: false) }.join("\n")} + #{fields.map { |field| hash_assignment_for_field(field, json: false) }.join("\n")} + result + end + + #{type_signature(params: "T::Hash[T.untyped, T.untyped]", returns: "T::Hash[Symbol, T.untyped]")} + def as_json(options = {}) + result = {} + #{oneofs.map { |oneof| hash_assignment_for_oneof(oneof, json: true) }.join("\n")} + #{fields.map { |field| hash_assignment_for_field(field, json: true) }.join("\n")} result end + + def to_json(options = {}) + require 'json' + JSON.dump(as_json(options)) + end RUBY end - def convert_field(field) + def hash_assignment_for_oneof(oneof, json: false) + "send(field.name.dump).tap { |f| result[f.to_sym] = send(f) if f }" + end + + def hash_assignment_for_field(field, json: false) + key_string = "#{(json ? field.json_name : field.name).dump}.to_sym" + recurse_with = json ? "as_json" : "to_h" + if field.repeated? - "result['#{field.name}'.to_sym] = #{iv_name(field)}" + "result[#{key_string}] = #{iv_name(field)}" elsif field.type == :TYPE_MESSAGE - "result['#{field.name}'.to_sym] = #{iv_name(field)}.to_h" + "result[#{key_string}] = #{iv_name(field)}.#{recurse_with}" else - "result['#{field.name}'.to_sym] = #{iv_name(field)}" + "result[#{key_string}] = #{iv_name(field)}" end end @@ -782,6 +802,7 @@ def initialize_code initialize_type_signature(fields) + "def initialize(" + initialize_signature + ")\n" + init_bitmask(message) + + init_oneof_json_names(message) + initialize_oneofs + fields.map { |field| if field.has_oneof_index? && !field.optional? @@ -910,6 +931,13 @@ def iv_name(field) "@#{field.name}" end + # Our generated code needs to keep a hash of oneof field names to their corresponding JSON name. This is + # unnecessary for other field types since we can access the json_name upon code generation, but the particular + # oneof field isn't known until runtime. + def oneof_json_name(field) + "@_oneof_json_names[:#{field.name}]" + end + def initialize_required_field(field) <<~RUBY #{bounds_check(field, lvar_read(field)).chomp} @@ -1650,6 +1678,18 @@ def init_bitmask(msg) end end + ONEOF_JSON_NAMES = ERB.new(<<~RUBY, trim_mode: "-") + @_oneof_json_names = { + <%- oneof_json_names.each do |field_name, json_name| -%> + :<%= field_name.dump %> => <%= json_name.dump %>, + <%- end %> + } + RUBY + + def init_oneof_json_names(_msg) + ONEOF_JSON_NAMES.result(binding) + end + def set_bitmask(field) # rubocop:disable Naming/AccessorMethodName i = @optional_field_bit_lut.fetch(field.number) || raise("optional field should have a bit") "@_bitmask |= #{format("%#018x", 1 << i)}" diff --git a/lib/protoboeuf/decorated_field.rb b/lib/protoboeuf/decorated_field.rb index a6255c9..5de14a3 100644 --- a/lib/protoboeuf/decorated_field.rb +++ b/lib/protoboeuf/decorated_field.rb @@ -8,7 +8,16 @@ class DecoratedField extend Forwardable - def_delegators :@original_field, :name, :label, :type_name, :type, :number, :options, :oneof_index, :has_oneof_index? + def_delegators :@original_field, + :name, + :label, + :type_name, + :type, + :number, + :options, + :oneof_index, + :has_oneof_index?, + :json_name def initialize(field:, message:, syntax:) @original_field = field diff --git a/lib/protoboeuf/google/protobuf/any.rb b/lib/protoboeuf/google/protobuf/any.rb index 5977153..906cff6 100644 --- a/lib/protoboeuf/google/protobuf/any.rb +++ b/lib/protoboeuf/google/protobuf/any.rb @@ -28,6 +28,8 @@ def value=(v) end def initialize(type_url: "", value: "") + @_oneof_json_names = {} + @type_url = type_url @value = value @@ -553,10 +555,24 @@ def _encode(buff) def to_h result = {} + result["type_url".to_sym] = @type_url result["value".to_sym] = @value result end + + def as_json(options = {}) + result = {} + + result["typeUrl".to_sym] = @type_url + result["value".to_sym] = @value + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/boolvalue.rb b/lib/protoboeuf/google/protobuf/boolvalue.rb index ed3424c..cdf4860 100644 --- a/lib/protoboeuf/google/protobuf/boolvalue.rb +++ b/lib/protoboeuf/google/protobuf/boolvalue.rb @@ -22,6 +22,8 @@ def value=(v) end def initialize(value: false) + @_oneof_json_names = {} + @value = value end @@ -349,9 +351,22 @@ def _encode(buff) def to_h result = {} + + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/bytesvalue.rb b/lib/protoboeuf/google/protobuf/bytesvalue.rb index 05905a2..bf9a2ad 100644 --- a/lib/protoboeuf/google/protobuf/bytesvalue.rb +++ b/lib/protoboeuf/google/protobuf/bytesvalue.rb @@ -22,6 +22,8 @@ def value=(v) end def initialize(value: "") + @_oneof_json_names = {} + @value = value end @@ -408,9 +410,22 @@ def _encode(buff) def to_h result = {} + + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/descriptor.rb b/lib/protoboeuf/google/protobuf/descriptor.rb index af152fe..108f36c 100644 --- a/lib/protoboeuf/google/protobuf/descriptor.rb +++ b/lib/protoboeuf/google/protobuf/descriptor.rb @@ -67,6 +67,8 @@ def file=(v) end def initialize(file: []) + @_oneof_json_names = {} + @file = file end @@ -512,9 +514,22 @@ def _encode(buff) def to_h result = {} + + result["file".to_sym] = @file + result + end + + def as_json(options = {}) + result = {} + result["file".to_sym] = @file result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class FileDescriptorProto def self.decode(buff) @@ -648,6 +663,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if name == nil @name = "" else @@ -3357,6 +3374,7 @@ def _encode(buff) def to_h result = {} + result["name".to_sym] = @name result["package".to_sym] = @package result["dependency".to_sym] = @dependency @@ -3372,6 +3390,30 @@ def to_h result["edition".to_sym] = @edition result end + + def as_json(options = {}) + result = {} + + result["name".to_sym] = @name + result["package".to_sym] = @package + result["dependency".to_sym] = @dependency + result["publicDependency".to_sym] = @public_dependency + result["weakDependency".to_sym] = @weak_dependency + result["messageType".to_sym] = @message_type + result["enumType".to_sym] = @enum_type + result["service".to_sym] = @service + result["extension".to_sym] = @extension + result["options".to_sym] = @options.as_json + result["sourceCodeInfo".to_sym] = @source_code_info.as_json + result["syntax".to_sym] = @syntax + result["edition".to_sym] = @edition + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class DescriptorProto def self.decode(buff) @@ -3430,6 +3472,8 @@ def options=(v) def initialize(start: nil, end: nil, options: nil) @_bitmask = 0 + @_oneof_json_names = {} + if start == nil @start = 0 else @@ -4205,11 +4249,26 @@ def _encode(buff) def to_h result = {} + result["start".to_sym] = @start result["end".to_sym] = @end result["options".to_sym] = @options.to_h result end + + def as_json(options = {}) + result = {} + + result["start".to_sym] = @start + result["end".to_sym] = @end + result["options".to_sym] = @options.as_json + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class ReservedRange @@ -4254,6 +4313,8 @@ def end=(v) def initialize(start: nil, end: nil) @_bitmask = 0 + @_oneof_json_names = {} + if start == nil @start = 0 else @@ -4840,10 +4901,24 @@ def _encode(buff) def to_h result = {} + result["start".to_sym] = @start result["end".to_sym] = @end result end + + def as_json(options = {}) + result = {} + + result["start".to_sym] = @start + result["end".to_sym] = @end + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end # required field readers @@ -4928,6 +5003,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if name == nil @name = "" else @@ -7055,6 +7132,7 @@ def _encode(buff) def to_h result = {} + result["name".to_sym] = @name result["field".to_sym] = @field result["extension".to_sym] = @extension @@ -7067,6 +7145,27 @@ def to_h result["reserved_name".to_sym] = @reserved_name result end + + def as_json(options = {}) + result = {} + + result["name".to_sym] = @name + result["field".to_sym] = @field + result["extension".to_sym] = @extension + result["nestedType".to_sym] = @nested_type + result["enumType".to_sym] = @enum_type + result["extensionRange".to_sym] = @extension_range + result["oneofDecl".to_sym] = @oneof_decl + result["options".to_sym] = @options.as_json + result["reservedRange".to_sym] = @reserved_range + result["reservedName".to_sym] = @reserved_name + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class ExtensionRangeOptions def self.decode(buff) @@ -7140,6 +7239,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if number == nil @number = 0 else @@ -8038,6 +8139,7 @@ def _encode(buff) def to_h result = {} + result["number".to_sym] = @number result["full_name".to_sym] = @full_name result["type".to_sym] = @type @@ -8045,6 +8147,22 @@ def to_h result["repeated".to_sym] = @repeated result end + + def as_json(options = {}) + result = {} + + result["number".to_sym] = @number + result["fullName".to_sym] = @full_name + result["type".to_sym] = @type + result["reserved".to_sym] = @reserved + result["repeated".to_sym] = @repeated + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end module VerificationState DECLARATION = 0 @@ -8108,6 +8226,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + @uninterpreted_option = uninterpreted_option @declaration = declaration @@ -9104,12 +9224,28 @@ def _encode(buff) def to_h result = {} + result["uninterpreted_option".to_sym] = @uninterpreted_option result["declaration".to_sym] = @declaration result["features".to_sym] = @features.to_h result["verification".to_sym] = @verification result end + + def as_json(options = {}) + result = {} + + result["uninterpretedOption".to_sym] = @uninterpreted_option + result["declaration".to_sym] = @declaration + result["features".to_sym] = @features.as_json + result["verification".to_sym] = @verification + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class FieldDescriptorProto def self.decode(buff) @@ -9319,6 +9455,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if name == nil @name = "" else @@ -11243,6 +11381,7 @@ def _encode(buff) def to_h result = {} + result["name".to_sym] = @name result["number".to_sym] = @number result["label".to_sym] = @label @@ -11256,6 +11395,28 @@ def to_h result["proto3_optional".to_sym] = @proto3_optional result end + + def as_json(options = {}) + result = {} + + result["name".to_sym] = @name + result["number".to_sym] = @number + result["label".to_sym] = @label + result["type".to_sym] = @type + result["typeName".to_sym] = @type_name + result["extendee".to_sym] = @extendee + result["defaultValue".to_sym] = @default_value + result["oneofIndex".to_sym] = @oneof_index + result["jsonName".to_sym] = @json_name + result["options".to_sym] = @options.as_json + result["proto3Optional".to_sym] = @proto3_optional + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class OneofDescriptorProto def self.decode(buff) @@ -11289,6 +11450,8 @@ def options=(v) def initialize(name: nil, options: nil) @_bitmask = 0 + @_oneof_json_names = {} + if name == nil @name = "" else @@ -11876,10 +12039,24 @@ def _encode(buff) def to_h result = {} + result["name".to_sym] = @name result["options".to_sym] = @options.to_h result end + + def as_json(options = {}) + result = {} + + result["name".to_sym] = @name + result["options".to_sym] = @options.as_json + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class EnumDescriptorProto def self.decode(buff) @@ -11931,6 +12108,8 @@ def end=(v) def initialize(start: nil, end: nil) @_bitmask = 0 + @_oneof_json_names = {} + if start == nil @start = 0 else @@ -12517,10 +12696,24 @@ def _encode(buff) def to_h result = {} + + result["start".to_sym] = @start + result["end".to_sym] = @end + result + end + + def as_json(options = {}) + result = {} + result["start".to_sym] = @start result["end".to_sym] = @end result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end # required field readers @@ -12570,6 +12763,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if name == nil @name = "" else @@ -13707,6 +13902,7 @@ def _encode(buff) def to_h result = {} + result["name".to_sym] = @name result["value".to_sym] = @value result["options".to_sym] = @options.to_h @@ -13714,6 +13910,22 @@ def to_h result["reserved_name".to_sym] = @reserved_name result end + + def as_json(options = {}) + result = {} + + result["name".to_sym] = @name + result["value".to_sym] = @value + result["options".to_sym] = @options.as_json + result["reservedRange".to_sym] = @reserved_range + result["reservedName".to_sym] = @reserved_name + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class EnumValueDescriptorProto def self.decode(buff) @@ -13759,6 +13971,8 @@ def options=(v) def initialize(name: nil, number: nil, options: nil) @_bitmask = 0 + @_oneof_json_names = {} + if name == nil @name = "" else @@ -14511,11 +14725,26 @@ def _encode(buff) def to_h result = {} + result["name".to_sym] = @name result["number".to_sym] = @number result["options".to_sym] = @options.to_h result end + + def as_json(options = {}) + result = {} + + result["name".to_sym] = @name + result["number".to_sym] = @number + result["options".to_sym] = @options.as_json + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class ServiceDescriptorProto def self.decode(buff) @@ -14555,6 +14784,8 @@ def options=(v) def initialize(name: nil, method: [], options: nil) @_bitmask = 0 + @_oneof_json_names = {} + if name == nil @name = "" else @@ -15340,11 +15571,26 @@ def _encode(buff) def to_h result = {} + result["name".to_sym] = @name result["method".to_sym] = @method result["options".to_sym] = @options.to_h result end + + def as_json(options = {}) + result = {} + + result["name".to_sym] = @name + result["method".to_sym] = @method + result["options".to_sym] = @options.as_json + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class MethodDescriptorProto def self.decode(buff) @@ -15413,6 +15659,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if name == nil @name = "" else @@ -16478,6 +16726,7 @@ def _encode(buff) def to_h result = {} + result["name".to_sym] = @name result["input_type".to_sym] = @input_type result["output_type".to_sym] = @output_type @@ -16486,6 +16735,23 @@ def to_h result["server_streaming".to_sym] = @server_streaming result end + + def as_json(options = {}) + result = {} + + result["name".to_sym] = @name + result["inputType".to_sym] = @input_type + result["outputType".to_sym] = @output_type + result["options".to_sym] = @options.as_json + result["clientStreaming".to_sym] = @client_streaming + result["serverStreaming".to_sym] = @server_streaming + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class FileOptions def self.decode(buff) @@ -16697,6 +16963,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if java_package == nil @java_package = "" else @@ -19719,6 +19987,7 @@ def _encode(buff) def to_h result = {} + result["java_package".to_sym] = @java_package result["java_outer_classname".to_sym] = @java_outer_classname result["java_multiple_files".to_sym] = @java_multiple_files @@ -19744,6 +20013,40 @@ def to_h result["uninterpreted_option".to_sym] = @uninterpreted_option result end + + def as_json(options = {}) + result = {} + + result["javaPackage".to_sym] = @java_package + result["javaOuterClassname".to_sym] = @java_outer_classname + result["javaMultipleFiles".to_sym] = @java_multiple_files + result[ + "javaGenerateEqualsAndHash".to_sym + ] = @java_generate_equals_and_hash + result["javaStringCheckUtf8".to_sym] = @java_string_check_utf8 + result["optimizeFor".to_sym] = @optimize_for + result["goPackage".to_sym] = @go_package + result["ccGenericServices".to_sym] = @cc_generic_services + result["javaGenericServices".to_sym] = @java_generic_services + result["pyGenericServices".to_sym] = @py_generic_services + result["deprecated".to_sym] = @deprecated + result["ccEnableArenas".to_sym] = @cc_enable_arenas + result["objcClassPrefix".to_sym] = @objc_class_prefix + result["csharpNamespace".to_sym] = @csharp_namespace + result["swiftPrefix".to_sym] = @swift_prefix + result["phpClassPrefix".to_sym] = @php_class_prefix + result["phpNamespace".to_sym] = @php_namespace + result["phpMetadataNamespace".to_sym] = @php_metadata_namespace + result["rubyPackage".to_sym] = @ruby_package + result["features".to_sym] = @features.as_json + result["uninterpretedOption".to_sym] = @uninterpreted_option + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class MessageOptions def self.decode(buff) @@ -19819,6 +20122,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if message_set_wire_format == nil @message_set_wire_format = false else @@ -20908,6 +21213,7 @@ def _encode(buff) def to_h result = {} + result["message_set_wire_format".to_sym] = @message_set_wire_format result[ "no_standard_descriptor_accessor".to_sym @@ -20921,6 +21227,28 @@ def to_h result["uninterpreted_option".to_sym] = @uninterpreted_option result end + + def as_json(options = {}) + result = {} + + result["messageSetWireFormat".to_sym] = @message_set_wire_format + result[ + "noStandardDescriptorAccessor".to_sym + ] = @no_standard_descriptor_accessor + result["deprecated".to_sym] = @deprecated + result["mapEntry".to_sym] = @map_entry + result[ + "deprecatedLegacyJsonFieldConflicts".to_sym + ] = @deprecated_legacy_json_field_conflicts + result["features".to_sym] = @features.as_json + result["uninterpretedOption".to_sym] = @uninterpreted_option + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class FieldOptions def self.decode(buff) @@ -20964,6 +21292,8 @@ def value=(v) def initialize(edition: nil, value: nil) @_bitmask = 0 + @_oneof_json_names = {} + if edition == nil @edition = 0 else @@ -21527,10 +21857,24 @@ def _encode(buff) def to_h result = {} + + result["edition".to_sym] = @edition + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["edition".to_sym] = @edition result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class FeatureSupport @@ -21592,6 +21936,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if edition_introduced == nil @edition_introduced = 0 else @@ -22476,12 +22822,28 @@ def _encode(buff) def to_h result = {} + result["edition_introduced".to_sym] = @edition_introduced result["edition_deprecated".to_sym] = @edition_deprecated result["deprecation_warning".to_sym] = @deprecation_warning result["edition_removed".to_sym] = @edition_removed result end + + def as_json(options = {}) + result = {} + + result["editionIntroduced".to_sym] = @edition_introduced + result["editionDeprecated".to_sym] = @edition_deprecated + result["deprecationWarning".to_sym] = @deprecation_warning + result["editionRemoved".to_sym] = @edition_removed + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end module CType STRING = 0 @@ -22710,6 +23072,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if ctype == nil @ctype = 0 else @@ -24927,6 +25291,7 @@ def _encode(buff) def to_h result = {} + result["ctype".to_sym] = @ctype result["packed".to_sym] = @packed result["jstype".to_sym] = @jstype @@ -24943,6 +25308,31 @@ def to_h result["uninterpreted_option".to_sym] = @uninterpreted_option result end + + def as_json(options = {}) + result = {} + + result["ctype".to_sym] = @ctype + result["packed".to_sym] = @packed + result["jstype".to_sym] = @jstype + result["lazy".to_sym] = @lazy + result["unverifiedLazy".to_sym] = @unverified_lazy + result["deprecated".to_sym] = @deprecated + result["weak".to_sym] = @weak + result["debugRedact".to_sym] = @debug_redact + result["retention".to_sym] = @retention + result["targets".to_sym] = @targets + result["editionDefaults".to_sym] = @edition_defaults + result["features".to_sym] = @features.as_json + result["featureSupport".to_sym] = @feature_support.as_json + result["uninterpretedOption".to_sym] = @uninterpreted_option + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class OneofOptions def self.decode(buff) @@ -24975,6 +25365,8 @@ def features=(v) def initialize(features: nil, uninterpreted_option: []) @_bitmask = 0 + @_oneof_json_names = {} + if features == nil @features = nil else @@ -25612,10 +26004,24 @@ def _encode(buff) def to_h result = {} + result["features".to_sym] = @features.to_h result["uninterpreted_option".to_sym] = @uninterpreted_option result end + + def as_json(options = {}) + result = {} + + result["features".to_sym] = @features.as_json + result["uninterpretedOption".to_sym] = @uninterpreted_option + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class EnumOptions def self.decode(buff) @@ -25675,6 +26081,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if allow_alias == nil @allow_alias = false else @@ -26584,6 +26992,7 @@ def _encode(buff) def to_h result = {} + result["allow_alias".to_sym] = @allow_alias result["deprecated".to_sym] = @deprecated result[ @@ -26593,6 +27002,24 @@ def to_h result["uninterpreted_option".to_sym] = @uninterpreted_option result end + + def as_json(options = {}) + result = {} + + result["allowAlias".to_sym] = @allow_alias + result["deprecated".to_sym] = @deprecated + result[ + "deprecatedLegacyJsonFieldConflicts".to_sym + ] = @deprecated_legacy_json_field_conflicts + result["features".to_sym] = @features.as_json + result["uninterpretedOption".to_sym] = @uninterpreted_option + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class EnumValueOptions def self.decode(buff) @@ -26652,6 +27079,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if deprecated == nil @deprecated = false else @@ -27658,6 +28087,7 @@ def _encode(buff) def to_h result = {} + result["deprecated".to_sym] = @deprecated result["features".to_sym] = @features.to_h result["debug_redact".to_sym] = @debug_redact @@ -27665,6 +28095,22 @@ def to_h result["uninterpreted_option".to_sym] = @uninterpreted_option result end + + def as_json(options = {}) + result = {} + + result["deprecated".to_sym] = @deprecated + result["features".to_sym] = @features.as_json + result["debugRedact".to_sym] = @debug_redact + result["featureSupport".to_sym] = @feature_support.as_json + result["uninterpretedOption".to_sym] = @uninterpreted_option + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class ServiceOptions def self.decode(buff) @@ -27704,6 +28150,8 @@ def deprecated=(v) def initialize(features: nil, deprecated: nil, uninterpreted_option: []) @_bitmask = 0 + @_oneof_json_names = {} + if features == nil @features = nil else @@ -28433,11 +28881,26 @@ def _encode(buff) def to_h result = {} + result["features".to_sym] = @features.to_h result["deprecated".to_sym] = @deprecated result["uninterpreted_option".to_sym] = @uninterpreted_option result end + + def as_json(options = {}) + result = {} + + result["features".to_sym] = @features.as_json + result["deprecated".to_sym] = @deprecated + result["uninterpretedOption".to_sym] = @uninterpreted_option + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class MethodOptions def self.decode(buff) @@ -28513,6 +28976,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if deprecated == nil @deprecated = false else @@ -29403,12 +29868,28 @@ def _encode(buff) def to_h result = {} + result["deprecated".to_sym] = @deprecated result["idempotency_level".to_sym] = @idempotency_level result["features".to_sym] = @features.to_h result["uninterpreted_option".to_sym] = @uninterpreted_option result end + + def as_json(options = {}) + result = {} + + result["deprecated".to_sym] = @deprecated + result["idempotencyLevel".to_sym] = @idempotency_level + result["features".to_sym] = @features.as_json + result["uninterpretedOption".to_sym] = @uninterpreted_option + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class UninterpretedOption def self.decode(buff) @@ -29441,6 +29922,8 @@ def is_extension=(v) end def initialize(name_part: "", is_extension: false) + @_oneof_json_names = {} + @name_part = name_part @is_extension = is_extension @@ -29913,10 +30396,24 @@ def _encode(buff) def to_h result = {} + result["name_part".to_sym] = @name_part result["is_extension".to_sym] = @is_extension result end + + def as_json(options = {}) + result = {} + + result["namePart".to_sym] = @name_part + result["isExtension".to_sym] = @is_extension + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end # required field readers @@ -29995,6 +30492,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + @name = name if identifier_value == nil @@ -31287,6 +31786,7 @@ def _encode(buff) def to_h result = {} + result["name".to_sym] = @name result["identifier_value".to_sym] = @identifier_value result["positive_int_value".to_sym] = @positive_int_value @@ -31296,6 +31796,24 @@ def to_h result["aggregate_value".to_sym] = @aggregate_value result end + + def as_json(options = {}) + result = {} + + result["name".to_sym] = @name + result["identifierValue".to_sym] = @identifier_value + result["positiveIntValue".to_sym] = @positive_int_value + result["negativeIntValue".to_sym] = @negative_int_value + result["doubleValue".to_sym] = @double_value + result["stringValue".to_sym] = @string_value + result["aggregateValue".to_sym] = @aggregate_value + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class FeatureSet def self.decode(buff) @@ -31491,6 +32009,8 @@ def initialize( message_encoding: nil, json_format: nil ) + @_oneof_json_names = {} + if field_presence == nil @field_presence = 0 else @@ -32698,6 +33218,7 @@ def _encode(buff) def to_h result = {} + result["field_presence".to_sym] = @field_presence result["enum_type".to_sym] = @enum_type result["repeated_field_encoding".to_sym] = @repeated_field_encoding @@ -32706,6 +33227,23 @@ def to_h result["json_format".to_sym] = @json_format result end + + def as_json(options = {}) + result = {} + + result["fieldPresence".to_sym] = @field_presence + result["enumType".to_sym] = @enum_type + result["repeatedFieldEncoding".to_sym] = @repeated_field_encoding + result["utf8Validation".to_sym] = @utf8_validation + result["messageEncoding".to_sym] = @message_encoding + result["jsonFormat".to_sym] = @json_format + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class FeatureSetDefaults def self.decode(buff) @@ -32760,6 +33298,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + if edition == nil @edition = 0 else @@ -33552,11 +34092,26 @@ def _encode(buff) def to_h result = {} + result["edition".to_sym] = @edition result["overridable_features".to_sym] = @overridable_features.to_h result["fixed_features".to_sym] = @fixed_features.to_h result end + + def as_json(options = {}) + result = {} + + result["edition".to_sym] = @edition + result["overridableFeatures".to_sym] = @overridable_features.as_json + result["fixedFeatures".to_sym] = @fixed_features.as_json + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end # required field readers @@ -33587,6 +34142,8 @@ def maximum_edition=(v) end def initialize(defaults: [], minimum_edition: nil, maximum_edition: nil) + @_oneof_json_names = {} + @defaults = defaults if minimum_edition == nil @@ -34352,11 +34909,26 @@ def _encode(buff) def to_h result = {} + result["defaults".to_sym] = @defaults result["minimum_edition".to_sym] = @minimum_edition result["maximum_edition".to_sym] = @maximum_edition result end + + def as_json(options = {}) + result = {} + + result["defaults".to_sym] = @defaults + result["minimumEdition".to_sym] = @minimum_edition + result["maximumEdition".to_sym] = @maximum_edition + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class SourceCodeInfo def self.decode(buff) @@ -34436,6 +35008,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + path.each do |v| unless -2_147_483_648 <= v && v <= 2_147_483_647 raise RangeError, @@ -35673,6 +36247,7 @@ def _encode(buff) def to_h result = {} + result["path".to_sym] = @path result["span".to_sym] = @span result["leading_comments".to_sym] = @leading_comments @@ -35682,6 +36257,24 @@ def to_h ] = @leading_detached_comments result end + + def as_json(options = {}) + result = {} + + result["path".to_sym] = @path + result["span".to_sym] = @span + result["leadingComments".to_sym] = @leading_comments + result["trailingComments".to_sym] = @trailing_comments + result[ + "leadingDetachedComments".to_sym + ] = @leading_detached_comments + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end # required field readers @@ -35692,6 +36285,8 @@ def location=(v) end def initialize(location: []) + @_oneof_json_names = {} + @location = location end @@ -36137,9 +36732,22 @@ def _encode(buff) def to_h result = {} + result["location".to_sym] = @location result end + + def as_json(options = {}) + result = {} + + result["location".to_sym] = @location + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class GeneratedCodeInfo def self.decode(buff) @@ -36248,6 +36856,8 @@ def initialize( ) @_bitmask = 0 + @_oneof_json_names = {} + path.each do |v| unless -2_147_483_648 <= v && v <= 2_147_483_647 raise RangeError, @@ -37409,6 +38019,7 @@ def _encode(buff) def to_h result = {} + result["path".to_sym] = @path result["source_file".to_sym] = @source_file result["begin".to_sym] = @begin @@ -37416,6 +38027,22 @@ def to_h result["semantic".to_sym] = @semantic result end + + def as_json(options = {}) + result = {} + + result["path".to_sym] = @path + result["sourceFile".to_sym] = @source_file + result["begin".to_sym] = @begin + result["end".to_sym] = @end + result["semantic".to_sym] = @semantic + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end # required field readers @@ -37426,6 +38053,8 @@ def annotation=(v) end def initialize(annotation: []) + @_oneof_json_names = {} + @annotation = annotation end @@ -37871,9 +38500,22 @@ def _encode(buff) def to_h result = {} + result["annotation".to_sym] = @annotation result end + + def as_json(options = {}) + result = {} + + result["annotation".to_sym] = @annotation + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/doublevalue.rb b/lib/protoboeuf/google/protobuf/doublevalue.rb index 7bd516a..24a324a 100644 --- a/lib/protoboeuf/google/protobuf/doublevalue.rb +++ b/lib/protoboeuf/google/protobuf/doublevalue.rb @@ -22,6 +22,8 @@ def value=(v) end def initialize(value: 0.0) + @_oneof_json_names = {} + @value = value end @@ -343,9 +345,22 @@ def _encode(buff) def to_h result = {} + + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/duration.rb b/lib/protoboeuf/google/protobuf/duration.rb index c8848e9..a400728 100644 --- a/lib/protoboeuf/google/protobuf/duration.rb +++ b/lib/protoboeuf/google/protobuf/duration.rb @@ -39,6 +39,8 @@ def nanos=(v) end def initialize(seconds: 0, nanos: 0) + @_oneof_json_names = {} + unless -9_223_372_036_854_775_808 <= seconds && seconds <= 9_223_372_036_854_775_807 raise RangeError, @@ -597,10 +599,24 @@ def _encode(buff) def to_h result = {} + result["seconds".to_sym] = @seconds result["nanos".to_sym] = @nanos result end + + def as_json(options = {}) + result = {} + + result["seconds".to_sym] = @seconds + result["nanos".to_sym] = @nanos + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/field_mask.rb b/lib/protoboeuf/google/protobuf/field_mask.rb index a63d3af..6457984 100644 --- a/lib/protoboeuf/google/protobuf/field_mask.rb +++ b/lib/protoboeuf/google/protobuf/field_mask.rb @@ -22,6 +22,8 @@ def paths=(v) end def initialize(paths: []) + @_oneof_json_names = {} + @paths = paths end @@ -423,9 +425,22 @@ def _encode(buff) def to_h result = {} + + result["paths".to_sym] = @paths + result + end + + def as_json(options = {}) + result = {} + result["paths".to_sym] = @paths result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/floatvalue.rb b/lib/protoboeuf/google/protobuf/floatvalue.rb index 395f0f3..11058a8 100644 --- a/lib/protoboeuf/google/protobuf/floatvalue.rb +++ b/lib/protoboeuf/google/protobuf/floatvalue.rb @@ -22,6 +22,8 @@ def value=(v) end def initialize(value: 0.0) + @_oneof_json_names = {} + @value = value end @@ -343,9 +345,22 @@ def _encode(buff) def to_h result = {} + + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/int32value.rb b/lib/protoboeuf/google/protobuf/int32value.rb index 99e1f76..6723aac 100644 --- a/lib/protoboeuf/google/protobuf/int32value.rb +++ b/lib/protoboeuf/google/protobuf/int32value.rb @@ -27,6 +27,8 @@ def value=(v) end def initialize(value: 0) + @_oneof_json_names = {} + unless -2_147_483_648 <= value && value <= 2_147_483_647 raise RangeError, "Value (#{value}) for field value is out of bounds (-2147483648..2147483647)" @@ -429,9 +431,22 @@ def _encode(buff) def to_h result = {} + + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/int64value.rb b/lib/protoboeuf/google/protobuf/int64value.rb index b0c8f8c..469a2af 100644 --- a/lib/protoboeuf/google/protobuf/int64value.rb +++ b/lib/protoboeuf/google/protobuf/int64value.rb @@ -28,6 +28,8 @@ def value=(v) end def initialize(value: 0) + @_oneof_json_names = {} + unless -9_223_372_036_854_775_808 <= value && value <= 9_223_372_036_854_775_807 raise RangeError, @@ -431,9 +433,22 @@ def _encode(buff) def to_h result = {} + + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/stringvalue.rb b/lib/protoboeuf/google/protobuf/stringvalue.rb index 4ac9462..436ddc8 100644 --- a/lib/protoboeuf/google/protobuf/stringvalue.rb +++ b/lib/protoboeuf/google/protobuf/stringvalue.rb @@ -22,6 +22,8 @@ def value=(v) end def initialize(value: "") + @_oneof_json_names = {} + @value = value end @@ -408,9 +410,22 @@ def _encode(buff) def to_h result = {} + + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/struct.rb b/lib/protoboeuf/google/protobuf/struct.rb index 0228456..c3257d8 100644 --- a/lib/protoboeuf/google/protobuf/struct.rb +++ b/lib/protoboeuf/google/protobuf/struct.rb @@ -34,6 +34,8 @@ def fields=(v) end def initialize(fields: {}) + @_oneof_json_names = {} + @fields = fields end @@ -628,9 +630,22 @@ def _encode(buff) def to_h result = {} + + result["fields".to_sym] = @fields + result + end + + def as_json(options = {}) + result = {} + result["fields".to_sym] = @fields result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class Value def self.decode(buff) @@ -698,6 +713,14 @@ def initialize( struct_value: nil, list_value: nil ) + @_oneof_json_names = { + null_value: "nullValue", + number_value: "numberValue", + string_value: "stringValue", + bool_value: "boolValue", + struct_value: "structValue", + list_value: "listValue" + } @kind = nil # oneof field if null_value == nil @null_value = 0 @@ -1784,9 +1807,22 @@ def _encode(buff) def to_h result = {} - send("kind").tap { |f| result[f.to_sym] = send(f) if f } + send(field.name.dump).tap { |f| result[f.to_sym] = send(f) if f } + + result + end + + def as_json(options = {}) + result = {} + send(field.name.dump).tap { |f| result[f.to_sym] = send(f) if f } + result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end class ListValue def self.decode(buff) @@ -1805,6 +1841,8 @@ def values=(v) end def initialize(values: []) + @_oneof_json_names = {} + @values = values end @@ -2250,9 +2288,22 @@ def _encode(buff) def to_h result = {} + + result["values".to_sym] = @values + result + end + + def as_json(options = {}) + result = {} + result["values".to_sym] = @values result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/timestamp.rb b/lib/protoboeuf/google/protobuf/timestamp.rb index ff5cf7c..73a9786 100644 --- a/lib/protoboeuf/google/protobuf/timestamp.rb +++ b/lib/protoboeuf/google/protobuf/timestamp.rb @@ -39,6 +39,8 @@ def nanos=(v) end def initialize(seconds: 0, nanos: 0) + @_oneof_json_names = {} + unless -9_223_372_036_854_775_808 <= seconds && seconds <= 9_223_372_036_854_775_807 raise RangeError, @@ -597,10 +599,24 @@ def _encode(buff) def to_h result = {} + result["seconds".to_sym] = @seconds result["nanos".to_sym] = @nanos result end + + def as_json(options = {}) + result = {} + + result["seconds".to_sym] = @seconds + result["nanos".to_sym] = @nanos + result + end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/uint32value.rb b/lib/protoboeuf/google/protobuf/uint32value.rb index bd54058..9963f3d 100644 --- a/lib/protoboeuf/google/protobuf/uint32value.rb +++ b/lib/protoboeuf/google/protobuf/uint32value.rb @@ -27,6 +27,8 @@ def value=(v) end def initialize(value: 0) + @_oneof_json_names = {} + unless 0 <= value && value <= 18_446_744_073_709_551_615 raise RangeError, "Value (#{value}) for field value is out of bounds (0..18446744073709551615)" @@ -412,9 +414,22 @@ def _encode(buff) def to_h result = {} + + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end diff --git a/lib/protoboeuf/google/protobuf/uint64value.rb b/lib/protoboeuf/google/protobuf/uint64value.rb index e1764d8..f292a5e 100644 --- a/lib/protoboeuf/google/protobuf/uint64value.rb +++ b/lib/protoboeuf/google/protobuf/uint64value.rb @@ -27,6 +27,8 @@ def value=(v) end def initialize(value: 0) + @_oneof_json_names = {} + unless 0 <= value && value <= 18_446_744_073_709_551_615 raise RangeError, "Value (#{value}) for field value is out of bounds (0..18446744073709551615)" @@ -412,9 +414,22 @@ def _encode(buff) def to_h result = {} + + result["value".to_sym] = @value + result + end + + def as_json(options = {}) + result = {} + result["value".to_sym] = @value result end + + def to_json(options = {}) + require "json" + JSON.dump(as_json(options)) + end end end end