module Twirp::Encoding
Constants
- JSON
- JSON_STRICT
An opt-in content type useful when curling or manually testing a twirp service. This will fail if unknown fields are encountered. The return content type will be application/json.
- PROTO
Public Class Methods
decode(bytes, msg_class, content_type)
click to toggle source
# File lib/twirp/encoding.rb, line 28 def decode(bytes, msg_class, content_type) case content_type when JSON then msg_class.decode_json(bytes, ignore_unknown_fields: true) when JSON_STRICT then msg_class.decode_json(bytes, ignore_unknown_fields: false) when PROTO then msg_class.decode(bytes) else raise ArgumentError.new("Invalid content_type") end end
decode_json(bytes)
click to toggle source
# File lib/twirp/encoding.rb, line 49 def decode_json(bytes) ::JSON.parse(bytes) end
encode(msg_obj, msg_class, content_type)
click to toggle source
# File lib/twirp/encoding.rb, line 37 def encode(msg_obj, msg_class, content_type) case content_type when JSON, JSON_STRICT then msg_class.encode_json(msg_obj, emit_defaults: true) when PROTO then msg_class.encode(msg_obj) else raise ArgumentError.new("Invalid content_type") end end
encode_json(attrs)
click to toggle source
# File lib/twirp/encoding.rb, line 45 def encode_json(attrs) ::JSON.generate(attrs) end
valid_content_type?(content_type)
click to toggle source
# File lib/twirp/encoding.rb, line 53 def valid_content_type?(content_type) content_type == JSON || content_type == PROTO || content_type == JSON_STRICT end
valid_content_types()
click to toggle source
# File lib/twirp/encoding.rb, line 57 def valid_content_types [JSON, PROTO] end