Serializing Protobuf Reflection - reflection

Is there a way to through reflection to retrieve the equivalent of the .proto file which generated a protobuf object class from an instance of an object?
Essentially, it should be possible for a client to get this definition and run the equivalent of protoc on the type that was serialized such that it could decode future data (principally for dynamically typed languages).
I could send the .proto files, but then I need to worry about keeping the files in sync with the objects rather than having the protoc output be able to dump the .proto definition.

Related

(de)Serializing stream using System.Text.Json

I am trying to serialize an object into a MemoryStream using System.Text.Json's JsonSerializer. I am unable to find the implementation/method of that in the documentation. Can someone share the sample implementation for serialization and deserialization using System.Text.Json?
UPDATE
.NET 6 added JsonSerializer.Serialize overloads that write to a stream. It's now possible to write just :
JsonSerializer.Serialize(stream,myObject);
This produces unindented JSON using UTF8 without BOM
Original Answer
It's unclear what the problem is, or what documentation and examples are missing, as there's are multiple sections in learn.microsoft.com and hundreds of blog posts and articles. In the docs JSON serialization and deserialization is a good place to start and How to serialize and deserialize (marshal and unmarshal) JSON in .NET includes the section Serialize to UTF8.
A MemoryStream is just a Stream wrapper over a byte[] array anyway, so serializing to a MemoryStream is the same as serializing to a byte[] array directly. This can be done with JsonSerializer.SerializeToUtf8Bytes:
byte[] jsonUtf8Bytes =JsonSerializer.SerializeToUtf8Bytes(weatherForecast);
And finally, in .NET anything that needs to serialize to something, works through Reader and Writer objects, like TextReader, StreamReader, TextReader and -Writers. In JSON.NET's case, this is done through the Utf8JsonWriter object. JsonSerializer.Serialize has an overload that writes to a Utf8JsonWriter :
using var stream=File.OpenWrite(somePath);
using var writer=new Utf8JsonWriter(stream);
JsonSerializer.Serialize(writer,myObject);
That's the slow way of using System.Text.Json though. Using buffers means allocating them and cleaning them up, which is costly, especially in web applications. For this reason, ASP.NET Core uses IO pipelines instead of streams to receive and send data to sockets, using reusable buffers leased from buffer pools and passed along each step in the ASP.NET Core pipeline. Passing byte[] buffers around copies their contents, so .NET Core introduced the Span<> and Memory<> types, which represent a view over an existing (possibly pooled) buffer. This way, ASP.NET Core passes those "views" of the buffers around, not the buffers themselves.
System.Text.Json was built to use pipelines and reusable memory instead of streams, allowing ASP.NET Core to use minimal memory and as few allocations as possible in high traffic web sites. ASP.NET Core uses the Utf8JsonWriter(IBufferWriter) constructor to write to the output pipeline through a PipeWriter.
We can use the same overload to write to a reusable buffer with an ArrayBufferWriter. That's the equivalent of using a MemoryStream BUT the output is accessed through either a ReadOnlySpan<byte> or Memory<byte> so it doesn't have to be copied around :
using var buffer=new ArrayBufferWriter<byte>(65536);
using var writer=new Utf8JsonWriter(buffer);
JsonSerializer.Serialize(writer,myObject);
ReadOnlySpan<byte> data=buffer.WrittenSpan;
The better option is to use newtonsoft.json.
It has lot of examples

Call remote test library constructor as keyword from Robot Framework

