Decode a YAML serialized object - ruby

I have serialized an object in YAML and send it to a remote worker.
The worker doesent have the object definition so i get a YAML::Object.
How can i access the field inside it?
A text field seems like that base64 encoded, how can i decode that? (no, decode64 not works).

you can pass the object as something "known between both sides" (like an openstruct or hash) or give the description to the client.
It would be interesting to have a serialization format that also serialized the class and its methods...I'll have to think about that one...

try c["bar"]
you can also see all the provided keys using c.keys

Related

gob is appending gibberish to my object while decoding

I was trying to encode and decode HTTP responses. to deal with the body I created a custom ReadCloser with its own UnmarshalBinary and MarshalBinary methods. The gob output was inconsistent with the output of the UnmarshalBinary
I also created a sample repo to demonstrate the same - https://github.com/slayerjain/gob-decode-issue.
I've also created an issue on the golang repo - https://github.com/golang/go/issues/51645
Thanks to a user on Reddit I found the solution. The problem is that in the UnmarshalBinary method I need to create a copy of the byte array. Else it'll be populated with other data since it's a pointer.
ref: https://www.reddit.com/r/golang/comments/tddjdd/gob_is_appending_gibberish_to_my_object/

Is there a way that we can maintain a json in FHIR?

I wanted to store a JSON in FHIR. JSON can be anything, for example, it can be something like
{
assignee: "PCC OFF SHORE",
dueby: "30-03-1991",
description: "This will be assigned to PCC off shoure"
}
You can store any arbitrary data as a Binary resource in an Attachment data type (e.g. DocumentReference or in an extension). Technically, you could also put it in a string data type, but that wouldn't be terribly appropriate as strings are not expected to be parseable.
Certain FHIR resources accept elements of type value[x]. That type accepts variations like valueString or valueCode. For your case, valueBase64Binary may be a good alternative.
Read: https://www.hl7.org/fhir/extensibility.html#extension

JSON Parsing a Redis.get return

I'm storing some data in Redis and when I retrieve this data I'm having trouble parsing it.
When I run this:
$redis.get("data") I get this: "[{\"login\"=>\"name\", \"id\"=>1574}]"
When I try to use JSON.parse against the return body I get this error:
JSON::ParserError: 409: unexpected token at '{"login"=>"name", "id"=>1574}]'
What am I doing wrong?
You need to first serialize your ruby object into JSON before storing it in redis.
In this case you would need to call $redis.set('data', JSON.dump(data)) instead of what is currently being called, which is $redis.set('data', data.to_s). The .to_s gets called on whatever object is passed into redis' set method.
If you really need to parse the data you have already stored then you can use ruby's eval, although you should not use this in production!
$ data = eval($redis.get('data'))
=> [{"login"=>"name", "id"=>1574}]
$ $redis.set('data', JSON.dump(data))
"[{\"login\"=>\"name\", \"id\"=>1574}]"
is not json string
If you want to deserialize string then you write this.
"[{\"login\":\"name\", \"id\":1574}]"
=> is changed to :
So, you need to modify the creation JSON string.

What is the `data` object passed to a Codec's `decode` method?

I'd like to write a Codec plugin to enable LogStash to decode a binary data format.
The official documentation for writing a Codec shows that I need to define a decode method that accepts a single parameter: a variable called data.
I'm new to both LogStash and Ruby. Having worked mostly with statically typed languages, I'm unsure how to learn more about the data variable. I assume that it's analogous to an InputStream-type object, allowing me to read data as it becomes available, but I'm not sure.
Questions:
What type is the data object? What methods does it have?
How do Ruby developers typically go about investigating variables like this? I'm not sure I see a way to figure it out without writing a skeleton plugin and dumping a string representation of data to STDOUT.
Thanks!
The documentation for writing an input plugin hints at this. From the run() method section:
data = $stdin.sysread(16384)
#codec.decode(data) do |event|
decorate(event)
event.set("host", #host) if !event.include?("host")
queue << event
end
The data variable is a Ruby String, which is being used as a buffer of arbitrary bytes. I have verified this by creating a skeleton plugin and inspecting the value at runtime.
This seems to be cause for caution: the bytes provided to your codec's decode method are not guaranteed to be a complete event.

Should a JSON-P callback function accept a string?

I'm calling a REST API somebody else created. It supports JSONP to facilitate cross domain access.
The response I get back from the service looks like:
mycallback('{"token": "123456789"}');
Notice the single quotes wrapping the JSON data; Passing it as a string rather than a raw object. JQuery can handle this, but other libraries seem to expect a raw object instead.
mycallback({"token": "123456789"});
The raw object parameter makes more sense to me since it avoids the need to parse the JSON data, but I want to know for sure before asking the maintainer of the API to make the adjustment:
Which is most correct?
Passing a javascript literal (second) as shown here is more correct as it avoids deserializing the string back to a javascript object.
Passing a string is obviously a bad thing - you have two choices (#1 is preferred):
Ask the developer of the JSONP service to send proper JSONp instead of a string
Make your callback function smart so it uses something like payload = JSON.parse(payload); in case payload is a string.

Resources