Is it possible to call Test Library constructor from Robot Framework?
Using Remote library interface (NRobot.Server) to connect from RF to Test Library (implemented in C#).
Currently its exposing all public methods implemented under Test Library except constructors.
There are multiple Test Libraries in our project where some functionality implemented as part of constructors.
Hence need a way to call constructor as a test step to execute certain functionality whenever required.
If not possible then may need to move functionality from constructors to new public methods. But want to avoid that if possible.
Thanks in advance...
In short - no.
When calling a remote library, you're actually just the client in an XML-RPC comm protocol; it is the server's responsibility to have the library instantiated, so it (the very same library) can process your instructions and act as needed. Thus normally the library is already instantiated when you call it from your RF code - too late to invoke its constructor.
Naturally, this can be implemented differently - for the remote library server to instantiate the target library on a (special) call, and thus you'll to be able to provide constructor arguments, but that is library design/code change required in it.
This is in contrast of using local libraries, where they are instantiated in your local interpreter, on their import.

Use of Inbuilt rest end point to invoke module in ML database

I am using inbuilt rest end point in Marklogic that allow me to call modules in stored in module database in Marklogic.
http://localhost:8000/LATEST/invoke?data-urlencode=module=/modules/module.xqy&database=databasename&data-urlencode=vars='{"word1":"hello","word2":"world"}'
Does it also provide any option to call direct function present within lib module?
Using vars option it allows us to pass external parameter to the invoking modules. It seems that vars option only allow to pass primitive values to external parameter to invoking module.
But how we can use this vars option to pass XML data to invoking module so that it can be access through external variable defined within module.
Any suggestion would be appreciated.
Note : I am using postman for testing of rest API.
Many thanks.
Since your goal is to get to a library function, consider creating a REST extension instead of using /invoke with a main module. A REST extension can implement your choice of HTTP verbs and accept input in whatever for you'd like. The extension can then convert those inputs to function parameters and call the function.
For more information about REST extensions, see Extending the REST API, which includes an example XQuery extension.

How Handle can be used in Ejb apps? What's its significance?

I can see that Handle stores the reference to the beans. But how its useful for clients calling this ejb?
What are the things that clients can achieve by getting the Handle to the ejb bean?
In RMI-IIOP, a remote reference (stub) needs to be connected to an ORB instance to be usable. If you serialize and deserialize a stub yourself using ObjectOutputStream/ObjectInputStream to store in a file or database, then the deserialized stub will be disconnected, and attempting to use it will fail. If Handle and HomeHandle are serialized instead of the reference itself, then the EJB spec requires them to use the environment's HandleDelegate, which has a reference to the server's ORB instance, so the remote reference can be reconnected after deserialization.

SOAP, HTTP(S) POST. XML and Schema mess

I am very confused. I have a xsd file, no wsdl and apparently i send this data through SOAP. Now looking at all i went back and notice this
(using ssl) The regular session begins
with a HTTP POST request sent by the
client. The body of the request
contains XML document compliant with
SOME_API Request schema
So... i am not using SOAP at all? Am i suppose to do something with the schema file they provide me? No one here (at work) seems to know.
You should start by reading Http made really easy. Soap uses http to send its messages from client to server, and when the document you are talking about is asking you to send a message to the soap server using the HTTP protocol. A bit of googling should find you some nice soap getting started guides.
The message you send is an XML document that uses this schema. The schema defines the types of XML that are valid. Get a good XML editor such as oxygen and tell it you are making a new xml document using a schema (point to your xsd file) and see what you are allowed to type.
My search turned up these two:
Tutorial point SOAP Tutorial
W3Cschools
Just send an HTTP POST with XML that complies to the schema. You can either read the schema manually (which can be hard), or use an XML editor like Tom is suggesting, but there's another possibility: Many platforms offer tools that automatically generate classes from XSD schema, which you can later automatically serialize to get the correct XML.
For example, if you use .NET: Use the xsd.exe tool to generate classes from schema, then just fill these with information as regular classes, then use the XmlSerializer to convert the root class to XML.
Based on your description, it seems that it's not really a SOAP API at all, but rather something like XML-RPC.
Just send an HTTP POST with XML that complies to the schema. You can either read the schema manually (which can be hard), or use an XML editor like Tom is suggesting, but there's another possibility: Many platforms offer tools that automatically generate classes from XSD schema, which you can later automatically serialize to get the correct XML.
For example, if you use .NET: Use the xsd.exe tool to generate classes from schema, then just fill these with information as regular classes, then use the XmlSerializer to convert the root class to XML.
You are learning why standards should be followed.
If this is really a SOAP-based web service, then there must be a WSDL. There is no exception to that. The WSDL is meant to describe everything you need to know about the web service. I strongly suggest you ask the developers of the web service whether it is a SOAP web service, and ask them to supply a WSDL or to explain why they think they should not do that.

Resources